SQL Optimization-replace subquery with join query

Source: Internet
Author: User

SQL optimization -- replace subquery with join query

 

Test example:

Subquery:

Select A. * ,
( Select Workflowname From Workflowbase Where ID = Workflowid) workflowname
From [ [Zping.com ] ]
Where A. Operator = ' 402882ed1112669201%a8%92f33 '

 

Execution result:

( 360 Rows affected)
Table ' Worktable ' . Scan count 360 , Logical read 142334 Physical reads 0 Times, pre-read 0 Times, lob logic reads 0 Physical lob reads 0 Times, lob pre-read 0 Times.
Table ' Workflowbase ' . Scan count 1 , Logical read 1589 Physical reads 0 Times, pre-read 0 Times, lob logic reads 0 Physical lob reads 0 Times, lob pre-read 0 Times.
Table ' [Zping.com] ' . Scan count 1 , Logical read 366 Physical reads 0 Times, pre-read 0 Times, lob logic reads 0 Physical lob reads 0 Times, lob pre-read 0 Times.

 

Join query:

Select A. * , B. workflowname
From [ [Zping.com ] ] Inner   Join Workflowbase B On A. workflowid = B. ID
Where Operator = ' 402882ed1112669201%a8%92f33 '

Execution result:

( 360 Rows affected)
Table ' Worktable ' . Scan count 0 , Logical read 0 Physical reads 0 Times, pre-read 0 Times, lob logic reads 0 Physical lob reads 0 Times, lob pre-read 0 Times.
Table ' Workflowbase ' . Scan count 1 , Logical read 1589 Physical reads 0 Times, pre-read 0 Times, lob logic reads 0 Physical lob reads 0 Times, lob pre-read 0 Times.
Table ' [Zping.com] ' . Scan count 1 , Logical read 366 Physical reads 0 Times, pre-read 0 Times, lob logic reads 0 Physical lob reads 0 Times, lob pre-read 0 Times.

 

Here: the number of subqueries I/O:142334 +1589+366 = 144289

Associated query Io count:1589 +366 = 1922

The associated query is 75 times that of the subquery. 

 

Summary:

 

Subqueries and associated queries are used. Generally, subqueries are not used if associated queries are used,

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.