4
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
5
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
6
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
10
use warnings FATAL => 'all';
11
use English qw(-no_match_vars);
15
# See http://code.google.com/p/maatkit/wiki/Testing
16
shift @INC; # MaatkitTest's unshift
17
require "$trunk/bin/pt-index-usage";
20
my $dp = new DSNParser(opts=>$dsn_opts);
21
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
22
my $dbh = $sb->get_dbh_for('master');
25
plan skip_all => 'Cannot connect to sandbox master';
27
if ( !@{ $dbh->selectall_arrayref('show databases like "sakila"') } ) {
28
plan skip_all => "Sakila database is not loaded";
34
my $cnf = '/tmp/12345/my.sandbox.cnf';
35
my @args = ('-F', $cnf, '--save-results-database', 'D=mk');
36
my $samples = "t/pt-index-usage/samples/";
39
$sb->wipe_clean($dbh);
41
mk_index_usage::main(@args, "$trunk/t/lib/samples/empty",
42
qw(--empty-save-results --create-save-results-database --no-report),
43
'-t', 'sakila.actor,sakila.address');
47
my $rows = $dbh->selectcol_arrayref("show databases");
48
my $ok = grep { $_ eq "mk" } @$rows;
51
"--create-save-results-databse"
54
$rows = $dbh->selectcol_arrayref("show tables from `mk`");
57
[qw(index_alternatives index_usage indexes queries tables)],
61
$rows = $dbh->selectall_arrayref("select * from mk.tables order by db, tbl");
65
[qw( sakila actor 0 )],
66
[qw( sakila address 0 )],
68
"Populate tables table (filtered)"
72
$rows = $dbh->selectall_arrayref("select * from mk.indexes order by db, tbl");
76
[qw(sakila actor idx_actor_last_name 0)],
77
[qw(sakila actor PRIMARY 0)],
78
[qw(sakila address idx_fk_city_id 0)],
79
[qw(sakila address PRIMARY 0)],
81
"Populate indexes table (filtered)"
84
$rows = $dbh->selectall_arrayref("select * from mk.queries");
91
$rows = $dbh->selectall_arrayref("select * from mk.index_usage");
95
"No index usage counts yet"
98
$rows = $dbh->selectall_arrayref("select * from mk.index_alternatives");
102
"No index alternatives yet"
105
# Now for the real test.
106
mk_index_usage::main(@args, "$trunk/t/pt-index-usage/samples/slow007.txt",
107
qw(--empty-save-results --no-report), '-t', 'sakila.actor,sakila.address');
109
$rows = $dbh->selectall_arrayref("select * from mk.tables order by db, tbl");
113
[qw( sakila actor 4 )],
114
[qw( sakila address 0 )],
116
"Table access counts"
119
# EXPLAIN results differ a little between 5.0 and 5.1. 5.1 is smarter.
120
my $res = $sandbox_version ge '5.1' ?
123
[qw(sakila actor idx_actor_last_name 1)],
124
[qw(sakila actor PRIMARY 3)],
125
[qw(sakila address idx_fk_city_id 0)],
126
[qw(sakila address PRIMARY 0)],
130
[qw(sakila actor idx_actor_last_name 2)],
131
[qw(sakila actor PRIMARY 2)],
132
[qw(sakila address idx_fk_city_id 0)],
133
[qw(sakila address PRIMARY 0)],
136
$rows = $dbh->selectall_arrayref("select * from mk.indexes order by db, tbl");
143
$rows = $dbh->selectall_arrayref("select * from mk.queries order by query_id");
147
[ 4950186562421969363,
148
"select * from sakila.actor where last_name like ?",
149
"select * from sakila.actor where last_name like 'A%'",
151
[ 10334408417593890092,
152
"select * from sakila.actor where last_name like ? order by actor_id",
153
"select * from sakila.actor where last_name like 'A%' order by actor_id",
155
[ 10891801448710051322,
156
"select * from sakila.actor where actor_id>?",
157
"select * from sakila.actor where actor_id>10",
163
$rows = $dbh->selectall_arrayref("select query_id, db, tbl, idx, sample, cnt from index_usage iu left join queries q using (query_id) order by db, tbl, idx");
164
$res = $sandbox_version ge '5.1' ?
169
qw(sakila actor idx_actor_last_name),
170
"select * from sakila.actor where last_name like 'A%'",
174
10891801448710051322,
175
qw(sakila actor PRIMARY),
176
"select * from sakila.actor where actor_id>10",
180
10334408417593890092,
181
qw(sakila actor PRIMARY),
182
"select * from sakila.actor where last_name like 'A%' order by actor_id",
191
qw(sakila actor idx_actor_last_name),
192
"select * from sakila.actor where last_name like 'A%'",
196
10334408417593890092,
197
qw(sakila actor idx_actor_last_name),
198
"select * from sakila.actor where last_name like 'A%' order by actor_id",
202
10891801448710051322,
203
qw(sakila actor PRIMARY),
204
"select * from sakila.actor where actor_id>10",
214
$rows = $dbh->selectall_arrayref("select db,tbl,idx,alt_idx,sample from index_alternatives a left join queries q using (query_id)");
215
$res = $sandbox_version ge '5.1' ?
216
[[qw(sakila actor PRIMARY idx_actor_last_name),
217
"select * from sakila.actor where last_name like 'A%' order by actor_id"]]
225
# #############################################################################
226
# Run again to check that cnt vals are properly updated.
227
# #############################################################################
228
mk_index_usage::main(@args, "$trunk/t/pt-index-usage/samples/slow007.txt",
229
qw(--no-report), '-t', 'sakila.actor,sakila.address');
231
$rows = $dbh->selectall_arrayref("select * from mk.tables order by db, tbl");
235
[qw( sakila actor 8 )],
236
[qw( sakila address 0 )],
238
"Updated table access counts"
241
# EXPLAIN results differ a little between 5.0 and 5.1. 5.1 is smarter.
242
$res = $sandbox_version ge '5.1' ?
245
[qw(sakila actor idx_actor_last_name 2)],
246
[qw(sakila actor PRIMARY 6)],
247
[qw(sakila address idx_fk_city_id 0)],
248
[qw(sakila address PRIMARY 0)],
252
[qw(sakila actor idx_actor_last_name 4)],
253
[qw(sakila actor PRIMARY 4)],
254
[qw(sakila address idx_fk_city_id 0)],
255
[qw(sakila address PRIMARY 0)],
258
$rows = $dbh->selectall_arrayref("select * from mk.indexes order by db, tbl");
262
"Updated index usage counts"
265
$rows = $dbh->selectall_arrayref("select * from mk.queries order by query_id");
269
[ 4950186562421969363,
270
"select * from sakila.actor where last_name like ?",
271
"select * from sakila.actor where last_name like 'A%'",
273
[ 10334408417593890092,
274
"select * from sakila.actor where last_name like ? order by actor_id",
275
"select * from sakila.actor where last_name like 'A%' order by actor_id",
277
[ 10891801448710051322,
278
"select * from sakila.actor where actor_id>?",
279
"select * from sakila.actor where actor_id>10",
285
$rows = $dbh->selectall_arrayref("select query_id, db, tbl, idx, sample, cnt from index_usage iu left join queries q using (query_id) order by db, tbl, idx");
286
$res = $sandbox_version ge '5.1' ?
291
qw(sakila actor idx_actor_last_name),
292
"select * from sakila.actor where last_name like 'A%'",
296
10891801448710051322,
297
qw(sakila actor PRIMARY),
298
"select * from sakila.actor where actor_id>10",
302
10334408417593890092,
303
qw(sakila actor PRIMARY),
304
"select * from sakila.actor where last_name like 'A%' order by actor_id",
313
qw(sakila actor idx_actor_last_name),
314
"select * from sakila.actor where last_name like 'A%'",
318
10334408417593890092,
319
qw(sakila actor idx_actor_last_name),
320
"select * from sakila.actor where last_name like 'A%' order by actor_id",
324
10891801448710051322,
325
qw(sakila actor PRIMARY),
326
"select * from sakila.actor where actor_id>10",
336
$rows = $dbh->selectall_arrayref("select db,tbl,idx,alt_idx,sample from index_alternatives a left join queries q using (query_id)");
337
$res = $sandbox_version ge '5.1' ?
338
[[qw(sakila actor PRIMARY idx_actor_last_name),
339
"select * from sakila.actor where last_name like 'A%' order by actor_id"]]
344
"Same index alternatives"
348
# #############################################################################
349
# Issue 1184: Make mk-index-usage create views for canned queries
350
# #############################################################################
352
skip "MySQL sandbox version < 5.0", 1 unless $sandbox_version ge '5.0';
355
# #############################################################################
357
# #############################################################################
358
$sb->wipe_clean($dbh);