Create a function index to optimize an SQL statement

Source: Internet
Author: User
Development sends an SQL statement for Optimization
Select Distinct Oi.Customer_Id, Oi.Order_Id, Oi.Create_Time, 1 Type  From t_To_Order_Info Oi  Left Join t_To_Consign_Pymt_Record Cpr    On Oi.Order_Id = Cpr.Order_Id Where 1 = 1   And Exists (Select 1          From t_To_Order_Tickets         Where t_To_Order_Tickets.Order_Id = Oi.Order_Id           And To_Char(Departure_Time, 'yyyyMMdd') =               To_Char(Sysdate, 'yyyyMMdd'))   And Not Exists (Select 1 From t_To_Order_Change Oc Where Oc.Order_Id = Oi.Order_Id)   And ((Cpr.Pymt_Type = 1 Or       Cpr.Pymt_Type = 3 And Oi.Guest_Pymt_Sts_Id = 2) Or       (Cpr.Pymt_Type = 2 And       Cpr.Partner_Id In       (Select p.Partner_Id            From t_Tp_Partner p           Where p.Is_Company_Inner = 1) And Oi.Guest_Pymt_Sts_Id = 2) Or       (Cpr.Pymt_Type = 2 And       Cpr.Partner_Id In       (Select p.Partner_Id            From t_Tp_Partner p           Where p.Agent_Class_Id = 1             And p.Is_Company_Inner != 1) And       To_Char(Oi.Issue_Time) <> ' ') Or       (Cpr.Pymt_Type = 2 And       Cpr.Partner_Id In       (Select p.Partner_Id            From t_Tp_Partner p           Where p.Agent_Class_Id != 1) And To_Char(Oi.Issue_Time) <> ' '))

Such an SQL statement takes more than 30 seconds


My first thought is to look at the execution plan. The t_to_order_info and t_to_order_tickets tables are hash connections, but the two tables use full table scans, I first directed t_to_order_info to scan the entire table. I thought it was caused by exists. Here I don't know where the index was not taken, I run the SQL statement several times, and then run the TOP command in the EM tool.
This SQL statement is found in the SQL statement and read the suggestions given by Oracle.

First, run the following command to collect the index statistics.
Dbms_stats.gather_index_stats (ownname => 'test20110217 ', indname => 'idx _ bt_to_order_hang', estimate_percent => dbms_stats.auto_sample_size );
Dbms_stats.gather_index_stats (ownname => 'test20110217 ', indname => 'to _ relationchange_fk', estimate_percent => dbms_stats.auto_sample_size );
Dbms_stats.gather_index_stats (ownname => 'test20110217 ', indname => 'pk _ t_to_order_change', estimate_percent => dbms_stats.auto_sample_size );
End;


After the operation, the efficiency is not improved. Then we can see a suggestion from Oracle, that is
T_to_order_tickets
Wheret_to_order_tickets.order_id = Oi. order_id
Andto_char (departure_time, 'yyyymmdd') =
To_char (sysdate, 'yyyymmdd '))
There was a problem here. departure_time was originally indexed, but the index was not used here, because the to_char method could not make it index again,
This is the key to the problem. This problem can be solved through function indexing.

SQL> Create index test20110217.idx _ hansu on test20110217.t _ to_order_tickets (to_char ("departure_time", 'yyyymmdd '));
SQL> exec dbms_stats.gather_index_stats ('test20110217 ', 'idx _ hansu ');

The above function index is created, and the statistics of the new index are collected. The SQL efficiency is greatly improved, and the SQL efficiency is almost 1 second.
The proof is that the function indexes and common indexes on a column can coexist, but they do not affect performance. The problem has been solved here.
But after development, departure_time + 1 will cause no index again, so you must rewrite the SQL statement to solve this problem.

And departure_time between to_date (to_char (sysdate-1, 'yyyy-MM-DD hh24: MI: ss'), 'yyyy-MM-DD hh24: MI: ss ')
And to_date (to_char (sysdate, 'yyyy-MM-DD hh24: MI: ss'), 'yyyy-MM-DD hh24: MI: ss '))

In fact, the key to the problem lies in departure_time. In the above method, departure_time remains unchanged, and the index can be solved only when the equal sign is changed.

Summary:
The key to the problem is how to locate the problem because the departure_time column does not take the index,
This is a reminder from the EM tool. If the topsql cannot be found in the EM tool
Go to this point for analysis. T_to_order_tickets
This is the field. There is no experience in this optimization. Next time I see that if the time field is used, I should
Locate the problem quickly
 

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.