File: ExplainAnalyzer.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (446 lines) | stat: -rw-r--r-- 13,466 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use constant PTDEBUG => $ENV{PTDEBUG} || 0;
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Data::Dumper;
$Data::Dumper::Indent    = 1;
$Data::Dumper::Sortkeys  = 1;
$Data::Dumper::Quotekeys = 0;

use Test::More;

use ExplainAnalyzer;
use QueryRewriter;
use QueryParser;
use DSNParser;
use Sandbox;
use PerconaTest;

my $dp  = new DSNParser(opts=>$dsn_opts);
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master', {no_lc=>1});

if ( !$dbh ) {
   plan skip_all => "Cannot connect to sandbox master";
}

$dbh->do('use sakila');

my $qr  = new QueryRewriter();
my $qp  = new QueryParser();
my $exa = new ExplainAnalyzer(QueryRewriter => $qr, QueryParser => $qp);

# #############################################################################
# Tests for getting an EXPLAIN from a database.
# #############################################################################

my $want = [
         { id            => 1,
           select_type   => 'SIMPLE',
           table         => 'actor',
           type          => 'const',
           possible_keys => 'PRIMARY',
           key           => 'PRIMARY',
           key_len       => 2,
           ref           => 'const',
           rows          => 1,
           Extra         => $sandbox_version eq '5.6' ? undef : '',
         },
      ];
if ( $sandbox_version gt '5.6' ) {
 $want = [
         { id            => 1,
           select_type   => 'SIMPLE',
           table         => 'actor',
           type          => 'const',
           possible_keys => 'PRIMARY',
           key           => 'PRIMARY',
           key_len       => 2,
           filtered      => 100,
           partitions    => undef,
           ref           => 'const',
           rows          => 1,
           Extra         => $sandbox_version gt '5.6' ? undef : '',
         },
      ];
}

my $got = $exa->explain_query(
      dbh   => $dbh,
      query => 'select * from actor where actor_id = 5',
);

$got->[0]->{filtered} = int($got->[0]->{filtered}) if (defined($got->[0]->{filtered}));

is_deeply(
   $got,
   $want,
   'Got a simple EXPLAIN result',
);

$got = $exa->explain_query(
      dbh   => $dbh,
      query => 'delete from actor where actor_id = 5',
);
$got->[0]->{filtered} = int($got->[0]->{filtered}) if (defined($got->[0]->{filtered}));

is_deeply(
   $got,
   $want,
   'Got EXPLAIN result for a DELETE',
);

is(
   $exa->explain_query(
      dbh   => $dbh,
      query => 'insert into t values (1)',
   ),
   undef,
   "Doesn't EXPLAIN non-convertable non-SELECT"
);

# #############################################################################
# NOTE: EXPLAIN will vary between versions, so rely on the database as little as
# possible for tests.  Most things that need an EXPLAIN in the tests below
# should be using a hard-coded data structure.  Thus the following, intended to
# help prevent $dbh being used too much.
# #############################################################################
# XXX $dbh->disconnect;

# #############################################################################
# Tests for normalizing raw EXPLAIN into a format that's easier to work with.
# #############################################################################
is_deeply(
   $exa->normalize(
      [
         { id            => 1,
           select_type   => 'SIMPLE',
           table         => 'film_actor',
           type          => 'index_merge',
           possible_keys => 'PRIMARY,idx_fk_film_id',
           key           => 'PRIMARY,idx_fk_film_id',
           key_len       => '2,2',
           ref           => undef,
           rows          => 34,
           Extra         => 'Using union(PRIMARY,idx_fk_film_id); Using where',
         },
      ],
   ),
   [
      { id            => 1,
        select_type   => 'SIMPLE',
        table         => 'film_actor',
        type          => 'index_merge',
        possible_keys => [qw(PRIMARY idx_fk_film_id)],
        key           => [qw(PRIMARY idx_fk_film_id)],
        key_len       => [2,2],
        ref           => [qw()],
        rows          => 34,
        Extra         => {
           'Using union' => [qw(PRIMARY idx_fk_film_id)],
           'Using where' => 1,
        },
      },
   ],
   'Normalizes an EXPLAIN',
);

is_deeply(
   $exa->normalize(
      [
         { id            => 1,
           select_type   => 'PRIMARY',
           table         => undef,
           type          => undef,
           possible_keys => undef,
           key           => undef,
           key_len       => undef,
           ref           => undef,
           rows          => undef,
           Extra         => 'No tables used',
         },
         { id            => 1,
           select_type   => 'UNION',
           table         => 'a',
           type          => 'index',
           possible_keys => undef,
           key           => 'PRIMARY',
           key_len       => '2',
           ref           => undef,
           rows          => 200,
           Extra         => 'Using index',
         },
         { id            => undef,
           select_type   => 'UNION RESULT',
           table         => '<union1,2>',
           type          => 'ALL',
           possible_keys => undef,
           key           => undef,
           key_len       => undef,
           ref           => undef,
           rows          => undef,
           Extra         => '',
         },
      ],
   ),
   [
      { id            => 1,
        select_type   => 'PRIMARY',
        table         => undef,
        type          => undef,
        possible_keys => [],
        key           => [],
        key_len       => [],
        ref           => [],
        rows          => undef,
        Extra         => {
           'No tables used' => 1,
        },
      },
      { id            => 1,
        select_type   => 'UNION',
        table         => 'a',
        type          => 'index',
        possible_keys => [],
        key           => ['PRIMARY'],
        key_len       => [2],
        ref           => [],
        rows          => 200,
        Extra         => {
         'Using index' => 1,
        },
      },
      { id            => undef,
        select_type   => 'UNION RESULT',
        table         => '<union1,2>',
        type          => 'ALL',
        possible_keys => [],
        key           => [],
        key_len       => [],
        ref           => [],
        rows          => undef,
        Extra         => {},
      },
   ],
   'Normalizes a more complex EXPLAIN',
);

