聚集索引遇到的性能问题(查询分析器试验)

softlife12 softlife12 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


背景:做的是userId+sdate的聚集索引,userId在前
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='01'

后由于发现性能很差,便在查询分析器中做实验

当实现
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='01'
select * from test where datediff(day,sdate,'2006-1-2')=0 and userId='01'
select * from test where datediff(day,sdate,'2006-1-3')=0 and userId='01'

也即变化sdate,保持userId不变时,性能很好,

而当
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='01'
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='02'
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='03'
性能却慢了一倍多的时间

如果在where中把sdate和userId的位置对调也是这样

由于我在实际中的例子是按照
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='01'
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='02'
select * from test where datediff(day,sdate,'2006-1-1')=0 and userId='03'

来的,所以只能上来问下大家,谢谢
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
softlife12
MS-SQL Server 34,876 成员 254,635 内容