General experience in designing efficient SQL statements

Source: Internet
Author: User

1You do not needSQLThe statement uses the default reserved keywords.

2Try to useExistsAndNot existsReplaceInAndNot in

ThisSql2005Then, when the index and statistics are the same,Exists,InThe results are the same.

ToAdventureworksFor exampleHumanResources.EmployeeaddressAddresses availableEmployeeInformation,

UseInThe statement is as follows:

Set Statistics Io On

 

Select * FromHumanResources.Employee

WhereEmployeeidIn (SelectEmployeeidFromHumanResources.Employeeaddress EA)

 

Set Statistics Io Off

After execution, the message is as follows:

 

(290Rows affected)

Table 'Employeeaddress' . Scan count 1 , Logical read 4 Physical reads 0 Times, pre-read 0 Times, Lob Logical read 0 Times, Lob Physical read 0 Times, Lob Pre-read 0 Times.

Table 'Employee' . Scan count 1 , Logical read 9 Physical reads 0 Times, pre-read 0 Times, Lob Logical read 0 Times, Lob Physical read 0 Times, Lob Pre-read 0 Times.

Execution Plan

 

UseExists, The statement is as follows,

 

Set Statistics Io On

Select * FromHumanResources.Employee

Where Exists (SelectEmployeeidFromHumanResources.Employeeaddress EA

Where

HumanResources.Employee.Employeeid=EA.Employeeid)

 

Set Statistics Io Off

After execution, the message is as follows:

 

(290Rows affected)

Table 'Employeeaddress' . Scan count 1 , Logical read 4 Physical reads 0 Times, pre-read 0 Times, Lob Logical read 0 Times, Lob Physical read 0 Times, Lob Pre-read 0 Times.

Table 'Employee' . Scan count 1 , Logical read 9 Physical reads 0 Times, pre-read 0 Times, Lob Logical read 0 Times, Lob Physical read 0 Times, Lob Pre-read 0 Times.

Execution Plan

 

3Try not to useSelect * from .....,The field name must be written.Select field1, field2 ,...

There is nothing to say about this article, mainly for on-demand queries, do not return unnecessary columns and rows.

 

4InSQLIndex columns should be used as much as possible to speed up the query

5AnyOrderNon-index items or computed expressions of a statement can reduce the query speed.

6AnyWhereClauseIs nullOrIs not nullThe statement of is not allowed to use the index, which is less efficient.

7Wildcard%At the beginning of a word, the system does not use an index. When a wildcard appears at another location, the optimizer can use the index.

8InSQLUse as few formats as possible in the query statement

9Any operation on the column will cause the table to scan, including database functions and calculation expressions. During the query, try to move the operation to the right of the equal sign.

10 In,OrThe clause usually invalidates the index.

11Generally, the connection is more efficient than the subquery.

 

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.