一個複合索引的優化案例
示例SQL語句:
其執行計劃如下
都是通過全表掃描訪問的。
注意到A和B的連線欄位是prod_id, cust_id, time_id, channel_id;
主查詢的篩選條件是表B的以上欄位;
表AD,ID,AK,IK的篩選條件prod_id,cust_id,time_id,包括其中;
AS連(AD+AK)的連線欄位包括了prod_id, cust_id, time_id, channel_id;
IS連(ID+IK)的連線欄位包括了prod_id, cust_id, time_id, channel_id;
最後決定:在表A,B,AD,ID,AK,IK,AS,IS上都建立(prod_id,cust_id,time_id,channel_id)的複合索引,例:
再次檢視執行計劃,限於篇幅,只擷取部分
優化器選擇了新建立的索引,執行效率飛速提升。
進一步討論:如果主查詢中的篩選條件
WHERE (B.prod_id = 110) and (B.cust_id = 1541239) and
(B.time_id = to_date('2012-04-15','YYYY-MM-DD')) and (B.channel_id = 2)
去掉,則A、B的連線將會走全表掃描
點選(此處)摺疊或開啟
- select distinct A.time_id, A.seller, a.quantity_sold, A.amount_sold, B.tax_country
-
from sales_A A
join sales_B B on
B.prod_id = A.prod_id and
B.cust_id = A.cust_id and
B.time_id = A.time_id and
B.channel_id = A.channel_id
left join (
select promo_id, channel_id, TAX_COUNTRY
from sales_AD9598
where (prod_id = 110) and (cust_id = 1541239) and (time_id = to_date('2012-04-15','YYYY-MM-DD'))
union all
select promo_id, channel_id, TAX_COUNTRY
from sales_ID9902
where (prod_id = 110) and (cust_id = 1541239) and (time_id = to_date('2012-04-15','YYYY-MM-DD'))
union all
select promo_id, channel_id, TAX_COUNTRY
from sales_AK0306
where (prod_id = 110) and (cust_id = 1541239) and (time_id = to_date('2012-04-15','YYYY-MM-DD'))
union all
select promo_id, channel_id, TAX_COUNTRY
from sales_IK0711
where (prod_id = 110) and (cust_id = 1541239) and (time_id = to_date('2012-04-15','YYYY-MM-DD'))
union all
select AS9704.promo_id, AS9704.channel_id, AS9704.TAX_COUNTRY
from sales_AS9704 AS9704
left join sales_AD9598 AD9598 on
AS9704.prod_id = AD9598.prod_id and AS9704.cust_id = AD9598.cust_id and
AS9704.time_id = AD9598.time_id and AS9704.channel_id = AD9598.channel_id and
AS9704.promo_id = AD9598.promo_id
left join sales_AK0306 AK0306 on
AS9704.prod_id = AK0306.prod_id and AS9704.cust_id = AK0306.cust_id and
AS9704.time_id = AK0306.time_id and AS9704.channel_id = AK0306.channel_id and
AS9704.promo_id = AK0306.promo_id
where (AS9704.prod_id = 110) and (AS9704.cust_id = 1541239) and
(AS9704.time_id = to_date('2012-04-15','YYYY-MM-DD'))
union all
select IS0108.promo_id, IS0108.channel_id, IS0108.TAX_COUNTRY
from sales_IS0108 IS0108
left join sales_ID9902 ID9902 on
IS0108.prod_id = ID9902.prod_id and IS0108.cust_id = ID9902.cust_id and
IS0108.time_id = ID9902.time_id and IS0108.channel_id = ID9902.channel_id and
IS0108.promo_id = ID9902.promo_id
left join sales_IK0711 IK0711 on
IS0108.prod_id = IK0711.prod_id and IS0108.cust_id = IK0711.cust_id and
IS0108.time_id = IK0711.time_id and IS0108.channel_id = IK0711.channel_id and
IS0108.FULFILLMENT_CENTER = IK0711.FULFILLMENT_CENTER
where (IS0108.prod_id = 110) and (IS0108.cust_id = 1541239) and
(IS0108.time_id = to_date('2012-04-15','YYYY-MM-DD'))
) D on B.promo_id = D.promo_id and B.channel_id = D.channel_id
WHERE (B.prod_id = 110) and (B.cust_id = 1541239) and
(B.time_id = to_date('2012-04-15','YYYY-MM-DD')) and (B.channel_id = 2)
order by A.quantity_sold, A.amount_sold;
其執行計劃如下
都是通過全表掃描訪問的。
注意到A和B的連線欄位是prod_id, cust_id, time_id, channel_id;
主查詢的篩選條件是表B的以上欄位;
表AD,ID,AK,IK的篩選條件prod_id,cust_id,time_id,包括其中;
AS連(AD+AK)的連線欄位包括了prod_id, cust_id, time_id, channel_id;
IS連(ID+IK)的連線欄位包括了prod_id, cust_id, time_id, channel_id;
最後決定:在表A,B,AD,ID,AK,IK,AS,IS上都建立(prod_id,cust_id,time_id,channel_id)的複合索引,例:
點選(此處)摺疊或開啟
- create index sales_A_pk on sales_A(
- prod_id, cust_id, time_id, channel_id);
優化器選擇了新建立的索引,執行效率飛速提升。
進一步討論:如果主查詢中的篩選條件
WHERE (B.prod_id = 110) and (B.cust_id = 1541239) and
(B.time_id = to_date('2012-04-15','YYYY-MM-DD')) and (B.channel_id = 2)
去掉,則A、B的連線將會走全表掃描
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22621861/viewspace-2081846/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL優化案例一則--複合索引沒有被使用SQL優化索引
- SUM優化(複合索引)優化索引
- 又一個複合索引的SQL調優索引SQL
- ORACLE 9i資料庫優化案例(2) --- 單列索引變複合索引的優化Oracle資料庫優化索引
- 複合索引與函式索引優化一例索引函式優化
- SQLT 最佳化SQL 用複合索引代替單列索引的案例SQL索引
- SQL最佳化案例一則--複合索引沒有被使用SQL索引
- 增加複合索引優化SQL的簡單過程索引優化SQL
- 常數複合索引應用案例索引
- 查詢中讓優化器使用複合索引優化索引
- 一個效能優化的案例優化
- 【MySQL】NOT EXISTS優化的一個案例MySql優化
- 記一個SQL優化案例SQL優化
- 一個MySQL優化案例的初步思路MySql優化
- SQL優化(二)(聯合索引的使用)SQL優化索引
- SQL優化案例-正確的使用索引(二)SQL優化索引
- Oracle優化案例-正確的使用索引(二)Oracle優化索引
- MySQL複合索引MySql索引
- SQLServer索引優化(3):對於建組合索引的要求SQLServer索引優化
- SQL優化案例-分割槽索引之無字首索引(六)SQL優化索引
- Oracle優化案例-分割槽索引之無字首索引(六)Oracle優化索引
- sql優化案例一:使用了表示式不會使用索引SQL優化索引
- Mysql索引優化(一)MySql索引優化
- 一個缺乏索引和統計資訊的優化過程索引優化
- MySQL複合索引探究MySql索引
- MySQL建立複合索引MySql索引
- 【oracle 效能優化】組合索引查詢。Oracle優化索引
- SQL優化案例-自定義函式索引(五)SQL優化函式索引
- Oracle優化案例-自定義函式索引(五)Oracle優化函式索引
- 優化案例--重建索引引發的sql效能問題優化索引SQL
- Oracle效能優化-SQL優化(案例一)Oracle優化SQL
- OGG複製程式延遲高,優化方法一(使用索引)優化索引
- MongoDB複合索引詳解MongoDB索引
- DataFrame刪除複合索引索引
- 一次成功的優化案例優化
- 字首索引,一種優化索引大小的解決方案索引優化
- MySQL SQL優化案例(一)MySql優化
- IO優化案例一則優化