-- list all non-unique indexes
SELECT table_name AS `Table`,
index_name AS `Index`,
GROUP_CONCAT(column_name ORDER BY seq_in_index) AS `Columns`
FROM information_schema.statistics
WHERE NON_UNIQUE = 1 AND table_schema = 'mydatabase' AND table_name = 'mytable'
GROUP BY 1,2;
-- drop all non-unique indexes
SET SESSION group_concat_max_len=10240;
SELECT CONCAT('ALTER TABLE ', `Table`, ' DROP INDEX ', GROUP_CONCAT(`Index` SEPARATOR ', DROP INDEX '),';' )
FROM (
SELECT table_name AS `Table`,
index_name AS `Index`
FROM information_schema.statistics
WHERE NON_UNIQUE = 1 AND table_schema = 'mydatabase' AND table_name = 'mytable'
GROUP BY `Table`, `Index`) AS tmp
GROUP BY `Table`;
-- add all non-unique indexes , WITHOUT index length spec
SET SESSION group_concat_max_len=10240;
SELECT CONCAT('ALTER TABLE ', `Table`, ' ADD INDEX ', GROUP_CONCAT(CONCAT(`Index`, '(', `Columns`, ')') SEPARATOR ',\n ADD INDEX ') )
FROM (
SELECT table_name AS `Table`,
index_name AS `Index`,
GROUP_CONCAT(column_name ORDER BY seq_in_index) AS `Columns`
FROM information_schema.statistics
WHERE NON_UNIQUE = 1 AND table_schema = 'mydatabase' AND table_name = 'mytable'
GROUP BY `Table`, `Index`) AS tmp
GROUP BY `Table`;
indexes = ActiveRecord::Base.connection.execute("SHOW INDEX FROM tablename")
indexes.each do |index|
ActiveRecord::Base.connection.execute("ALTER TABLE tablename DROP INDEX #{index[2]};")
end
4条答案
按热度按时间6bc51xsx1#
简单脚本:
vmdwslir2#
如果你有phpmyadmin或任何类似的工具,你可以很容易地做到这一点的图形。
或者对每个索引执行如下操作
您可以使用以下命令获取索引
cvxl0en23#
在Ruby on Rails中,我会这样做:
wlsrxk514#
不,这里没有一个命令。你可以写一个脚本来显示所有的数据库,显示数据库中的所有表,显示这些表中的所有索引,并删除它们。但是如果你不接受一些答案,我就不为你写了。你也可以使用phpmyadmin或其他图形工具来为每个表选择这个整洁的“全选”框。