關於MS SQLSERVER索引優化問題:
有表Stress_test(id int
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]id 上有普通索引;
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]key 上有簇索引;
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]id 有有限量的重復;
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]key 有無限量的重復;
現在我需要按邏輯與查詢表中key=
求教高手最有效的查詢語句
測試環境
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]Hardware:P
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]Software:windows server
[$nbsp][$nbsp]首先我們建立一個測試的數據
[$nbsp][$nbsp][$nbsp]因為是隨機產生的數據
[$nbsp][$nbsp][$nbsp]下面的
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]Select @maxgroup=
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]Select @maxLoop=
如果要測試
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]Select @maxgroup=
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]Select @maxLoop=
所以如果你的SERVER或PC比較慢
(在我的PC上運行的速度是插入
作為一般的開發人員很容易就想到的語句
[$nbsp][$nbsp][$nbsp]
[$nbsp][$nbsp][$nbsp][$nbsp]select a
[$nbsp][$nbsp][$nbsp][$nbsp](select distinct [id] from stress_test where [key] =
[$nbsp][$nbsp][$nbsp][$nbsp](select distinct [id] from stress_test where [key] =
[$nbsp][$nbsp][$nbsp][$nbsp](select distinct [id] from stress_test where [key] =
[$nbsp][$nbsp][$nbsp][$nbsp]where a
[$nbsp][$nbsp][$nbsp]
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]select [id]
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]from stress_test
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]where [key]=
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]group by id having(count(distinct [key])=
[$nbsp][$nbsp][$nbsp]
[$nbsp][$nbsp][$nbsp][$nbsp]SELECT distinct a
[$nbsp][$nbsp][$nbsp][$nbsp]WHERE a
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]AND a
但作為T
[$nbsp][$nbsp][$nbsp]
[$nbsp][$nbsp][$nbsp][$nbsp]select distinct [id] from stress_test A where
[$nbsp][$nbsp][$nbsp][$nbsp]not exists (
[$nbsp][$nbsp][$nbsp][$nbsp]select
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp](select
[$nbsp][$nbsp][$nbsp][$nbsp]left join stress_test C on C
[$nbsp][$nbsp][$nbsp][$nbsp]where C
[$nbsp][$nbsp][$nbsp]
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]select distinct a
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]where not exists
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]( select * from keytb c
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]where not exists
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]( select * from stress_test b
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]where
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]b
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]and
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp]c
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp][$nbsp])
[$nbsp][$nbsp][$nbsp][$nbsp][$nbsp])
我們先分析這幾條語句(針對
請大家要特別留心Estimated row count的值
語句
語句
語句
語句
語句
我們可以先測試一下小的數據量(
大家可以下面測試腳本的
[$nbsp][$nbsp][$nbsp]Select @maxgroup=
[$nbsp][$nbsp][$nbsp]Select @maxLoop=
|
|
|
從測試的的數據來看
我們測試百萬條以上的記錄
統計表
|
|
|
|
|
統計表
|
|
|
|
|
測試總結
From:http://tw.wingwit.com/Article/program/Oracle/201311/18861.html