SQL query optimization

Source: Internet
Author: User

One, the database design angle

1. Avoid full table queries as much as possible, first consider indexing on the column of the Where level order by.

2. Try to avoid null judgments in the WHERE clause, causing the engine to discard the index for a full table scan. Columns such as: Select ID from t where num is NULL, can be set to 0 in num, ensure no null appears

3. Not all indexes are valid for queries, and if the fields contain large amounts of duplicate data, SQL optimizes the query and does not use the index

4. Indexing improves select efficiency, but decreases insert and update because indexing is not better than 6 indexes in a table

5. Avoid updating indexed data columns

6. Use numeric fields as much as possible

7. Use Varchar/nvarchar instead of Char/uchar

8. Try to use table variables instead of temporary tables

9. Avoid frequent creation and deletion of temporary tables

Two-SQL aspect

1. Try to avoid using in the WHERE clause! =,<, which causes the engine to discard the index for a full table scan

2. Try to avoid using or in the WHERE clause causes the engine to discard the index for full table scan

Select ID from t where id = 1 or id = 2 can use Select ID from t where ID union ALL select ID from t where id = 2

3.in and not in also use caution will cause a full table scan, for continuous values can be used between

Select ID from where ID in (between) can use Select ID from where ID 1 and 3

SQL query optimization

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.