标签:sql优化
要提高SQL语句的执行效率,最常见的方法就是建立索引,以及尽量避免全表扫描。给大家整理一些常见的SQL优化技巧,避免全表扫描。一个简单的优化,也许能让你的SQL执行效率提高几倍,甚至几十倍。如:
select id from table where name is null
select id from table where name = 0
如:
select name from table where id <> 0
select name from table where id < 0 union all select name from table where id > 0
这里我们为什么没有使用 or 来链接 where 后的两个条件呢?这就是我们下面要说的第3个优化技巧。
select id from table where name = 'C++' or name = 'C#'
select id from table where name = 'C++' union all select id from table where name = 'C#'
select name from table where id in(1,2,3,4,5)
select name from table where id between 1 and 5
select id from table where name like '%jayzai%'
select id from table where name like '%jayzai'
select id from table where name like 'jayzai%'
select name from table where id/2 = 100
select name from table where id = 100*2
select id from table where substring(name,1,8) = 'jayzai'或
select id from table where datediff(day,datefield,'2014-07-17') >= 0
这两条语句中都对字段进行了函数处理,这样就是的查询分析器放弃了索引的使用。正确的写法是这样的:
select id from table where name like 'jayzai%'或
select id from table where datefield <= '2014-07-17'
select name from table where id in(select id from b)
如果我们将这条语句换成下面的写法:
这样,查询出来的结果一样,但是下面这条语句查询的速度要快的多。标签:sql优化
原文地址:http://blog.csdn.net/jayzai/article/details/45074529