IndexUsage tracks index and tables usage of queries. It can then show which indexes are not used. You use it by telling it about all the tables and indexes that exist, and then you give it index usage stats from ExplainAnalyzer. Afterwards, you ask it to show you unused indexes.
If the object is created with a dbh and db, then results (the indexes, tables, queries and index usages) are saved in tables.
IndexUsage | IndexUsage tracks index and tables usage of queries. |
Functions | |
new | |
add_indexes | Tell the object that an index exists. |
add_query | Tell the object that a unique query (class) exists. |
add_table_usage | Increase usage count for table (even if no indexes in it are used). |
add_index_usage | Save information about how a query used an index. |
find_unused_indexes | Find unused indexes and pass them to the callback. |
save_results | Save all the table, index and query usage information to tables. |
_d |
sub add_indexes
Tell the object that an index exists. Internally, it just creates usage counters for the index and the table it belongs to.
%args | Arguments |
db | Database name |
tbl | Table name |
indexes | Hashref to an indexes struct returned by TableParser::get_keys() |
sub add_query
Tell the object that a unique query (class) exists.
%args | Arguments |
query_id | Query ID (hex checksum of fingerprint) |
fingerprint | Query fingerprint (QueryRewriter::fingerprint()) |
sample | Query SQL |
sub add_index_usage
Save information about how a query used an index.
%args | Arguments |
usage | Uusage information, in the same format as the output from ExplainAnalyzer::get_index_usage() |
query_id | Query ID, if saving results; see save_results() |
sub find_unused_indexes
Find unused indexes and pass them to the callback. For every table in every database, determine whether each index was used or not. But only if the table was used. Don’t say “this index should be dropped” if the table was never queried. For each table, collect the unused indexes and execute the callback subroutine with a hashref that looks like this:
{ db => db, tbl => tbl, idx => [<list of unused indexes on this table>] }
$callback | Coderef called with unused indexes |
sub save_results
Save all the table, index and query usage information to tables. This sub should only be called once! If it’s called a second time, the cnt columns will be updated with their current val + this object’s cnt value because of “ON DUPLICATE KEY UPDATE cnt = cnt + ?”. This is required so that the tool can be ran multiple times, updating saved result counts each time. Thus, the tool should only call this sub once. Then it needs to create a new IndexUsage object (unless we implement a reset() sub).
%args | Arguments |
dbh | DBH |
db | Database where mk-index-usage --save-results tables are located |
sub new
Tell the object that an index exists.
sub add_indexes
Tell the object that a unique query (class) exists.
sub add_query
Increase usage count for table (even if no indexes in it are used).
sub add_table_usage
Save information about how a query used an index.
sub add_index_usage
Find unused indexes and pass them to the callback.
sub find_unused_indexes
Save all the table, index and query usage information to tables.
sub save_results
sub _d
sub get_keys
sub fingerprint
sub get_index_usage