零度小编今天给大家示例一下:
常见的SQL语句优化实战案例!
1、SQL语句中,where和having各有用途(查询成果回来前的用途各不相同)。
“Where” 是一个束缚声明,运用Where来束缚来之数据库的数据,Where是在成果回来之前起作用的,且Where中不能运用聚合函数。
“Having”是一个过滤声明,是在查询回来成果集以后对查询成果进行的过滤操作,在Having中能够运用聚合函数。
2、少用 *
许多朋友很喜欢用*,比方:select * from tb_user;
一般来说,并不需求一切的column值,只需求一些,有的只是需求1个2个,eg: username,age,address
拿5W的数据量,10个属性来测验:
运用select * from tb_user;平均需求20秒,
运用select username,agefrom tb_user;平均需求12秒
一般在开发的过程中会这样做
注意:标签内 refid的值和 标签内的id称号一致
3.多Exists,少in
Exists只检查存在性,性能比in强许多,有些朋友不会用Exists,就举个例子
例,想要得到有电话号码的人的基本信息,table2有冗余信息
select * from table1;–(id,name,age)
select * from table2;–(id,phone)
in:
select * from table1 t1 where t1.id in (select t2.id from table2 t2 where t1.id=t2.id);
Exists:
select * from table1 t1 where Exists (select 1 from table2 t2 where t1.id=t2.id);
4、SQL语句的编写要简洁,能用一句千万不要用两句。
转载请注明: 零度资源网
本文原网址:https://0dzyw.com/1988.html/