日期:2014-05-17  浏览次数:21165 次

如何提高以下SQL语句的执行效率
数据有2张表
1:z_user
2:z_businesslog
z_user   表中有locationcode作为和z_businesslog中的locationcode连接使用,并且z_user中有字段atime(开户时间),要求计算atime到当前日期的天数,并且统计z_user中的每条数据在z_businesslog中的统计数量,我写了如下SQL语句,但是执行效率相当低,大概需要33秒左右,其中z_user有数据300条,z_businesslog中有数据3W条多,请大家帮忙分析一下:

select   zu.locationcode,zu.field,zu.unit,zu.atime   as   begindate  
,abs(trunc(to_date(zu.atime, 'yyyy-mm-dd ')-sysdate))   as   cc   ,(select   count(*)   from   z_businesslog   zb   where   zb.locationcode=zu.locationcode)   as   dd
from   z_user   zu   where   zu.role=1  
order   by   zu.locationcode

------解决方案--------------------
这样的效率不知道会不会高点
把select count(*) from z_businesslog zb where zb.locationcode=zu.locationcode) as dd
不在select 后面写换个地方写
select zu.locationcode,zu.field,zu.unit,zu.atime as begindate,
abs(trunc(to_date(zu.atime, 'yyyy-mm-dd ')-sysdate)) as cc ,
count(*) from (select * from z_user where zu.role=1 )zu,z_businesslog zb
where zu.locationcode=zb.locationcode
group by zu.locationcode,zu.field,zu.unit,zu.atime,abs(trunc(to_date(zu.atime, 'yyyy-mm-dd ')-sysdate))