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
|
package DBIx::Class::Helper::ResultSet::Explain;
$DBIx::Class::Helper::ResultSet::Explain::VERSION = '2.023007';
use strict;
use warnings;
use DBIx::Introspector;
# ABSTRACT: Get query plan for a ResultSet
sub _introspector {
my $d = DBIx::Introspector->new(drivers => '2013-12.01');
$d->decorate_driver_connected(MSSQL => splain => 'GETUTCDATE()');
$d->decorate_driver_connected(
SQLite => splain => sub {
sub {
my ($dbh, $query) = @_;
my ($sql, @bind) = @{$$query};
$sql =~ s/\s*\((.*)\)\s*/$1/;
shift->selectall_arrayref("EXPLAIN $sql", undef, @bind)
},
},
);
$d->decorate_driver_connected(
Pg => splain => sub {
sub {
my ($dbh, $query) = @_;
my ($sql, @bind) = @{$$query};
shift->selectall_arrayref("EXPLAIN ANALYZE $sql", undef, @bind)
},
},
);
$d->decorate_driver_connected(
mysql => splain => sub {
sub {
my ($dbh, $query) = @_;
my ($sql, @bind) = @{$$query};
shift->selectall_arrayref("EXPLAIN EXTENDED $sql", undef, @bind)
},
},
);
return $d;
}
use namespace::clean;
my $i;
sub explain {
$i ||= _introspector();
my $self = shift;
my $storage = $self->result_source->storage;
$storage->ensure_connected;
my $dbh = $storage->dbh;
$i->get($dbh, undef, 'splain')->($dbh, $self->as_query)
}
1;
__END__
=pod
=head1 NAME
DBIx::Class::Helper::ResultSet::Explain - Get query plan for a ResultSet
=head1 SYNOPSIS
This module mostly makes sense to be used without setting as a component:
use Devel::Dwarn;
Dwarn DBIx::Class::ResultSet::Explain::explain($rs)
But as usual, if you prefer to use it as a component here's how:
package MyApp::Schema::ResultSet::Foo;
__PACKAGE__->load_components(qw{Helper::ResultSet::Explain});
...
1;
And then in a script or something:
use Devel::Dwarn;
Dwarn $rs->explain;
=head1 DESCRIPTION
This is just a handy little tool that gives you the query plan for a given
ResultSet. The output is in no way normalized, so just treat it as a debug tool
or something. The only supported DB's are those listed below. Have fun!
See L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it
to your entire schema.
=head1 EXAMPLE OUTPUT FROM SUPPORTED DB's
=head2 SQlite
[
[
0,
"Init",
0,
11,
0,
"",
"00",
undef
],
[
1,
"OpenRead",
0,
3,
0,
4,
"00",
undef
],
[
2,
"Rewind",
0,
9,
0,
"",
"00",
undef
],
[
3,
"Rowid",
0,
1,
0,
"",
"00",
undef
],
[
4,
"Column",
0,
1,
2,
"",
"00",
undef
],
[
5,
"Column",
0,
2,
3,
"",
"00",
undef
],
[
6,
"Column",
0,
3,
4,
"",
"00",
undef
],
[
7,
"ResultRow",
1,
4,
0,
"",
"00",
undef
],
[
8,
"Next",
0,
3,
0,
"",
"01",
undef
],
[
9,
"Close",
0,
0,
0,
"",
"00",
undef
],
[
10,
"Halt",
0,
0,
0,
"",
"00",
undef
],
[
11,
"Transaction",
0,
0,
15,
0,
"01",
undef
],
[
12,
"TableLock",
0,
3,
0,
"Gnarly",
"00",
undef
],
[
13,
"Goto",
0,
1,
0,
"",
"00",
undef
]
]
=head2 Pg
[
[
"Seq Scan on \"Gnarly\" me (cost=0.00..16.20 rows=620 width=100) (actual time=0.001..0.001 rows=0 loops=1)"
],
[
"Total runtime: 0.020 ms"
]
]
=head2 mysql
[
[
1,
"SIMPLE",
"me",
"ALL",
undef,
undef,
undef,
undef,
1,
"100.00",
""
]
]
=head1 AUTHOR
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|