is_deeply(
   $exa->normalize(
      [
         { id            => 1,
           select_type   => 'SIMPLE',
           table         => 't1',
           type          => 'ALL',
           possible_keys => 'PRIMARY',
           key           => undef,
           key_len       => undef,
           ref           => undef,
           rows          => '4',
           # Extra         => 'Using where; Using temporary; Using filesort',
         },
      ],
   ),
   [
      {
         Extra          => {},  # is auto-vivified
         id             => 1,
         select_type    => 'SIMPLE',
         table          => 't1',
         type           => 'ALL',
         possible_keys  => ['PRIMARY'],
         key            => [],
         key_len        => [],
         ref            => [],
         rows           => '4',
      }
   ],
   "normalize() doesn't crash if EXPLAIN Extra is missing"
);

# #############################################################################
# Tests for trimming indexes out of possible_keys.
# #############################################################################
is_deeply(
   $exa->get_alternate_indexes(
      [qw(index1 index2)],
      [qw(index1 index2 index3 index4)],
   ),
   [qw(index3 index4)],
   'Normalizes alternate indexes',
);

# #############################################################################
# Tests for translating aliased names back to their real names.
# #############################################################################

# Putting it all together: given a query and an EXPLAIN, determine which indexes
# the query used.
is_deeply(
   $exa->get_index_usage(
      query => "select * from film_actor as fa inner join sakila.actor as a "
             . "on a.actor_id = fa.actor_id and a.last_name is not null "
             . "where a.actor_id = 5 or film_id = 5",
      db    => 'sakila',
      explain => $exa->normalize(
         [
            { id            => 1,
              select_type   => 'SIMPLE',
              table         => 'fa',
              type          => 'index_merge',
              possible_keys => 'PRIMARY,idx_fk_film_id',
              key           => 'PRIMARY,idx_fk_film_id',
              key_len       => '2,2',
              ref           => undef,
              rows          => 34,
              Extra         => 'Using union(PRIMARY,idx_fk_film_id); Using where',
            },
            { id            => 1,
              select_type   => 'SIMPLE',
              table         => 'a',
              type          => 'eq_ref',
              possible_keys => 'PRIMARY,idx_actor_last_name',
              key           => 'PRIMARY',
              key_len       => '2',
              ref           => 'sakila.fa.actor_id',
              rows          => 1,
              Extra         => 'Using where',
            },
         ],
      ),
   ),
   [  {  db  => 'sakila',
         tbl => 'film_actor',
         idx => [qw(PRIMARY idx_fk_film_id)],
         alt => [],
      },
      {  db  => 'sakila',
         tbl => 'actor',
         idx => [qw(PRIMARY)],
         alt => [qw(idx_actor_last_name)],
      },
   ],
   'Translate an EXPLAIN and a query into simplified index usage',
);

# This is kind of a pathological case.
is_deeply(
   $exa->get_index_usage(
      query   => "select 1 union select count(*) from actor a",
      db      => 'sakila',
      explain => $exa->normalize(
         [
            { id            => 1,
              select_type   => 'PRIMARY',
              table         => undef,
              type          => undef,
              possible_keys => undef,
              key           => undef,
              key_len       => undef,
              ref           => undef,
              rows          => undef,
              Extra         => 'No tables used',
            },
            { id            => 1,
              select_type   => 'UNION',
              table         => 'a',
              type          => 'index',
              possible_keys => undef,
              key           => 'PRIMARY',
              key_len       => '2',
              ref           => undef,
              rows          => 200,
              Extra         => 'Using index',
            },
            { id            => undef,
              select_type   => 'UNION RESULT',
              table         => '<union1,2>',
              type          => 'ALL',
              possible_keys => undef,
              key           => undef,
              key_len       => undef,
              ref           => undef,
              rows          => undef,
              Extra         => '',
            },
         ],
      ),
   ),
   [  {  db  => 'sakila',
         tbl => 'actor',
         idx => [qw(PRIMARY)],
         alt => [],
      },
   ],
   'Translate an EXPLAIN and a query for a harder case',
);

# Here's a query that uses a table but no indexes in it.
is_deeply(
   $exa->get_index_usage(
      query   => "select * from film_text",
      db      => 'sakila',
      explain => $exa->normalize(
         [
            { id            => 1,
              select_type   => 'SIMPLE',
              table         => 'film_text',
              type          => 'ALL',
              possible_keys => undef,
              key           => undef,
              key_len       => undef,
              ref           => undef,
              rows          => 1000,
              Extra         => '',
            },
         ],
      ),
   ),
   [  {  db  => 'sakila',
         tbl => 'film_text',
         idx => [],
         alt => [],
      },
   ],
   'Translate an EXPLAIN for a query that uses no indexes',
);

# #############################################################################
# Methods to save and retrieve index usage for a specific query and database.
# #############################################################################
is_deeply(
   $exa->get_usage_for('0xdeadbeef', 'sakila'),
   undef,
   'No usage recorded for 0xdeadbeef');

$exa->save_usage_for('0xdeadbeef', 'sakila',
   [  {  db  => 'sakila',
         tbl => 'actor',
         idx => [qw(PRIMARY)],
         alt => [],
      },
   ]);

is_deeply(
   $exa->get_usage_for('0xdeadbeef','sakila'),
   [  {  db  => 'sakila',
         tbl => 'actor',
         idx => [qw(PRIMARY)],
         alt => [],
      },
   ],
   'Got saved usage for 0xdeadbeef');

# #############################################################################
# Done.
# #############################################################################
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;