SQL索引(索引优化)

SQL索引(索引优化)SQL索引 若想利用索引达到预想的提高查询速度的效果,我们在添加索引时,必须遵循以下原则 #1.最左前缀匹配原则,非常重要的原则, create index ix_name_email on s1(…

SQL索引(索引优化)

#1.最左前缀匹配原则,非常重要的原则,
create index ix_name_email on s1(name,email,)
– 最左前缀匹配:必须按照从左到右的顺序匹配
select * from s1 where name=”egon”; #可以
select * from s1 where name=”egon” and email=”asdf”; #可以
select * from s1 where email=”alex@oldboy.com”; #不可以
mysql会一直向右匹配直到遇到范围查询(>、<、between、like)就停止匹配,
比如a = 1 and b = 2 and c > 3 and d = 4 如果建立(a,b,c,d)顺序的索引,
d是用不到索引的,如果建立(a,b,d,c)的索引则都可以用到,a,b,d的顺序可以任意调整。

#2.=和in可以乱序,比如a = 1 and b = 2 and c = 3 建立(a,b,c)索引可以任意顺序,mysql的查询优化器
会帮你优化成索引可以识别的形式

#3.尽量选择区分度高的列作为索引,区分度的公式是count(distinct col)/count(*),
表示字段不重复的比例,比例越大我们扫描的记录数越少,唯一键的区分度是1,而一些状态、
性别字段可能在大数据面前区分度就是0,那可能有人会问,这个比例有什么经验值吗?使用场景不同,
这个值也很难确定,一般需要join的字段我们都要求是0.1以上,即平均1条扫描10条记录

#4.索引列不能参与计算,保持列“干净”,比如from_unixtime(create_time) = ’2014-05-29’
就不能使用到索引,原因很简单,b+树中存的都是数据表中的字段值,
但进行检索时,需要把所有元素都应用函数才能比较,显然成本太大。
所以语句应该写成create_time = unix_timestamp(’2014-05-29’);

 

最左前缀示范

mysql> select * from s1 where id>3 and name=”egon” and email=”alex333@oldboy.com” and gender=”male”;
Empty set (0.39 sec)

mysql> create index idx on s1(id,name,email,gender); #未遵循最左前缀
Query OK, 0 rows affected (15.27 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from s1 where id>3 and name=”egon” and email=”alex333@oldboy.com” and gender=”male”;
Empty set (0.43 sec)

mysql> drop index idx on s1;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> create index idx on s1(name,email,gender,id); #遵循最左前缀
Query OK, 0 rows affected (15.97 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from s1 where id>3 and name=”egon” and email=”alex333@oldboy.com” and gender=”male”;
Empty set (0.03 sec)

 

索引无法命中的情况需要注意:

– like “%xx”
select * from tb1 where email like “%cn”;

– 使用函数
select * from tb1 where reverse(email) = “wupeiqi”;

– or
select * from tb1 where nid = 1 or name = “seven@live.com”;

特别的:当or条件中有未建立索引的列才失效,以下会走索引
select * from tb1 where nid = 1 or name = “seven”;
select * from tb1 where nid = 1 or name = “seven@live.com” and email = “alex”

– 类型不一致
如果列是字符串类型,传入条件是必须用引号引起来,不然…
select * from tb1 where email = 999;

普通索引的不等于不会走索引
– !=
select * from tb1 where email != “alex”

特别的:如果是主键,则还是会走索引
select * from tb1 where nid != 123
– >
select * from tb1 where email > “alex”

特别的:如果是主键或索引是整数类型,则还是会走索引
select * from tb1 where nid > 123
select * from tb1 where num > 123

#排序条件为索引,则select字段必须也是索引字段,否则无法命中
– order by
select name from s1 order by email desc;
当根据索引排序时候,select查询的字段如果不是索引,则不走索引
select email from s1 order by email desc;
特别的:如果对主键排序,则还是走索引:
select * from tb1 order by nid desc;

– 组合索引最左前缀
如果组合索引为:(name,email)
name and email — 使用索引
name — 使用索引
email — 不使用索引

– count(1)或count(列)代替count(*)在mysql中没有差别了

– create index xxxx on tb(title(19)) #text类型,必须制定长度

 

 

– 避免使用select *
– count(1)或count(列) 代替 count(*)
– 创建表时尽量时 char 代替 varchar
– 表的字段顺序固定长度的字段优先
– 组合索引代替多个单列索引(经常使用多个条件查询时)
– 尽量使用短索引
– 使用连接(JOIN)来代替子查询(Sub-Queries)
– 连表时注意条件类型需一致
– 索引散列值(重复少)不适合建索引,例:性别不适合

 

慢查询优化的基本步骤

0.先运行看看是否真的很慢,注意设置SQL_NO_CACHE
1.where条件单表查,锁定最小返回记录表。这句话的意思是把查询语句的where都应用到表中返回的记录数最小的表开始查起,单表每个字段分别查询,看哪个字段的区分度最高
2.explain查看执行计划,是否与1预期一致(从锁定记录较少的表开始查询)
3.order by limit 形式的sql语句让排序的表优先查
4.了解业务方使用场景
5.加索引时参照建索引的几大原则
6.观察结果,不符合预期继续从0分

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/9114.html

(0)
上一篇 2023-02-15
下一篇 2023-02-15

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注