- 論壇徽章:
- 0
|
Mysql管理必備工具M(jìn)aatkit詳解之四(mk-duplicate-key-checker)
2009年05月19日 作者: 大頭剛
mk-duplicate-key-checker - 檢查MYSQL重復(fù)索引,并給出刪除語(yǔ)句。安裝方法可以參考
這里
。
在oracle里,不允許在同一個(gè)列上創(chuàng)建多個(gè)索引,如果試圖創(chuàng)建就會(huì)報(bào)錯(cuò)如下:
CREATE INDEX sg1 ON uid_tmp(user_id);
CREATE INDEX sg1 ON uid_tmp(user_id)
*
ERROR AT line 1:
ORA-01408: such column list already indexed
而在mysql上,卻是允許的,不知道為何要這樣,總之是可以的。
mysql> create index sg1 on t1(id);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index sg2 on t1(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | |
| t1 | 1 | sg1 | 1 | id | A | 2 | NULL | NULL | | BTREE | |
| t1 | 1 | sg2 | 1 | id | A | 2 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.00 sec)
在ID這個(gè)列上,居然有3個(gè)索引,顯然嚴(yán)重浪費(fèi)資源。如果你的數(shù)據(jù)庫(kù)有很多表,一個(gè)一個(gè)表去查看顯然是一件很費(fèi)力,很機(jī)械的事情,那么你可以用mk-duplicate-key-checker工具來(lái)幫你,使用很簡(jiǎn)單:
[root@mysql2 u01]# mk-duplicate-key-checker -u sg -p
# ########################################################################
# test.t1
# ########################################################################
# sg1 is a duplicate of PRIMARY
# Column types:
# `id` int(11) NOT NULL
# To remove this duplicate key, execute:
ALTER TABLE `test`.`t1` DROP KEY `sg1`;
# sg2 is a duplicate of PRIMARY
# Column types:
# `id` int(11) NOT NULL
# To remove this duplicate key, execute:
ALTER TABLE `test`.`t1` DROP KEY `sg2`;
# ########################################################################
# Summary of keys
# ########################################################################
# Size Duplicate Keys 256
# Total Duplicate Keys 2
# Total Keys 293
給出了存在重復(fù)索引的表和列,以及刪除的語(yǔ)法。
OK,還有其他的一些參數(shù),可以看下幫助文件。
mk-duplicate-key-checker --help
mk-duplicate-key-checker examines MySQL tables for duplicate or redundant
indexes and foreign keys. Connection options are read from MySQL option files.
For more details, please use the --help option, or try 'perldoc
/usr/bin/mk-duplicate-key-checker' for complete documentation.
Usage: /usr/bin/mk-duplicate-key-checker
Options:
--all-structs Compare indexes with different structs (BTREE, HASH, etc)
--ask-pass Prompt for a password when connecting to MySQL
--charset -A Default character set
--[no]clustered PK columns appended to secondary key is duplicate (default
yes)
--config Read this comma-separated list of config files; if
specified, this must be the first option on the command
line
--databases -d Check only this comma-separated list of databases
--defaults-file -F Only read mysql options from the given file
--engines -e Do only tables whose storage engine is in this
comma-separated list
--help Show help and exit
--host -h Connect to host
--ignore-databases Ignore this comma-separated list of databases
--ignore-engines Ignore this comma-separated list of storage engines
--ignore-order Ignore index order so KEY(a,b) duplicates KEY(b,a)
--ignore-tables Ignore this comma-separated list of tables
--key-types Check for duplicate f=foreign keys, k=keys or fk=both
(default fk)
--password -p Password to use when connecting
--port -P Port number to use for connection
--set-vars Set these MySQL variables (default wait_timeout=10000)
--socket -S Socket file to use for connection
--[no]sql Print DROP KEY statement for each duplicate key (default
yes)
--[no]summary Print summary of indexes at end of output (default yes)
--tables -t Check only this comma-separated list of tables
--user -u User for login if not current user
--verbose -v Output all keys and/or foreign keys found, not just
redundant ones
--version Show version and exit
Options and values after processing arguments:
--all-structs FALSE
--ask-pass FALSE
--charset (No value)
--clustered TRUE
--config /etc/maatkit/maatkit.conf,/etc/maatkit/mk-duplicate-key-checker.conf,
/root/.maatkit.conf,/root/.mk-duplicate-key-checker.conf
--databases (No value)
--defaults-file (No value)
--engines (No value)
--help TRUE
--host (No value)
--ignore-databases
--ignore-engines
--ignore-order FALSE
--ignore-tables
--key-types fk
--password (No value)
--port (No value)
--set-vars wait_timeout=10000
--socket (No value)
--sql TRUE
--summary TRUE
--tables (No value)
--user (No value)
--verbose FALSE
--version FALSE
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u3/111930/showart_2184834.html |
|