建表时声明
create table testindex (
id int(3) not null,age int(3) not null,dept char(12),name char(12),primary key(id),key index_name(name)
);
desc testindex;
联合索引
create index index_name_test on testindex(age,dept);
create index index_name_tests on testindex(name(1),dept(1));
show create table testindex\G;
show columns from testindex\G;
尽量在唯一值多的大表上建立索引
对三个列建立,有个前缀特性,如:a,b,c a,ab,abc可以走索引,但是bc之类(开头不为a)的不走索引
唯一索引(可以为空,主键索引不能为空)
create unique index uni_ind_name_test on testindex(dept)
创建条件:在大表上建立唯一值多的索引
select count(distinct user)from MysqL.user;
删除索引
drop index index_name_test from MysqL.user;
--排查慢语句
show full percesslist;
long_query_time = 1
log-slow-queries = /data/3306/slow.log
log_queries_not_using_indexes
分割日志发送至管理员
加explain查看语句的具体执行方式
定位在哪些列上建立索引,查看条件字段列的唯一值数量select count(distinct column_name) from table_name;
原文地址:https://www.jb51.cc/centos/381068.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。