Home >Database >Mysql Tutorial >两个 mysql SQL优化技艺

两个 mysql SQL优化技艺

WBOY
WBOYOriginal
2016-06-07 16:24:03943browse

两个 mysql SQL优化技巧 1, limit offset 优化 ? ? ? ?mysql SELECT * FROM `bigtable` order by value limit 50,5; ? ? ? ?优化后: ? ? ? ?mysql EXPLAIN SELECT * FROM `bigtable` ? ? ? ? ? ? ? ? INNER JOIN( ? ? ? ? ? ??select id from `bigtable` or

两个 mysql SQL优化技巧

1, limit offset 优化

? ? ? ?mysql >SELECT * FROM `bigtable` order by value limit 50,5;

? ? ? ?优化后:

? ? ? ?mysql >EXPLAIN SELECT * FROM `bigtable`

? ? ? ? ? ? ? ? INNER JOIN(

? ? ? ? ? ??select id from `bigtable` order by value limit 50,5

? ? ? ? ? ? ? ? ?) as lim USING(id);

? ? ? ?说明:通过联结先取得符合条件的主键ID,然后再在原表上查找剩余的行,这样减少了读取无用行中的数据。

? ? ? ?如果事先将limit 分页计算出索引列的准确位置,那么可以使用索引范围扫描,写成这样:

? ? ? mysql >SELECT * FROM `bigtable` where position between 5 and 10 order by position;

? ? ? position 是bigtable 表的一列。

2, 始终使用union all 代替union ,除非需要服务器消除重复的行。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn