MySQL性能优化之道

Posted 沐风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL性能优化之道相关的知识,希望对你有一定的参考价值。

1.in和not in子查询优化

not in 是不能命中索引的,所以以下子查询性能很低。

如果是确定且有限的集合时,可以使用。如 IN (0,1,2)。

用 exists或 notexists代替

select *  from test1  where EXISTS (select * from test2  where id2 = id1 )
select *  FROM test1  where NOT EXISTS (select * from test2  where id2 = id1 )

用join代替

 select id1 from test1 INNER JOIN test2 ON id2 = id1 
 select id1 from test1 LEFT JOIN test2 ON id2 = id1  where id2 IS NULL

 

以上是关于MySQL性能优化之道的主要内容,如果未能解决你的问题,请参考以下文章