首页 新闻 搜索 专区 学院

提高sql查询速率

0
悬赏园豆:5 [已解决问题] 解决于 2012-08-22 10:29

现在一个sql查询出来得要31秒,求问怎么提高效率,语句如下:

select e.emp_no,e.emp_cname,e.emp_ename,e.dept_code,b.[name], ISNULL(convert(nvarchar(20),EventsDateTime,120),'') cardtime
 from  [benq].Sky2004.dbo.AccessEvents a,employee e,[benq].Sky2004.dbo.VIEW_DeviceReader b where
a.deviceid=b.recnum and b.[name] like '%考勤%'
and a.personno=e.emp_no
and a.personno in(select c.emp_no from employee c where c.dept_code='2000' and c.active='Y')
AND (convert(nvarchar(20),EventsDateTime,112) BETWEEN '2012-08-01' AND '2012-08-21')
and e.active='Y'
order by  convert(nvarchar(10),EventsDateTime,112) desc,e.emp_no,EventsDateTime

暗夜的萤火虫的主页 暗夜的萤火虫 | 初学一级 | 园豆:8
提问于:2012-08-21 08:21
< >
分享
最佳答案
0

 

1.日期的比较使用系统datediff()函数
(convert(nvarchar(20),EventsDateTime,112) BETWEEN '2012-08-01' AND '2012-08-21') 修改为

datediff(dd,EventsDateTime,'2012-08-01')<=0 and datediff(dd,EventsDateTime,'2012-08-21')>=0 

2.对字段 EventsDateTime ,deviceid,recnum ,personno,emp_no ,active 增加索引

3.order by convert(nvarchar(10),EventsDateTime,112) desc中如果EventsDateTime是日期类型数据,直接使用

order by EventsDateTime desc

4.直接使用JOIN 

5.VIEW_DeviceReader 应该是一个视图,本查询仅仅用到了b.[name],b.recnum 最好直接从原始表获取数据,并且对字段

recnum 增加索引

语句参考:

 

SELECT  e.emp_no, e.emp_cname, e.emp_ename, e.dept_code, b.[name], ISNULL(CONVERT(NVARCHAR(20), a.EventsDateTime, 120), '') cardtime
FROM    [benq].Sky2004.dbo.AccessEvents a
        INNER JOIN ( SELECT emp_no, active, emp_ename, dept_code, emp_cname FROM employee e1 WHERE e1.active = 'Y'
                   ) e ON a.personno = e.emp_no
        INNER JOIN ( SELECT recnum, [name] FROM [benq].Sky2004.dbo.VIEW_DeviceReader b1 WHERE b1.[name] LIKE '%考勤%'
                   ) b ON a.deviceid = b.recnum
WHERE   a.personno IN ( SELECT  c.emp_no
                        FROM    employee c
                        WHERE   c.dept_code = '2000'
                                AND c.active = 'Y' )
        AND DATEDIFF(dd, a.EventsDateTime, '2012-08-01') <= 0
        AND DATEDIFF(dd, a.EventsDateTime, '2012-08-21') >= 0              
收获园豆:5
acepro | 小虾三级 |园豆:1218 | 2012-08-21 19:37

哥们,你太用功了。我最后是把where条件中的convert去掉,时间在2秒以内了,你这个我在查询分析器里试了下是31秒,但还是很谢谢啊

暗夜的萤火虫 | 园豆:8 (初学一级) | 2012-08-22 10:29
其他回答(3)
0

创建索引,注意like的不要筹建索引

jason2013 | 园豆:1998 (小虾三级) | 2012-08-21 09:06

select  e.emp_no,e.emp_cname,e.emp_ename,e.dept_code,b.[name], ISNULL(convert(nvarchar(20),EventsDateTime,120),'') cardtime

from   [benq].Sky2004.dbo.AccessEvents a,employee            [benq].Sky2004.dbo.VIEW_DeviceReader b

where

a.personno not exists (select 1  from employee c where c.dept_code='2000' and c.active='Y') AND (convert(nvarchar(20),EventsDateTime,112) BETWEEN '2012-08-01' AND '2012-08-21') and e.active='Y' and  b.[name] like '%考勤%'  

and a.deviceid=b.recnum 

and a.personno=e.emp_no

order by convert(nvarchar(10),EventsDateTime,112) desc,e.emp_no,EventsDateTime  

支持(0) 反对(0) Mark1997 | 园豆:147 (初学一级) | 2012-08-21 09:23

创建在哪些列中好一些呢

支持(0) 反对(0) 暗夜的萤火虫 | 园豆:8 (初学一级) | 2012-08-21 09:27

@MikeAndy: 

再试下

select  e.emp_no,e.emp_cname,e.emp_ename,e.dept_code,b.[name],ISNULL(convert(nvarchar(20),EventsDateTime,120),'') cardtime

from   [benq].Sky2004.dbo.AccessEvents a,employee            [benq].Sky2004.dbo.VIEW_DeviceReader b

where

a.personno exists (select c.personno  from employee c where c.dept_code='2000' and c.active='Y')

AND (convert(nvarchar(20),EventsDateTime,112) BETWEEN '2012-08-01' AND '2012-08-21') and e.active='Y' and  b.[name] like '%考勤%'  

and a.deviceid=b.recnum 

and a.personno=e.emp_no

order by convert(nvarchar(10),EventsDateTime,112) desc,e.emp_no,EventsDateTime  

支持(0) 反对(0) Mark1997 | 园豆:147 (初学一级) | 2012-08-21 09:55
0

朋友,上面那个试下?我也学习下

Mark1997 | 园豆:147 (初学一级) | 2012-08-21 09:24

有语法错误,等会哈

支持(0) 反对(0) 暗夜的萤火虫 | 园豆:8 (初学一级) | 2012-08-21 09:38

我也觉得没问题的,但是查询分析器一直报exists和 AND附近存在语法错误,在帮我看看吧,本人实在是小白

支持(0) 反对(0) 暗夜的萤火虫 | 园豆:8 (初学一级) | 2012-08-21 09:46
0

增加索引 然后不要 like %xx%

you know what it is | 园豆:241 (菜鸟二级) | 2012-08-22 08:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册