首页 新闻 赞助 找找看

关于sql2008下datetime类型字段与字符串类型的时间转换的问题

0
[待解决问题]

最近遇到一个问题,请sql大神指点一下

sql1:

select count(1) from ( SELECT CustCode,ShopCode,CreateTime,UniqCode,SaleType,TotalMoney,ExamineStatus,[Status],DeleFlag FROM t_ShopSerLog y where 1=1 and datediff(DAY,'2017/6/26 0:00:00',y.CreateTime) >=0 and datediff(DAY,'2017/6/27 0:00:00',y.CreateTime) <0 ) a left join t_Cust b on a.CustCode= b.UniqCode left join t_CustVCard c on a.CustCode= c.CustUniqCode and a.ShopCode=c.ShopCode where 1=1 and (a.Status=1 or a.ExamineStatus=-1 or a.SaleType=9) and a.DeleFlag=1 and a.ShopCode='a4fc6360-7d6d-41b5-907e-8815e2246e2b'

sql2:

select count(1) from ( SELECT CustCode,ShopCode,CreateTime,UniqCode,SaleType,TotalMoney,ExamineStatus,[Status],DeleFlag FROM t_ShopSerLog y where 1=1 and y.CreateTime >='2017/6/26 0:00:00' and y.CreateTime<'2017/6/27 0:00:00' ) a left join t_Cust b on a.CustCode= b.UniqCode left join t_CustVCard c on a.CustCode= c.CustUniqCode and a.ShopCode=c.ShopCode where 1=1 and (a.Status=1 or a.ExamineStatus=-1 or a.SaleType=9) and a.DeleFlag=1 and a.ShopCode='a4fc6360-7d6d-41b5-907e-8815e2246e2b'

sql1 在2008跟2012上运行的都比较快,sql2在2012上运行的也可以,但sql2在2008上运行的超级慢,几十秒,一运行CPU就100%,这是什么原因?sql1跟sql2唯一的区别就是这个时间字符串,求指点

问题补充:

本来想着不用sql给的函数,怕函数比较慢,直接取区间值会比较快一点,谁知道更慢了

一只小青蛙-呱-呱-dyj的主页 一只小青蛙-呱-呱-dyj | 菜鸟二级 | 园豆:202
提问于:2017-07-03 10:58
< >
分享
所有回答(1)
0

我打酱油帮你分析下把  你的CreateTime是日期类型字段?

你可以测试下面几个方法:

1.把最后WHERE的过滤方法放到()a 这个句子里面,如下:

select count(1) from ( SELECT CustCode,ShopCode,CreateTime,UniqCode,SaleType,TotalMoney,ExamineStatus,[Status],DeleFlag FROM t_ShopSerLog y where 1=1 and (y.CreateTime >='2017/6/26 0:00:00' and y.CreateTime<'2017/6/27 0:00:00') and (a.Status=1 or a.ExamineStatus=-1 or a.SaleType=9) and a.DeleFlag=1 and a.ShopCode='a4fc6360-7d6d-41b5-907e-8815e2246e2b') a left join t_Cust b on a.CustCode= b.UniqCode left join t_CustVCard c on a.CustCode= c.CustUniqCode and a.ShopCode=c.ShopCode  

 

2. 给createTime这个字段单独加个索引 

犇牛牛 | 园豆:31 (初学一级) | 2017-07-03 14:48

嗯,是datetime 类型

针对一:上午我就把where后面的条件放到了()a 里面去了,但还是不能用y.CreateTime >='2017/6/26 0:00:00' and y.CreateTime<'2017/6/27 0:00:00' 这个条件,一用08就卡死了,12版本就不会,很无语

针对二:还真没有在createtime上加索引,因为这个字段的时间查询的不是特别频繁而且一直在改动,所以才没有加索引,不过待会可以加上试试

 

 

清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册