Oracle SQL Optimization Example---using function index

Source: Internet
Author: User

In the AWR report, it was found that a SQL is inefficient:

Select Batch_status from T_batch_info

where batch_status= ' 0 '

and sys_id= ' STM06 ';

View execution Plan Discovery the query takes advantage of the index, which contains the Batch_status field, but with the following SQL query:

Select Batch_status,count (*) from T_batch_info
GROUP BY Batch_status

There are few values found for the Batch_status field:

Batch_status COUNT (*)

0 40350

1 4237357
2 1227
3 433515

Before the SQL query condition is batch_status= ' 0 ', it only has more than 40,000 data, and most of them are 1, there is 400多万条 data, so although the index, but the use of low efficiency,

To solve this problem, a function index is created, with the null value not being indexed,

Create INDEX Ix_t_batch_info on T_batch_info (Decode (Batch_status, ' 1 ', NULL, ' 2 ', NULL, ' 3 ', null,batch_status)) online;

And the query statement is changed to:

Select Batch_status from T_batch_info

where Decode (Batch_status, ' 1 ', NULL, ' 2 ', NULL, ' 3 ', null,batch_status) = ' 0 '

and sys_id= ' STM06 ';

Proven to be more efficient.

Oracle SQL Optimization Example---using function index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.