Merge pull request #996 from utdrmac/pt-dup-index-invis

Add invisible option to pt-duplicate-key-checker
This commit is contained in:
Sveta Smirnova
2025-08-26 19:42:23 +03:00
committed by GitHub
4 changed files with 57 additions and 7 deletions

View File

@@ -5301,12 +5301,19 @@ sub print_duplicate_key {
if ( $o->get('sql') ) {
if ( $dupe->{dupe_type} ne 'clustered' ) {
print "# To remove this duplicate "
. ($args{is_fk} ? 'foreign key' : 'index')
. ", execute:\n"
. 'ALTER TABLE ' . $q->quote($db, $tbl)
. ($args{is_fk} ? ' DROP FOREIGN KEY ' : ' DROP INDEX ')
. "`$dupe->{key}`;\n";
if ( $o->get('invisible') && !$args{is_fk} ) {
print "# To make this index invisible, execute:\n"
. 'ALTER TABLE ' . $q->quote($db, $tbl)
. " ALTER INDEX `$dupe->{key}` INVISIBLE;\n";
}
else {
print "# To remove this duplicate "
. ($args{is_fk} ? 'foreign key' : 'index')
. ", execute:\n"
. 'ALTER TABLE ' . $q->quote($db, $tbl)
. ($args{is_fk} ? ' DROP FOREIGN KEY ' : ' DROP INDEX ')
. "`$dupe->{key}`;\n";
}
}
else {
# Suggest shortening clustered dupes instead of
@@ -5566,6 +5573,19 @@ type: Hash
Ignore this comma-separated list of tables. Table names may be qualified with
the database name.
=item --invisible
The output SQL will make the index INVISIBLE, instead of dropping it.
An invisible index cannot be considered by the query optimizer, but is still
maintained when writes happen to the table. To make the index usable by the
optimizer, execute ALTER INDEX .. VISIBLE
While dropping an index is a fast, background operation, adding indexes is
a slow, CPU, and disk IO intensive operation. If you discover that an index
was made invisible incorrectly, it becomes a NOOP (no-operation) to make the index visible again,
compared to completely re-adding the index.
=item --key-types
type: string; default: fk

View File

@@ -8879,7 +8879,7 @@ sub main {
# it's a hack, like ignoring a problem instead of fixing it somehow. We
# should take a look at the things that get printed in a "normal"
# non-quiet run, and "if !quiet" them, and then do some kind of Logger.pm
# or Messager.pm module for a future release.
# or Message.pm module for a future release.
close STDOUT;
open STDOUT, '>', '/dev/null'
or warn "Cannot reopen STDOUT to /dev/null: $OS_ERROR";

View File

@@ -80,6 +80,15 @@ ok(
'--nosql'
);
ok(
no_diff(
sub { pt_duplicate_key_checker::main(@args, qw(-d test --invisible)) },
"$sample/basic_invisible.txt",
transform_sample => $transform_int
),
'--invisible'
);
ok(
no_diff(
sub { pt_duplicate_key_checker::main(@args, qw(-d test --nosummary)) },

View File

@@ -0,0 +1,21 @@
# ########################################################################
# test.dupe_key
# ########################################################################
# a is a left-prefix of a_2
# Key definitions:
# KEY `a` (`a`),
# KEY `a_2` (`a`,`b`)
# Column types:
# `a` int(11) default null
# `b` int(11) default null
# To make this index invisible, execute:
ALTER TABLE `test`.`dupe_key` ALTER INDEX `a` INVISIBLE;
# ########################################################################
# Summary of indexes
# ########################################################################
# Size Duplicate Indexes 0
# Total Duplicate Indexes 1
# Total Indexes 2