| 1 | package DBIx::Class::Helper::ResultSet::Explain; | ||||||||||||||||||||
| 2 | 54 | $DBIx::Class::Helper::ResultSet::Explain::VERSION = '2.032002'; | |||||||||||||||||||
| 3 | # ABSTRACT: Get query plan for a ResultSet | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 54 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 54 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 54 | ▶ | use parent 'DBIx::Class::ResultSet'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 54 | ▶ | use DBIx::Introspector; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub _introspector { | ||||||||||||||||||||
| 13 | my $d = DBIx::Introspector->new(drivers => '2013-12.01'); | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | $d->decorate_driver_connected(MSSQL => splain => 'GETUTCDATE()'); | ||||||||||||||||||||
| 16 | $d->decorate_driver_connected( | ||||||||||||||||||||
| 17 | SQLite => splain => sub { | ||||||||||||||||||||
| 18 | sub { | ||||||||||||||||||||
| 19 | my ($dbh, $query) = @_; | ||||||||||||||||||||
| 20 | my ($sql, @bind) = @{$$query}; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | $sql =~ s/\s*\((.*)\)\s*/$1/; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | shift->selectall_arrayref("EXPLAIN $sql", undef, @bind) | ||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||
| 28 | $d->decorate_driver_connected( | ||||||||||||||||||||
| 29 | Pg => splain => sub { | ||||||||||||||||||||
| 30 | sub { | ||||||||||||||||||||
| 31 | my ($dbh, $query) = @_; | ||||||||||||||||||||
| 32 | my ($sql, @bind) = @{$$query}; | ||||||||||||||||||||
| 33 | shift->selectall_arrayref("EXPLAIN ANALYZE $sql", undef, @bind) | ||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | $d->decorate_driver_connected( | ||||||||||||||||||||
| 39 | mysql => splain => sub { | ||||||||||||||||||||
| 40 | sub { | ||||||||||||||||||||
| 41 | my ($dbh, $query) = @_; | ||||||||||||||||||||
| 42 | my ($sql, @bind) = @{$$query}; | ||||||||||||||||||||
| 43 | shift->selectall_arrayref("EXPLAIN EXTENDED $sql", undef, @bind) | ||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | return $d; | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 54 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 54 | my $i; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | sub explain { | ||||||||||||||||||||
| 56 | 0 | ▶ | $i ||= _introspector(); | ||||||||||||||||||
|
Subroutine
explain: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/3
$i ||= _introspector()
MC/DC0%0/2
$i ||= _introspector()
$i
_introspector()
Truth table0%0/3
A ||= B
A= $iB= _introspector()
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | my $self = shift; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | my $storage = $self->result_source->storage; | |||||||||||||||||||
| 61 | 0 | $storage->ensure_connected; | |||||||||||||||||||
| 62 | 0 | my $dbh = $storage->dbh; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | $i->get($dbh, undef, 'splain')->($dbh, $self->as_query) | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 54 | 1; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | __END__ | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =pod | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head1 NAME | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | DBIx::Class::Helper::ResultSet::Explain - Get query plan for a ResultSet | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 SYNOPSIS | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | This module mostly makes sense to be used without setting as a component: | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | use Devel::Dwarn; | ||||||||||||||||||||
| 82 | Dwarn DBIx::Class::ResultSet::Explain::explain($rs) | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | But as usual, if you prefer to use it as a component here's how: | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | package MyApp::Schema::ResultSet::Foo; | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | __PACKAGE__->load_components(qw{Helper::ResultSet::Explain}); | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | ... | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1; | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | And then in a script or something: | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | use Devel::Dwarn; | ||||||||||||||||||||
| 97 | Dwarn $rs->explain; | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head1 DESCRIPTION | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | This is just a handy little tool that gives you the query plan for a given | ||||||||||||||||||||
| 102 | ResultSet. The output is in no way normalized, so just treat it as a debug tool | ||||||||||||||||||||
| 103 | or something. The only supported DB's are those listed below. Have fun! | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | See L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it | ||||||||||||||||||||
| 106 | to your entire schema. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head1 EXAMPLE OUTPUT FROM SUPPORTED DB's | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 SQlite | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | [ | ||||||||||||||||||||
| 113 | [ | ||||||||||||||||||||
| 114 | 0, | ||||||||||||||||||||
| 115 | "Init", | ||||||||||||||||||||
| 116 | 0, | ||||||||||||||||||||
| 117 | 11, | ||||||||||||||||||||
| 118 | 0, | ||||||||||||||||||||
| 119 | "", | ||||||||||||||||||||
| 120 | "00", | ||||||||||||||||||||
| 121 | undef | ||||||||||||||||||||
| 122 | ], | ||||||||||||||||||||
| 123 | [ | ||||||||||||||||||||
| 124 | 1, | ||||||||||||||||||||
| 125 | "OpenRead", | ||||||||||||||||||||
| 126 | 0, | ||||||||||||||||||||
| 127 | 3, | ||||||||||||||||||||
| 128 | 0, | ||||||||||||||||||||
| 129 | 4, | ||||||||||||||||||||
| 130 | "00", | ||||||||||||||||||||
| 131 | undef | ||||||||||||||||||||
| 132 | ], | ||||||||||||||||||||
| 133 | [ | ||||||||||||||||||||
| 134 | 2, | ||||||||||||||||||||
| 135 | "Rewind", | ||||||||||||||||||||
| 136 | 0, | ||||||||||||||||||||
| 137 | 9, | ||||||||||||||||||||
| 138 | 0, | ||||||||||||||||||||
| 139 | "", | ||||||||||||||||||||
| 140 | "00", | ||||||||||||||||||||
| 141 | undef | ||||||||||||||||||||
| 142 | ], | ||||||||||||||||||||
| 143 | [ | ||||||||||||||||||||
| 144 | 3, | ||||||||||||||||||||
| 145 | "Rowid", | ||||||||||||||||||||
| 146 | 0, | ||||||||||||||||||||
| 147 | 1, | ||||||||||||||||||||
| 148 | 0, | ||||||||||||||||||||
| 149 | "", | ||||||||||||||||||||
| 150 | "00", | ||||||||||||||||||||
| 151 | undef | ||||||||||||||||||||
| 152 | ], | ||||||||||||||||||||
| 153 | [ | ||||||||||||||||||||
| 154 | 4, | ||||||||||||||||||||
| 155 | "Column", | ||||||||||||||||||||
| 156 | 0, | ||||||||||||||||||||
| 157 | 1, | ||||||||||||||||||||
| 158 | 2, | ||||||||||||||||||||
| 159 | "", | ||||||||||||||||||||
| 160 | "00", | ||||||||||||||||||||
| 161 | undef | ||||||||||||||||||||
| 162 | ], | ||||||||||||||||||||
| 163 | [ | ||||||||||||||||||||
| 164 | 5, | ||||||||||||||||||||
| 165 | "Column", | ||||||||||||||||||||
| 166 | 0, | ||||||||||||||||||||
| 167 | 2, | ||||||||||||||||||||
| 168 | 3, | ||||||||||||||||||||
| 169 | "", | ||||||||||||||||||||
| 170 | "00", | ||||||||||||||||||||
| 171 | undef | ||||||||||||||||||||
| 172 | ], | ||||||||||||||||||||
| 173 | [ | ||||||||||||||||||||
| 174 | 6, | ||||||||||||||||||||
| 175 | "Column", | ||||||||||||||||||||
| 176 | 0, | ||||||||||||||||||||
| 177 | 3, | ||||||||||||||||||||
| 178 | 4, | ||||||||||||||||||||
| 179 | "", | ||||||||||||||||||||
| 180 | "00", | ||||||||||||||||||||
| 181 | undef | ||||||||||||||||||||
| 182 | ], | ||||||||||||||||||||
| 183 | [ | ||||||||||||||||||||
| 184 | 7, | ||||||||||||||||||||
| 185 | "ResultRow", | ||||||||||||||||||||
| 186 | 1, | ||||||||||||||||||||
| 187 | 4, | ||||||||||||||||||||
| 188 | 0, | ||||||||||||||||||||
| 189 | "", | ||||||||||||||||||||
| 190 | "00", | ||||||||||||||||||||
| 191 | undef | ||||||||||||||||||||
| 192 | ], | ||||||||||||||||||||
| 193 | [ | ||||||||||||||||||||
| 194 | 8, | ||||||||||||||||||||
| 195 | "Next", | ||||||||||||||||||||
| 196 | 0, | ||||||||||||||||||||
| 197 | 3, | ||||||||||||||||||||
| 198 | 0, | ||||||||||||||||||||
| 199 | "", | ||||||||||||||||||||
| 200 | "01", | ||||||||||||||||||||
| 201 | undef | ||||||||||||||||||||
| 202 | ], | ||||||||||||||||||||
| 203 | [ | ||||||||||||||||||||
| 204 | 9, | ||||||||||||||||||||
| 205 | "Close", | ||||||||||||||||||||
| 206 | 0, | ||||||||||||||||||||
| 207 | 0, | ||||||||||||||||||||
| 208 | 0, | ||||||||||||||||||||
| 209 | "", | ||||||||||||||||||||
| 210 | "00", | ||||||||||||||||||||
| 211 | undef | ||||||||||||||||||||
| 212 | ], | ||||||||||||||||||||
| 213 | [ | ||||||||||||||||||||
| 214 | 10, | ||||||||||||||||||||
| 215 | "Halt", | ||||||||||||||||||||
| 216 | 0, | ||||||||||||||||||||
| 217 | 0, | ||||||||||||||||||||
| 218 | 0, | ||||||||||||||||||||
| 219 | "", | ||||||||||||||||||||
| 220 | "00", | ||||||||||||||||||||
| 221 | undef | ||||||||||||||||||||
| 222 | ], | ||||||||||||||||||||
| 223 | [ | ||||||||||||||||||||
| 224 | 11, | ||||||||||||||||||||
| 225 | "Transaction", | ||||||||||||||||||||
| 226 | 0, | ||||||||||||||||||||
| 227 | 0, | ||||||||||||||||||||
| 228 | 16, | ||||||||||||||||||||
| 229 | 0, | ||||||||||||||||||||
| 230 | "01", | ||||||||||||||||||||
| 231 | undef | ||||||||||||||||||||
| 232 | ], | ||||||||||||||||||||
| 233 | [ | ||||||||||||||||||||
| 234 | 12, | ||||||||||||||||||||
| 235 | "TableLock", | ||||||||||||||||||||
| 236 | 0, | ||||||||||||||||||||
| 237 | 3, | ||||||||||||||||||||
| 238 | 0, | ||||||||||||||||||||
| 239 | "Gnarly", | ||||||||||||||||||||
| 240 | "00", | ||||||||||||||||||||
| 241 | undef | ||||||||||||||||||||
| 242 | ], | ||||||||||||||||||||
| 243 | [ | ||||||||||||||||||||
| 244 | 13, | ||||||||||||||||||||
| 245 | "Goto", | ||||||||||||||||||||
| 246 | 0, | ||||||||||||||||||||
| 247 | 1, | ||||||||||||||||||||
| 248 | 0, | ||||||||||||||||||||
| 249 | "", | ||||||||||||||||||||
| 250 | "00", | ||||||||||||||||||||
| 251 | undef | ||||||||||||||||||||
| 252 | ] | ||||||||||||||||||||
| 253 | ] | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =head2 Pg | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | [ | ||||||||||||||||||||
| 258 | [ | ||||||||||||||||||||
| 259 | "Seq Scan on \"Gnarly\" me (cost=0.00..16.20 rows=620 width=100) (actual time=0.001..0.001 rows=0 loops=1)" | ||||||||||||||||||||
| 260 | ], | ||||||||||||||||||||
| 261 | [ | ||||||||||||||||||||
| 262 | "Planning time: 0.179 ms" | ||||||||||||||||||||
| 263 | ], | ||||||||||||||||||||
| 264 | [ | ||||||||||||||||||||
| 265 | "Execution time: 0.014 ms" | ||||||||||||||||||||
| 266 | ] | ||||||||||||||||||||
| 267 | ] | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | =head2 mysql | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | [ | ||||||||||||||||||||
| 272 | [ | ||||||||||||||||||||
| 273 | 1, | ||||||||||||||||||||
| 274 | "SIMPLE", | ||||||||||||||||||||
| 275 | "me", | ||||||||||||||||||||
| 276 | "ALL", | ||||||||||||||||||||
| 277 | undef, | ||||||||||||||||||||
| 278 | undef, | ||||||||||||||||||||
| 279 | undef, | ||||||||||||||||||||
| 280 | undef, | ||||||||||||||||||||
| 281 | 1, | ||||||||||||||||||||
| 282 | "100.00", | ||||||||||||||||||||
| 283 | "" | ||||||||||||||||||||
| 284 | ] | ||||||||||||||||||||
| 285 | ] | ||||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | =head1 AUTHOR | ||||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt. | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 296 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | =cut | ||||||||||||||||||||