SQL Performance Optimization Tips (i)

Source: Internet
Author: User


About the effect of SQL condition matching on execution efficiency test

First, create a scalar function
Create function Ff_test ()

returns int

As

Begin

DECLARE @i int=0

while (@i<100000000)

Set @i+=1

Return @i

End

Next, select a random table, where you use the business table Mt_delegate

Observe the following two types of situations

1. Select * from mt_delegate where procid=-1 or dbo.ff_test () >10000

2. Select * from Mt_delegate where dbo.ff_test () >10000 or Procid=-1

The result is that the order of the Where condition statements does not affect execution, and the SQL engine clearly optimizes where conditions

Observe the following situation again

1.------------------------------------------------------------------------------------------------
Select A.* from Dbo.mt_delegate a
INNER JOIN Dbo.mt_delegate B on A.procid=b.procid and (dbo. Ff_test () >10000 or
A.ACTIVITYID=-1)

2.------------------------------------------------------------------------------------------------
Select A.* from Dbo.mt_delegate a
INNER JOIN Dbo.mt_delegate B on a.procid=b.procid and (A.activityid=-1 or
Dbo. Ff_test () >10000)

As a result, the query of 1 cases is obviously inefficient, and the 2 case results quickly, so it is presumed that SQL connection query condition execution is based on left-to-right order, not some articles say right match

conclusion, the SQL query connection query condition and the Where condition have different sequence requirements for the condition, and the connection query suggests that the large probability condition be placed on the left side

SQL Performance Optimization Tips (i)

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.