删除索引:
DROP INDEX idx_ruleId_cityId ON detail_price;
DROP INDEX 索引名称 ON 表名;
创建联合唯一索引:
ALTER TABLE detail_price ADD unique INDEX idx_ruleId_cityId(rule_id,city_id);
ALTER TABLE 表名 ADD unique INDEX 索引名称(字段1,字段2);
(如果表中已经有重复的记录,这时候添加唯一索引,执行上面的操作时,数据库会返回添加失败信息,因为已经有重复的记录了,建立索引失败。)
可以通过以下sql查询重复记录,删除多余的记录,只保留一条:
select rule_id,city_id,count(*) as count from detail_price group by rule_id,city_id having count>1;
删除表中多余的重复记录,只保留rowid最小的记录:
delete from detail_price a where (a.rule_id,a.city_id) in (select rule_id,city_id from detail_price group by rule_id,city_id having count>1)
and rowid not in (select min(rowid)from detail_price group by rule_id,city_id having count>1 )
————————————————
MySQL中通过SQL方式,获取索引信息的3种方法。
方法一:查看特定表的索引,请使用SHOW INDEX:
SHOW INDEX FROM t_user; # t_user 表名
方法二:查询某个数据库(table_schema)的全部表索引,可以从INFORMATION_SCHEMA架构中的STATISTICS表中获取索引信息
SELECT DISTINCT
TABLE_NAME,
INDEX_NAME
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'dbtest'; # dbtest 数据库名称
方法三:根据innodb_index_stats获取索引信息
SELECT * FROM mysql.`innodb_index_stats` a WHERE a.`database_name` = '数据库名' and a.table_name like '%表名%';