写SQL时尽量不要对字段进行运算操作

有时我们在做项目时,需要在SQL文里面对某个字段经过运算后再与一个常量比较:那么这将会对运行性能产生很大的影响:
请看下面SQL的执行效率:
select * from iroomtypeprice where amount/30< 100011
当改写成下面的SQL语句时效率明显提高了:
select * from iroomtypeprice where amount< 1000*30(<1
语句1因为要对没没条记录的字段amount做一个/的运算,所以必须进行全表扫描,表的合适索引不会起作用;而语句二就会用到表上的合适索引。因此效率明显提高。
 
另外,在where后面尽量少用substring等函数,这样也可以提高执行效率,如:
select * from iroomtypeprice where substring(card_no,1,4)='5378'(13)改成
select * from iroomtypeprice where card_no like '5378%'(<1)
 
select * from iroomtypeprice wher econvert(char(10),date,112)='19991201'10
select * from iroomtypeprice where date= '1999/12/01'< 1
 
posted @ 2008-02-16 23:29  ZYB  阅读(977)  评论(1编辑  收藏  举报