SQL SERVER 強制指定使用索引 -转载 只为学习

今天很高兴 ,有学会了一种数据库优化的方式,哈哈

今天遇到一個查詢逾時的問題:兩段SQL,只差在WHERE,一個是WHERE COLUMN1='AAA',一個是WHERE COLUMN1='BBB',產生的執行計畫卻不一樣;一個用PK索引,一個用IX索引(叢集索引跟非叢集索引的差別?)

查到兩種方法,INDEX()跟FORCESEEK

  1. INDEX('指定索引名稱')
  2. FORCESEEK 指定從哪個資料表搜尋
--系統會自動選用IX_index select   count(1) from  table1 a   with(nolock) join  table2 b   with(nolock)   on  a.key_col=b.key_col where  b.some_col= 'aaa'   --系統會自動選用PK_index select   count(1) from  table1 a   with(nolock) join  table2 b   with(nolock)   on  a.key_col=b.key_col where  b.some_col= 'bbb'   --指定使用 PK_index select   count(1) from  table1 a   with(nolock) join  table2 b   with(nolock,,   INDEX( PK_table2 ))   on  a.key_col=b.key_col where  b.some_col= 'aaa'   --沒實際用成功過,不知是否有效,資料庫相容性模式要設為90 select   count(1) from  table1 a   with(nolock) join  table2 b   with(FORCESEEK)   on  a.key_col=b.key_col where  b.some_col= 'aaa'

參考資料:

http://msdn.microsoft.com/zh-tw/library/bb677261.aspx

http://msdn.microsoft.com/zh-tw/library/bb510478.aspx

DotBlogs Tags: T-SQL 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoshengjinbu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值