In a TENS database search, how to improve the efficiency of the query?

Source: Internet
Author: User
Tags domain server

In a TENS database search, how to improve the efficiency of the query?

1) Database design aspects:


A, to optimize the query, should try to avoid full table scan, first of all should consider the where and order by the columns involved in the index.


b, you should try to avoid the null value of the field in the Where clause to judge, otherwise it will cause the engine to abandon the use of indexes for full table scan, such as: Select ID from t where num is null can set the default value of 0 on NUM, to ensure that the table NUM column does not have a null value, Then query: Select ID from t where num=0

c, not all indexes are valid for the query, SQL is based on the data in the table for query optimization, when the index column has a large number of data duplication, the query may not use the index, such as the table has a field sex,male, female almost half, So even if you build an index on sex, it doesn't work for query efficiency.

D, the index is not the more the better, although the index can improve the efficiency of the corresponding select, but also reduce the efficiency of insert and UPDATE, because the INSERT or update when the index may be rebuilt, so how to build the index needs careful consideration, depending on the situation. The number of indexes on a table should not be more than 6, if too many you should consider whether some of the indexes that are not commonly used are necessary.

E, as far as possible to avoid updating the index data columns, because the order of the index data columns is the physical storage order of table records, once the column value changes will cause the entire table records the order of adjustment, it will consume considerable resources. If the application needs to update the index data columns frequently, you need to consider whether the index should be built as an index.

F, try to use a numeric field, if the field containing only numeric information should not be designed as a character type, which will reduce the performance of query and connection, and increase storage overhead. This is because the engine compares each character in a string one at a time while processing queries and joins, and it is sufficient for a numeric type to be compared only once.

G, as far as possible to use Varchar/nvarchar instead of Char/nchar, because the first variable long field storage space is small, you can save storage space, and secondly for the query, in a relatively small field search efficiency is obviously higher.

H, try to use table variables instead of temporary tables. If the table variable contains a large amount of data, be aware that the index is very limited (only the primary key index).

I, avoid frequent creation and deletion of temporary tables to reduce the consumption of system table resources.

J, temporary tables are not unusable, and they can be used appropriately to make certain routines more efficient, for example, when you need to repeatedly reference a dataset in a large table or a common table. However, for one-time events, it is best to use an export table.

K, when you create a new temporary table, if you insert a large amount of data at one time, you can use SELECT INTO instead of CREATE table to avoid causing a large number of logs to increase speed, and if the amount of data is small, create table and insert to mitigate the resources of the system tables.

L, if a temporary table is used, make sure that all temporary tables are explicitly deleted at the end of the stored procedure, TRUNCATE table first, and then drop table, which avoids longer locking of the system tables.

2) SQL statement aspect:

A, try to avoid using the! = or <> operator in the WHERE clause, or discard the engine for full table scanning using the index.

b, you should try to avoid using or in the WHERE clause to join the condition, otherwise it will cause the engine to abandon using the index for full table scan, such as: Select ID from t where num=10 or num=20 can query: Select ID from t whe Re num=10 union ALL select IDs from T where num=20

C, N, and not in should also be used with caution, otherwise it will cause a full table scan, such as: Select ID from t where num in (three-to-three) for consecutive values, can be used between do not use in the: Select ID from t where n Um between 1 and 3

D, the following query will also cause a full table scan: Select ID from t where name like '%abc% '

E, if you use a parameter in the WHERE clause, it also causes a full table scan. Because SQL is only available at run time

