sql如下:
Select count(*) From (select cce.storeid,csf.username,cc.cid,sysclassification,cc.remark, isnull(cs.storequest,0) storequest,cardname,cardno,cardpwd,money,isnull(realmoney,0) realmoney,isnull(usermoney,0) usermoney,isnull(sysmoney,0) sysmoney,cc.createtime,cc.status,errinfo,orderno from card_corder cc left join card_corder_ext cce on cc.cid=cce.cid inner join card_users csf on csf.uid=cc.uid left join card_storequest cs on cs.hisid=cc.cid and cs.ordertype=0 where 1=1 and cc.createtime between cast('2013-04-13 00:00:00' as datetime) and cast('2013-04-15 00:00:00' as datetime) ) daTable
执行结果如下:
平常没事,但是有时候就会
超时时间已到。在操作完成之前超时时间已过或服务器未响应。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
这个时候我把sql拿出来到数据库执行也是很慢。
但是我修改一下sql:
Select count(*) From (select cce.storeid,csf.username,cc.cid,sysclassification,cc.remark, isnull(cs.storequest,0) storequest,cardname,cardno,cardpwd,money,isnull(realmoney,0) realmoney,isnull(usermoney,0) usermoney,isnull(sysmoney,0) sysmoney,cc.createtime,cc.status,errinfo,orderno from card_corder cc left join card_corder_ext cce on cc.cid=cce.cid inner join card_users csf on csf.uid=cc.uid left join card_storequest cs on cs.hisid=cc.cid and cs.ordertype=0 where 1=1 --and cc.createtime between cast('2013-04-13 00:00:00' as datetime) and cast('2013-04-15 00:00:00' as datetime) ) daTable
把时间的条件去掉.
查询一秒不到就查询出来了.
总记录也不是很多,大概7W条。网站总是时不时的来报个错。很奇怪阿。
and cc.createtime between '2013-04-13' and '2013-04-15 00:00:00'
执行函数的效率较低
在数据库内添加 sort 字段
例如 time(datetime) 2013-04-15
sort(bigint) 20130415
查询的时候 sort between 20130415 ...
用datetime 查询 会大大降低查询的效率
SELECT COUNT(*) FROM ( SELECT cce.storeid FROM card_corder cc LEFT JOIN card_corder_ext cce ON cc.cid = cce.cid INNER JOIN card_users csf ON csf.uid = cc.uid LEFT JOIN card_storequest cs ON cs.hisid = cc.cid AND cs.ordertype = 0 WHERE cc.createtime BETWEEN '2013-04-13 00:00:00' and '2013-04-15 00:00:00' ) daTable
优化下数据库,索引,主键,数据用多少取多少,对条件字段,最好在索引上...
优化一下sql,不需要查询的字段都去掉...
我估计是卡住了, 加个NOLOCK吧,
死锁,