Some simple tips for optimizing database statements

Source: Internet
Author: User
Tags sql client

1. Index

A. The fields used in the where and join parts of the SQL statement should be indexed.

B. Try to use indexed fields for direct judgment, do not convert the indexed fields before you judge, because this will result in the addition of the index is invalid, the effect of optimization is not achieved.

e.g. querying all data in table A for all 2016 years

CREATE INDEX a_idx on A (Date_column); SELECT text, date_column from  A WHERE to_char (date_column, ' YYYY ') = ' 2016 ';

The above index Date_column does not take effect, this query can be changed to compare the scope of date_column.

SELECT text, date_column from  A WHERE date_column >= to_date (' 2016-01-01 ', ' yyyy-mm-dd ') and   Date_column <  to_date (' 2016-01-01 ', ' yyyy-mm-dd ');

C. Build the index to cover your SQL statements as much as possible.

e.g. the following 2 query statements all use the B field in table A

CREATE INDEX A_idx on a (a, b); SELECT ID, a, b from  a WHERE a =: A and   B =: b; SELECT ID, a, b from  a WHERE B =: b;

The index cover the first SELECT statement, but the second SQL statement does not use the index to the fullest extent. You can modify the index to achieve maximum utilization.

CREATE INDEX a_idx on A (b, a);

D. Indexed fields, if the like wildcard filter some strings, the index is not valid at this time.

CREATE INDEX a_idx on A (text); SELECT ID, text from  A WHERE text like '%term% ';

Note: If all the referenced columns in the entire query are included in the index, then the query does not require direct access to the table, and the query speed increases.

2. removing unwanted tables and fields

Do not return unwanted fields to the SQL client.

Do not query for tables that are not needed in SQL statements .

3. Remove External links

4. try not to compare the fields before converting them .

It is more efficient to query the original field directly than after the conversion. For example, this article is the first SQL statement to query 2016 data.

Some simple tips for optimizing database statements

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.