Restore original NibbleIterator and implement simpler solution: only use MySQL's chosen index if --where.

This commit is contained in:
Daniel Nichter
2012-05-08 12:43:47 -06:00
parent e2073065b1
commit 30b6b88766
2 changed files with 61 additions and 141 deletions

View File

@@ -3580,9 +3580,6 @@ sub new {
else {
my $index = $nibble_params->{index}; # brevity
my $index_cols = $tbl->{tbl_struct}->{keys}->{$index}->{cols};
my $order_by = join(', ', map {$q->quote($_)} @{$index_cols});
my $limit = $chunk_size - 1;
PTDEBUG && _d('Initial chunk size (LIMIT):', $limit);
my $asc = $args{TableNibbler}->generate_asc_stmt(
%args,
@@ -3593,52 +3590,18 @@ sub new {
);
PTDEBUG && _d('Ascend params:', Dumper($asc));
my $from = "$tbl->{name} FORCE INDEX(`$index`)";
my $order_by = join(', ', map {$q->quote($_)} @{$index_cols});
my $first_lb_sql
= "SELECT /*!40001 SQL_NO_CACHE */ "
. join(', ', map { $q->quote($_) } @{$asc->{scols}})
. " FROM $tbl->{name}"
. " FROM $from"
. ($where ? " WHERE $where" : '')
. " ORDER BY $order_by"
. " LIMIT 1"
. " /*first lower boundary*/";
PTDEBUG && _d($first_lb_sql);
my $first_lower = $cxn->dbh()->selectrow_arrayref($first_lb_sql);
PTDEBUG && _d('First lower boundary:', Dumper($first_lower));
if ( !$args{chunk_index} || (lc($args{chunk_index}) ne lc($index)) ) {
my $sql
= "EXPLAIN SELECT /*!40001 SQL_NO_CACHE */ "
. join(', ', map { $q->quote($_) } @{$asc->{scols}})
. " FROM $tbl->{name}"
. " WHERE " . $asc->{boundaries}->{'>='}
. ($where ? " AND ($where)" : '')
. " ORDER BY $order_by"
. " LIMIT ?, 2"
. " /*get MySQL index*/";
my $sth = $cxn->dbh()->prepare($sql);
my $mysql_index = _get_mysql_index(
Cxn => $cxn,
sth => $sth,
params => [@$first_lower, $limit],
);
PTDEBUG && _d('MySQL index:', $mysql_index);
if ( lc($index) ne lc($mysql_index) ) {
my $chosen_index_struct = $tbl->{tbl_struct}->{keys}->{$index};
my $mysql_index_struct = $tbl->{tbl_struct}->{keys}->{$mysql_index};
warn "The best index for chunking $tbl->{name} is $index ("
. ($chosen_index_struct->{is_unique} ? "unique" : "not unique")
. ", covers " . scalar @{$chosen_index_struct->{cols}}
. " columns), but index $mysql_index ("
. ($mysql_index_struct->{is_unique} ? "unique" : "not unique")
. ", covers " . scalar @{$mysql_index_struct->{cols}}
. " columns) that MySQL chose will be used instead.\n";
$index = $mysql_index;
}
}
my $from = "$tbl->{name} FORCE INDEX(`$index`)";
PTDEBUG && _d('First lower boundary statement:', $first_lb_sql);
my $resume_lb_sql;
if ( $args{resume} ) {
@@ -3700,11 +3663,14 @@ sub new {
. " /*explain $comments{nibble}*/";
PTDEBUG && _d('Explain nibble statement:', $explain_nibble_sql);
my $limit = $chunk_size - 1;
PTDEBUG && _d('Initial chunk size (LIMIT):', $limit);
$self = {
%args,
index => $index,
limit => $limit,
first_lower => $first_lower,
first_lb_sql => $first_lb_sql,
last_ub_sql => $last_ub_sql,
ub_sql => $ub_sql,
nibble_sql => $nibble_sql,
@@ -3892,12 +3858,18 @@ sub can_nibble {
}
my ($cxn, $tbl, $chunk_size, $o) = @args{@required_args};
my $row_est = get_row_estimate(
my $where = $o->has('where') ? $o->get('where') : '';
my ($row_est, $mysql_index) = get_row_estimate(
Cxn => $cxn,
tbl => $tbl,
where => $o->has('where') ? $o->get('where') : '',
where => $where,
);
if ( !$where ) {
$mysql_index = undef;
}
my $one_nibble = !defined $args{one_nibble} || $args{one_nibble}
? $row_est <= $chunk_size * $o->get('chunk-size-limit')
: 0;
@@ -3910,7 +3882,7 @@ sub can_nibble {
$one_nibble = 1;
}
my $index = _find_best_index(%args);
my $index = _find_best_index(%args, mysql_index => $mysql_index);
if ( !$index && !$one_nibble ) {
die "There is no good index and the table is oversized.";
}
@@ -4014,18 +3986,6 @@ sub _get_index_cardinality {
return $cardinality;
}
sub _get_mysql_index {
my (%args) = @_;
my @required_args = qw(Cxn sth params);
my ($cxn, $sth, $params) = @args{@required_args};
PTDEBUG && _d($sth->{Statement}, 'params:', @$params);
$sth->execute(@$params);
my $row = $sth->fetchrow_hashref();
$sth->finish();
PTDEBUG && _d(Dumper($row));
return $row->{key};
}
sub get_row_estimate {
my (%args) = @_;
my @required_args = qw(Cxn tbl);
@@ -4035,11 +3995,11 @@ sub get_row_estimate {
my ($cxn, $tbl) = @args{@required_args};
my $sql = "EXPLAIN SELECT * FROM $tbl->{name} "
. "WHERE " . ($args{where} || '1=1 /*get row estimate*/');
. "WHERE " . ($args{where} || '1=1');
PTDEBUG && _d($sql);
my $expl = $cxn->dbh()->selectrow_hashref($sql);
PTDEBUG && _d(Dumper($expl));
return $expl->{rows} || 0;
return ($expl->{rows} || 0), $expl->{key};
}
sub _prepare_sths {
@@ -4071,6 +4031,9 @@ sub _get_bounds {
my $dbh = $self->{Cxn}->dbh();
$self->{first_lower} = $dbh->selectrow_arrayref($self->{first_lb_sql});
PTDEBUG && _d('First lower boundary:', Dumper($self->{first_lower}));
if ( my $nibble = $self->{resume} ) {
if ( defined $nibble->{lower_boundary}
&& defined $nibble->{upper_boundary} ) {
@@ -6430,7 +6393,7 @@ sub main {
my $chunk_size_limit = $o->get('chunk-size-limit');
my @too_large;
foreach my $slave ( @$slaves ) {
my $n_rows = NibbleIterator::get_row_estimate(
my ($n_rows) = NibbleIterator::get_row_estimate(
Cxn => $slave,
tbl => $tbl,
where => $o->get('where'),