Some small ways to improve SQL performance

Source: Internet
Author: User

Set Noccount on
Ignore count (hide 1 rows affected)

With (NOLOCK)
Ignore transaction locks (apply nolock across domains)

Avoid using In,not in,or or having in the WHERE clause.
You can use exist and not exist instead of in and not.

Do not declare numbers in character format, declare character values in numeric format, avoid full table scans
SELECT emp.ename, emp.job from emp WHERE emp.empno = 7369;
Do not use: SELECT emp.ename, emp.job from emp WHERE emp.empno = ' 7369 '

For the rule of the SELECT statement, do the business need those fields to write which fields, do not use *. Also avoid full table scan
Use select Empno,ename,category from emp WHERE empno = ' 7369 '
Instead of using the SELECT * from emp WHERE empno = ' 7369 '

Use like '%parm1% ' carefully, this will cause full table scan, should use like ' parm1% ' to do is index scan

Using Union to implement or operations
Select Id,name,age from table where Name= ' Zhang San ' union ALL select id,name,age from table where age>=18
Do not use Select id,name,age from table where Name= ' Zhang San ' or age>=18

7. Avoid using COUNT (*) to get the number of records in the table
(1) To get the number of records in a table, we typically use the following SQL statement:
SelectCount (*) from Dbo.orders
This statement performs a full table scan to get the number of rows.
(2) But the following SQL statement does not perform a full table scan as it can get the number of rows:
SELECT rows from sysindexes WHERE ID =object_id (' dbo. Orders ') and indid <2

Avoid repeating calls to functions in stored procedures, triggers, functions, and batches, and you should store the results in a variable that you can use directly later.

Instead of using sp_xxx as a naming convention, it causes additional searches and increases I/O (because the system stored procedure name starts with sp_), and this also increases the odds of conflicting with the system stored procedure name.

Simple and Practical article
http://xsxjb.iteye.com/blog/1140036

Some small ways to improve SQL performance

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.