Oracle Sql最佳化一例---利用函數索引

來源:互聯網
上載者:User

標籤:bat   sql   and   creat   部分   where   建立   nbsp   驗證   

在awr報告中,發現有個Sql效率很低:

select batch_status from  t_batch_info

where BATCH_STATUS=‘0‘ 

and sys_id=‘STM06‘;

查看執行計畫發現查詢利用了索引,索引中包含了batch_status欄位,但是通過以下sql查詢:

select batch_status,count(*) from t_batch_info 
group by BATCH_STATUS

發現batch_status欄位的值很少:

batch_status count(*)

0   40350

1   4237357
2  1227
3   433515

之前sql的查詢條件是batch_status=‘0‘,它只有4萬多條資料,而絕大部分都是1,有400多萬條資料,因此雖然用到了索引,但是使用效率低,

為解決這個問題,建立了函數索引,利用null值不進索引的特性,

create index ix_t_batch_info  on t_batch_info (decode(BATCH_STATUS,‘1‘,null,‘2‘,null,‘3‘,null,BATCH_STATUS)) online;

而查詢語句改成:

select batch_status from  t_batch_info

where decode(BATCH_STATUS,‘1‘,null,‘2‘,null,‘3‘,null,BATCH_STATUS)=‘0‘ 

and sys_id=‘STM06‘;

經過驗證,效率提高很多。

 

Oracle Sql最佳化一例---利用函數索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.