浅谈MySQL中优化sql语句查询常用的30种方法

论坛 期权论坛     
选择匿名的用户   2021-6-1 19:05   229   0
<span style="font-family:tahoma,arial,宋体; font-size:14px">1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">2.应尽量避免在 where 子句中使用!&#61;或&lt;&gt;操作符,否则将引擎放弃使用索引而进行全表扫描。 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">3.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num is null </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">可以在num上设置默认值0,确保表中num列没有null值,然后这样查询: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num&#61;0 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">4.应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num&#61;10 or num&#61;20 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">可以这样查询: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num&#61;10 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">union all </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num&#61;20 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">5.下面的查询也将导致全表扫描: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where name like &#39;%abc%&#39; </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">若要提高效率,可以考虑全文检索。 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">6.in 和 not in 也要慎用,否则会导致全表扫描,如: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num in(1,2,3) </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">对于连续的数值,能用 between 就不要用 in 了: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num between 1 and 3 </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">7.如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t where num&#61;&#64;num </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">可以改为强制查询使用索引: </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">select id from t with(index(索引名)) where num&#61;&#64;num </span>
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<br style="font-family:tahoma,arial,宋体; font-size:14px">
<span style="font-family:tahoma,arial,宋体; font-size:14px">8.应尽量避免在 where 子句中对
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP