Optimize SQL, return fewer rows, NL is much faster than hash

Source: Internet
Author: User

SQL as follows

Select   as Sortno    from Engineering_type T    Left Join engineering_type tt      on =  where=0and   =0Order   by Sortno, T.sort

SQL is simple, equivalent to self-connected, return rows 12 rows, very small, but run around 5s to produce results

Take a look at the execution plan

As you can see, the table and table go a hash join, our general rule when the return row number is large, more than million lines, usually use a hash join, in the return of the number of rows is rarely used NL connection

Modify SQL

Select /*+ USE_NL (T,TT) leading (t)*/T.id, T.value, Tt.sort asSortno fromEngineering_type T Left JoinEngineering_type TT onT.parentid=tt.idwhereT.delete_flag= 0    andT.hasson= 0 Order  bySortno, T.sort

The implementation plan is as follows

The execution plan changed, and the hash became NL.

The execution speed of SQL is significantly improved, and the return implementation is 0.03 seconds or so, seconds kill.

When SQL chooses how to connect, the first thing to do is to look at the number of rows returned by the driver table and the driver table, which is the most basic judgment.

Optimize SQL, return fewer rows, NL is much faster than hash

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.