现在一个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
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
哥们,你太用功了。我最后是把where条件中的convert去掉,时间在2秒以内了,你这个我在查询分析器里试了下是31秒,但还是很谢谢啊
创建索引,注意like的不要筹建索引
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
创建在哪些列中好一些呢
@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
朋友,上面那个试下?我也学习下
有语法错误,等会哈
我也觉得没问题的,但是查询分析器一直报exists和 AND附近存在语法错误,在帮我看看吧,本人实在是小白
增加索引 然后不要 like %xx%