Sql Server 自动查询优化有时候也是靠不住的

[ 2006-10-19 20:44:34 | 作者: progame ]
文字大小: | |
这样的一个查询
select ... from table where fitler1 and fitler2 and filter3 and fitler4

fitler3 中使用了计算函数,filter4 非常复杂,filter1 和 filter2 可以把结果集的范围迅速减小,所以利用 fitler2 的一个 date 索引是最好的检索方案。

但是sql server不是这样想的,它居然对表进行了聚集索引的扫描,是 scan,不是 seek! 结果在 scan 后的 fitler 时因为 function 的计算非常耗时,导致整个查询需要2分多钟,怒!

想办法吧,改成这样
select * from (select ... from table where filter1 and filter2) a where filter3 and filter4

效果依然,sql server 肯定在说,小样,想蒙我,以为穿件马夹我不认识你了?!

没办法,为了按照自己的意思做,强行使用 table hints, 人为指定使用的 index:
 select * from table with (index(ix_table_dateindex)) where fitler1 and filter2 and filter3 and filter4

难道说我的 sqlbroker 需要考虑加上 query hints? 苍天啊,大地啊,等到有需要那一天再说吧。
评论Feed 评论Feed: /feed.asp?q=comment&id=124

浏览模式: 显示全部 | 评论: 2 | 引用: 0 | 排序 | 浏览: 1542
引用 noname*
[ 2006-10-23 21:57:23 ]
这是一个隐藏评论.
引用 progame
[ 2006-10-24 09:51:29 ]
不认识 何方神圣?

发表
表情图标
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:   (非注册用户不需要输入密码) 注册?
验证码(不区分大小写) * 请输入验证码