Design efficient and reasonable MySQL Query statements from the perspective of most system application instances, the query operation occupies the largest proportion in various database operations, the SELECT statement based on the query operation is the most expensive statement in the SQL statement. For example, if the amount of data is accumulated to a certain extent, for example, if the database table information of a bank account is accumulated to millions or even tens of millions of records, it usually takes tens of minutes to scan a full table, even several hours. If you use a better query policy than full table scan, you can usually reduce the query time to several minutes. This shows the importance of the query optimization technology.
During the implementation of the application project, I found that many programmers only pay attention to the gorgeous user interface when developing database applications using some front-end database development tools (such as PowerBuilder and Delphi, the efficiency of query statements is not valued, resulting in low efficiency of the developed application system and serious waste of resources. Therefore, it is very important to design efficient and reasonable query statements. Based on the application example and the database theory, this article introduces the application of the query optimization technology in the real system.
Analyze problems
Many programmers think that query optimization is a task of DBMS (database management system). it has little to do with the SQL statements compiled by the programmers. this is wrong. A good query plan can often increase the program performance by dozens of times. A query plan is a set of SQL statements submitted by the user. a query plan is a set of statements generated after optimization. The process of the DBMS processing the query plan is as follows: after the lexical and syntax check of the query statement is completed, the statement is submitted to the query optimizer of the DBMS, after the optimizer completes algebra optimization and access path optimization, the pre-compilation module processes the statements and generates query plans, and then submits the statements to the system for processing and execution at the appropriate time, finally, return the execution result to the user. The actual database products (such as Oracle and Sybase) use a cost-based optimization method in later versions, this optimization can estimate the cost of different query plans based on the information obtained from the system dictionary table, and then select a better plan. Although the database products have been doing better and better in query optimization, the SQL statements submitted by users are the basis for system optimization, it is hard to imagine that a bad query plan will become efficient after the system is optimized. Therefore, the merits and demerits of the statements written by users are crucial. We will not discuss the system's query optimization. The following describes the solutions to improve the user's query plan.
Solve the problem
The following uses Informix as an example to describe how to improve the user query plan.
1. use indexes reasonably
An index is an important data structure in a database. its fundamental goal is to improve query efficiency. Currently, most database products adopt the ISAM index structure first proposed by IBM. The index should be used properly. the usage principles are as follows:
● The Optimizer automatically generates an index for fields that are frequently connected but not specified as foreign keys.
● Index the columns that are frequently sorted or grouped (that is, group by or order by operations.
● Create a search for columns with different values that are frequently used in conditional expressions. do not create an index for columns with fewer values. For example, in the "gender" column of the employee table, there are only two different values: "male" and "female", so there is no need to create an index. If an index is created, the query efficiency is not improved, but the update speed is greatly reduced.
● If there are multiple columns to be sorted, you can create a compound index on these columns ).
● Use system tools. For example, the Informix database has a tbcheck tool that can be checked on suspicious indexes. On some database servers, the index may be invalid or the reading efficiency may be reduced due to frequent operations. if an index-based query slows down, you can use the tbcheck tool to check the index integrity, fix the issue if necessary. In addition, when a database table updates a large amount of data, deleting and re-indexing can increase the query speed.
2. avoid or simplify sorting
Duplicate sorting of large tables should be simplified or avoided. When indexes can be used to automatically generate output in the appropriate order, the optimizer avoids the sorting step. The following are some influencing factors:
● The index does not contain one or more columns to be sorted;