Oracle table join causes sorting disorder

Source: Internet
Author: User
Tags joins

Hot weather, weekend at home knocking at the code to find a strange SQL, when using the LEFT JOIN table to join the query, a to go to join B, before the order of a table row, suddenly confused, the first row of records ran to the end, before the descending order to become ascending! Simulate SQL as follows

SELECT
A. *
From
(
SELECT
A. *
From
(
SELECT
A. *,
ROWNUM R
From
(
SELECT
*
From
Tporder A
WHERE
To_char (A. Plattime, ' yyyy-mm-dd ') >= ' 2015-08-09 '
and To_char (A. Plattime, ' yyyy-mm-dd ') <= ' 2015-08-09 '
and A. TYPE in (0, 1, 23, 24, 25, 30)
and A. Busi_type = 1
ORDER by
Plattime DESC
) A
Left joins Tmerinfo B on A. STOREID = B.taccountid
) A
Left joins Tbankmore C on A. CARD_BANK_TP = c. ID
WHERE
A. R <=20
) A
Left JOIN tpransaction tr on a.id = tr. Torderid (execute this sentence before you follow plattime in descending-to-ascending order).

It's been a long time without thinking about the solution, and suddenly it's like looking at this SQL execution plan, we know that table joins have 3 ways, nested inner loops, sort merge connections, Hash joins, according to our SQL case, because the ID of a table and the Torder of B table are connected, the primary key of a table must be sorted, The default is ascending, and the Oracle optimization engine is definitely using a sort merge connection algorithm that causes the problem to occur. The workaround is to set up a descending index for the Torderid field in the B table, so that the merging will not only improve the efficiency of SQL execution, but also avoid the problem just now!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Oracle table join causes sorting disorder

Related Article

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.