SQL Server Optimization Methods

Source: Internet
Author: User
Although the query speed is slow for many reasons, if you optimize the query speed, you can also solve the query speed problem to a certain extent. There are many causes of slow query speed, which are common as follows: 1. no index or no index is used (this is the most common problem of slow query and is a defect in programming) 2. low I/O throughput forms a bottleneck effect. 3. No calculation column is created.

Although the query speed is slow for many reasons, if you optimize the query speed, you can also solve the query speed problem to a certain extent. There are many causes of slow query speed, which are common as follows: 1. no index or no index is used (this is the most common problem of slow query and is a defect in programming) 2. I/O throughput is small, forming a bottleneck effect. 3. No calculation column is created.

Although the query speed is slow for many reasons, if some optimization is adopted, the query problem can be solved to a certain extent.

There are many reasons for slow query speed. The following are common causes:

1. No index or no index is used (this is the most common problem of slow query and is a defect in programming)

2. Low I/O throughput leads to a bottleneck effect.

3. the query is not optimized because no computing column is created.

4. Insufficient memory

5. slow network speed

6. The queried data volume is too large (you can use multiple queries to reduce the data volume in other ways)

7. Lock or deadlock (this is also the most common problem of slow query and is a defect in programming)

8. sp_lock, sp_who, viewed by the active user, because the Read and Write resources are competing.

9. Unnecessary rows and columns are returned.

10. The query statement is not good and is not optimized.

You can optimize the query by using the following methods:

1. Put data, logs, and indexes on different I/O devices to increase the reading speed. In the past, Tempdb can be placed on RAID0, which SQL2000 does not support. The larger the data size (size), the more important it is to increase I/O.

2. vertically and horizontally split the table to reduce the table size (sp_spaceuse)

3. upgrade hardware

4. Create indexes, optimize indexes, optimize access methods, and limit the data volume of the result set based on the query conditions. Note that the fill factor should be appropriate (preferably the default value 0 ). The index should be as small as possible. Use a column with a small number of bytes to create an index (refer to the index creation). Do not create a single index for fields with a limited number of values, such as gender fields.

5. Improve Network Speed

6. Expand the server memory. Windows 2000 and SQL server 2000 support 4-8 GB memory. Configure virtual memory: the virtual memory size should be configured based on services running concurrently on the computer. When running Microsoft SQL Server 2000, you can set the virtual memory size to 1.5 times the physical memory installed on your computer. If you have installed the full-text search feature and intend to run the Microsoft Search Service for full-text indexing and query, consider: set the virtual memory size to at least three times the physical memory installed on the computer. Configure the SQL Server max server memory Server configuration option to 1.5 times the physical memory (half the virtual memory size ).

7. Increase the number of server CPUs, but you must understand that parallel processing requires more resources such as memory than serial processing. Whether to use parallelism or serial travel is automatically evaluated and selected by MsSQL. A single task is divided into multiple tasks and can be run on the processor. For example, if the sort, connection, scan, and group by statements of delayed queries are executed simultaneously, SQL SERVER determines the optimal parallel level based on the system load, complex queries that consume a large amount of CPU are most suitable for parallel processing. However, UPDATE, INSERT, and DELETE operations cannot be processed in parallel.

8. If you use like for query, you cannot simply use index, but the full-text index consumes space. Like 'a % 'uses indexes. like' % a' does not use indexes. When you use like '% a %' to query, the query time is proportional to the total length of the field value. Therefore, the CHAR type cannot be used, but the VARCHAR type is not used. Create a full-text index for a long field value.

9. Separate DB Server and Application Server; Separate OLTP and OLAP

10. The distributed partition view can be used to implement Database Server consortium. A consortium is a group of separately managed servers, but they collaborate to share the processing load of the system. This mechanism of forming Database Server consortium through partition data can expand a group of servers to support the processing needs of large multi-layer Web sites.

1. Before implementing the partition view, a horizontal partition table must be created.

2. After creating a member table, define a distributed partition view on each Member Server, and each view has the same name. In this way, queries that reference the view name of a distributed partition can run on any Member Server. System operations are the same as if each member server has a copy of the original table, but in fact each server has only one member table and a distributed partition view. The data location is transparent to the application.

11. Rebuild the index dbcc reindex, dbcc indexdefrag, shrink data and logs dbcc shrinkdb, and dbcc shrinkfile. set to automatically contract logs. For large databases, do not set Automatic database growth, which will reduce the server performance. There is a great emphasis on the writing of T-SQL, the following lists the common points:

First, the DBMS processes the query plan as follows:

1. query statement lexical and syntax check

2. submit the statement to the query optimizer of the DBMS.

3. optimizer performs algebra optimization and access path optimization

4. A query plan is generated by the Pre-compilation module.

5. Then, it is submitted to the system for processing and execution at the appropriate time.

6. finally, the execution result is returned to the user. Next, let's take a look at the data storage structure of SQL SERVER: the size of a page is 8 K (8060) bytes, and 8 pages are a disk area, store data in the Tree B.

12. Differences between Commit and rollback. Rollback: Roll back all transactions; Commit: Submit the current transaction. There is no need to write transactions in dynamic SQL. If you want to write them, write them out, such as begin tran exec (@ s) commit trans or write dynamic SQL into functions or stored procedures.

13. use the Where clause in the Select statement to limit the number of returned rows to avoid table scanning. If unnecessary data is returned, the server's I/O resources are wasted, increasing the network burden and reducing performance. If the table is large, the table is locked during table scan and other connections are prohibited from accessing the table. Otherwise, the consequences are serious.

14. The SQL statement does not affect execution.

15. Try not to use the cursor. It occupies a large amount of resources. If you need row-by-row execution, try to use non-cursor technology, such as loop on the client, using temporary tables, Table variables, subqueries, and Case statements.

The cursor can be classified according to the extraction options it supports:

1. You must extract rows from the first row to the last row. Fetch next is the only allowed extraction operation and is also the default method.

2. The rollability can be used to randomly extract arbitrary rows anywhere in the cursor.

3. The cursor technology becomes very powerful in SQL2000, and its purpose is to support loops. There are four concurrent options:

1. READ_ONLY: Update cannot be located through the cursor, and there is no lock in the rows that make up the result set.

2. optimistic with valueS: OPTIMISTIC Concurrency Control is a standard part of transaction control theory. Optimistic Concurrency control is used in this case. In the interval between opening the cursor and updating the row, there is only a small chance for the second user to update a row. When a cursor is opened with this option, there is no lock to control the rows, which will help maximize its processing capability. If you try to modify a row, the current value of the row is compared with the value obtained from the last row extraction. If any value changes, the server will know that the other person has updated the row and will return an error. If the value is the same, the server executes the modification and selects this concurrency option.

3. optimistic with row versioning: this OPTIMISTIC concurrency control option is based on ROW version control. Use row version control. The table must have a version identifier, which can be used by the server to determine whether the row is changed after the cursor is read. In SQL Server, this performance is provided by the timestamp data type. It is a binary number that indicates the relative sequence of changes in the database. Each database has a global current timestamp value: @ DBTS. Every time you change a row with a timestamp column in any way, SQL Server first stores the current @ DBTS value in the timestamp column, and then adds the value of @ DBTS. If a table has a timestamp column, the timestamp is recorded as a row. The server can compare the current timestamp value of a row with the timestamp value stored during the last extraction to determine whether the row has been updated. The server does not need to compare the values of all columns. You only need to compare the timestamp column. If the application requires Optimistic Concurrency Based on Row Version Control for tables without a timestamp column, the cursor is optimistic concurrency control based on the value by default.

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.