The local variable is parsed, but the optimizer cannot defer the selection of the access plan to the runtime; it must be selected at compile time. However, if an access plan is established at compile time, the value of the variable is still unknown and therefore cannot be selected as an input for the index. The following statement will perform a full table scan: Select ID from t where [email protected] can be changed to force query using index: SELECT ID from T with (index name) where [email Protec Ted

F, try to avoid the expression of the field in the Where clause, which causes the engine to discard the use of the index for a full table scan. For example: Select ID from t where num/2=100 should be changed to: Select ID from t where num=100*2

G, you should try to avoid function operations on the field in the Where clause, which will cause the engine to abandon using the index for full table scanning. such as: Select ID from t where substring (name,1,3) = ' abc ' –name with ABC start ID select ID from t where DATEDIFF (day,createdate, ' 2005-11- 30′) =0– ' 2005-11-30 ' generated ID should be changed to: Select ID from t where name like ' abc% ' select ID from t where createdate>= ' 2005-11-30′a nd createdate< ' 2005-12-1′

H, do not perform functions, arithmetic operations, or other expression operations on the left side of "=" in the WHERE clause, or the index may not be used correctly by the system.

I, do not write some meaningless queries, such as the need to generate an empty table structure: Select Col1,col2 into #t from T where 1=0 such code will not return any result set, but will consume system resources, should be changed to this: CREATE TABLE #t (...)

J, many times replacing in with exists is a good choice: Select num from a where num in (select num from B) is replaced with the following statement: Select Num from a where

Exists (select 1 from b where num=a.num)

K, anywhere do not use SELECT * from T, the specific field list instead of "*", do not return any fields that are not used.

L, avoid using cursors as much as possible because cursors are inefficient and should be considered for overwriting if the cursor is manipulating more than 10,000 rows of data.

M, try to avoid the return of large data to the client, if the amount of data is too large, should consider whether the corresponding demand is reasonable.

N, try to avoid large transaction operation, improve the system concurrency ability.

3) Java aspect: key content

A, as far as possible to create less objects.

b, the position of reasonable pendulum system design. A large number of data operations, and a small number of data operations must be separate. A lot of data manipulation, certainly not the ORM framework is fixed. ,

C. Manipulating data using the JDBC link database

D, control the memory, let the data flow up, not all read to the memory and processing, but the edge of reading edge processing;

E, reasonable use of memory, some data to be cached

How to optimize the database, how to improve the performance of the database?

Answer:

1) Hardware tuning performance is most likely to affect the performance of the disk and network throughput, the solution to expand virtual memory, and ensure that there is enough space to expand, the database server to shut down the unnecessary services, the database server and the primary domain server separate To maximize the throughput of the SQL database server, and to run SQL on a machine with more than one processor.

2) Tuning the database

If the query frequency of the table is relatively high, the index is established, when the index is established, all query search operations on the table are made, the index is indexed according to where selection criteria, as far as possible, the integer key is set as having only one cluster index, the data is physically sequentially on the data page, and the lookup range is shortened. To establish a non-clustered index on all columns that are used frequently by the query, the query can be overwritten as much as possible, but the index is not too large, and the update DELETE INSERT statement needs to be used to maintain a sharp increase in the sales of these indexes; Avoid having too many index keys in the index; avoid columns indexed with large data types ; Ensure that there are a few rows for each index key value.

3) using Stored procedures

In the implementation of the application process, the operation of the database can be implemented by the stored procedure as far as possible through the stored procedure, because the stored procedure is stored on the database server one-time is designed, coded, tested, and reused, the application that needs to perform the task can simply execute the stored procedure, And only returns the result set or the numeric value, thus not only can make the program modularization, simultaneously improves the response speed, reduces the network traffic, and accepts the input through the input parameter, causes the realization of the logical consistency in the application.

4) Application Architecture and algorithms

Establishing a query condition index is only a precondition for speed improvement, and the increase in response speed also relies on the use of indexes. Because people in the use of SQL often fall into a misunderstanding, that is too focused on the results are correct, especially for the data volume is not particularly large database operations, whether to index and use the index of the good or bad response to the program is not very fast, As a result, programmers ignore the possible performance differences between different implementations when writing a program, which is particularly noticeable in large data volumes or in larger or complex database environments, such as online transaction processing OLTP or DSS for decision support systems. In practice, poor SQL often comes from inappropriate index design, non-sufficient connection conditions, and a WHERE clause that is not optimized. After the proper optimization of them, the speed of operation has been significantly improved!

In a TENS database search, how to improve the efficiency of the query?

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.