Collect and summarize database optimization issues

Source: Internet
Author: User
In my work practice, I found that poor SQL statements often come from inappropriate index design, unfilled connection conditions, and unoptimized where clauses. The following section describes the database optimization issues. If you need a friend, you can refer to the following section to summarize these three aspects: selectcount (*) fromrecordwheredate20171201

In my work practice, I found that poor SQL statements often come from inappropriate index design, unfilled connection conditions, and unoptimized where clauses. The following describes the database optimization issues. For more information, see the following: select count (*) from record where date '123'

In my work practice, I found that poor SQL statements often come from inappropriate index design, unfilled connection conditions, and unoptimized where clauses. The following describes the database optimization issues. For more information, see

Person

The following is a summary of these three aspects:

Select count (*) from record where date> '20160301' and date <'20160301' and amount> 19991201 (25 seconds)

Select date, sum (amount) from record group by date (55 seconds)

Select count (*) from record where date> '123' and place in ('bj ', 'sh') (27 seconds)

Select count (*) from record where date> '20160901' and date <'20160901' and amount> 19991201 (14 seconds)

Select date, sum (amount) from record group by date (28 seconds)

Select count (*) from record where date> '2013' and place in ('bj ', 'sh') (14 seconds)

Select count (*) from record where date> '20160301' and date <'20160301' and amount> 19991201 (26 seconds)

Select date, sum (amount) from record group by date (27 seconds)

Select count (*) from record where date> '2013' and place in ('bj, 'sh') (<1 second)

Select count (*) from record where date> '20160301' and date <'20160301' and amount> 19991201 (<1 second)

Select date, sum (amount) from record group by date (11 seconds)

Select count (*) from record where date> '2013' and place in ('bj ', 'sh') (<1 second)

---- 5. Conclusion :----

Select sum (a. amount) from account a, card B where a. card_no = B. card_no (20 seconds)

Select sum (a. amount) from account a, card B where a. card_no = B. card_no and a. account_no = B. account_no (<1 second)

Summary:

Select * from record wheresubstring (card_no, 5378) = '000000' (13 seconds)

Select * from record whereamount/30 <1000 (11 seconds)

Select * from record whereconvert (char (10), date, 112) = '123' (10 seconds)

Analysis:

Select * from record where card_no like '000000' (<1 second)

Select * from record where amount <1000*30 (<1 second)

Select * from record where date = '2014/1/01' (<1 second)

Select count (*) from stuff where id_no in ('0', '1') (23 seconds)

Select count (*) from stuff where id_no = '0' select count (*) from stuff where id_no = '1'

Create proc count_stuff asdeclare @ a intdeclare @ B intdeclare @ c intdeclare @ d char (10) beginselect @ a = count (*) from stuff where id_no = '0' select @ B = count (*) from stuff where id_no = '1' endselect @ c = @ a + @ bselect @ d = convert (char (10), @ c) print @ d

---- Conclusion :----

1. If developers use tables or views of other databases, they must create a View in the current database to perform cross-database operations. It is best not to directly use "databse. dbo. table_name ", because sp_depends cannot display the cross-database table or view used by the SP, it is not convenient to verify.

2. Before submitting the SP, the developer must have used set showplan on to analyze the query plan and perform its own query optimization check.

3. High program running efficiency and application optimization. Pay attention to the following points during SP writing:

A) SQL usage specifications:

I. Avoid large transaction operations as much as possible. Use the holdlock clause with caution to improve the system concurrency capability.

Ii. Try to avoid repeated accesses to the same or several tables, especially tables with large data volumes. You can consider extracting data to a temporary table based on the conditions and then connecting it.

Iii. avoid using a cursor whenever possible because the cursor is inefficient. If the cursor operation contains more than 10 thousand rows of data, it should be rewritten. If the cursor is used, try to avoid table join operations in the cursor loop.

Iv. note that when writing where statements, the order of statements must be taken into account. The order before and after condition clauses should be determined based on the index order and range size, and the field order should be consistent with the index order as much as possible ,, the range is from large to small.

V. do not perform functions, arithmetic operations, or other expression operations on the left side of "=" in the where clause. Otherwise, the system may not be able to correctly use the index.

Vi. use exists instead of select count (1) to determine whether a record exists. The count function is used only when all the rows in the statistical table are used, and count (1) is more efficient than count.

Vii. Try to use "> =" instead of "> ".

Viii. Note the replacement between the or clause and the union clause.

Ix. Pay attention to the data types connected between tables to avoid the connection between different types of data.

X. Pay attention to the relationship between parameters and data types in stored procedures.

Xi. Pay attention to the data volume of insert and update operations to prevent conflicts with other applications. If the data volume exceeds 200 data pages (400 Kb), the system will update the lock and the page lock will be upgraded to the table lock.

B) Specification for indexing:

I. You should consider creating indexes in combination with applications. We recommend that you create a large OLTP table with no more than six indexes.

Ii. Try to use the index field as the query condition, especially the clustered index. If necessary, you can use index index_name to forcibly specify the index.

Iii. Avoid performing table scan when querying large tables. If necessary, create an index.

Iv. when using an index field as a condition, if the index is a joint index, you must use the first field in the index as the condition to ensure that the system uses the index, otherwise, the index will not be used.

V. Pay attention to index maintenance, rebuild indexes periodically, and recompile the stored procedure.

C) use of tempdb:

I. Try to avoid using distinct, order by, group by, having, join, and *** pute, because these statements will increase the burden on tempdb.

Ii. Avoid frequent creation and deletion of temporary tables and reduce the consumption of system table resources.

Iii. when creating a temporary table, if a large amount of data is inserted at one time, you can use select into instead of create table to avoid logs and increase the speed. If the data volume is small, in order to ease the system table resources, we recommend that you first create table and then insert.

Iv. if the temporary table has a large amount of data and requires an index, you should place the process of creating a temporary table and creating an index in a single sub-storage process, in this way, the system can use the index of the temporary table.

V. if a temporary table is used, you must explicitly delete all temporary tables at the end of the stored procedure. First truncate the table and then drop the table, so that the system table can be locked for a long time.

Vi. Use caution when connecting large temporary tables to other large tables to query and modify them, reducing the burden on the system table, because this operation will use the tempdb system table multiple times in one statement.

D) Reasonable algorithm usage:

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.