OpenSql的优化原则
OpenSql的优化原则
Sql语句语法是否合理,将直接影响到程序的执行效率
1)要充分利用index,且index的字段尽量同时使用
2)尽量使用select f1 f2。。。(具体字段)来代替select *写法
3)使用up to n rows来实现对数据前n项的查询
4)充分利用系统提供的标准函数,如:max,min,avg,sum,count等
5)尽量不要在频率较高的循环语句中使用update \insert\delete\modify等操作
6)对于需要同时对多个表数据查询时,尽量使用Join语句,且注意应选择资料量最小的表作为基表,尽量避免3个以上Table 间的相关Join查询
7)在查询单条数据时尽量使用select single 语句,不要使用select.....endselect 语句
如: select id name discount from scustom
into ( xid,xname,xdiscount )
where custtype = 'B'
order by discount.
write:/ xid,xname,xdiscount.
enselect
改进后:
select single id name discount from scustom
into ( xid,xname,xdiscount )
where custtype = 'B'
order by discount.
write:/ xid,xname,xdiscount.
8)对于存在 OR条件判断的语句中,尽量使用IN来代替
select * from sflight
into xflight
where carrid = 'LH'
and ( connid = '0300' or connid = '0302' )
write:/ xflight-fldate.
endselect.
优化后:
select * from sflight
into xflight
where carrid = 'LH'
and connid in( '0300','0302' )
write:/ xflight-fldate.
endselect.
9) where语句中尽量避免使用模糊查询