IndexUsage

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.

Summary
IndexUsageIndexUsage tracks index and tables usage of queries.
Functions
new
add_indexesTell the object that an index exists.
add_queryTell the object that a unique query (class) exists.
add_table_usageIncrease usage count for table (even if no indexes in it are used).
add_index_usageSave information about how a query used an index.
find_unused_indexesFind unused indexes and pass them to the callback.
save_resultsSave all the table, index and query usage information to tables.
_d

Functions

new

sub new

Parameters

%argsArguments

Returns

IndexUsage object

add_indexes

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.

Parameteres

%argsArguments

Required Arguments

dbDatabase name
tblTable name
indexesHashref to an indexes struct returned by TableParser::get_keys()

add_query

sub add_query

Tell the object that a unique query (class) exists.

Parameters

%argsArguments

Required Arguments

query_idQuery ID (hex checksum of fingerprint)
fingerprintQuery fingerprint (QueryRewriter::fingerprint())
sampleQuery SQL

add_table_usage

sub add_table_usage

Increase usage count for table (even if no indexes in it are used).  If saving results, the tables table is updated, too.

Parameters

%argsArguments

Required Arguments

dbDatabase name
tblTable name

add_index_usage

sub add_index_usage

Save information about how a query used an index.

Parameters

%argsArguments

Required Arguments

usageUusage information, in the same format as the output from ExplainAnalyzer::get_index_usage()

Optional Arguments

query_idQuery ID, if saving results; see save_results()

find_unused_indexes

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>] }

Parameters

$callbackCoderef called with unused indexes

save_results

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).

Parameters

%argsArguments

Required Arguments

dbhDBH
dbDatabase where mk-index-usage --save-results tables are located

_d

sub _d
sub new
sub add_indexes
Tell the object that an index exists.
sub add_query
Tell the object that a unique query (class) exists.
sub add_table_usage
Increase usage count for table (even if no indexes in it are used).
sub add_index_usage
Save information about how a query used an index.
sub find_unused_indexes
Find unused indexes and pass them to the callback.
sub save_results
Save all the table, index and query usage information to tables.
sub _d
ExplainAnalyzer contains utility methods for getting and manipulating EXPLAIN data.
sub get_keys
sub fingerprint
sub get_index_usage
Close