MySQL query data as well as the day when the number of queries to enhance the speed

select * from 表名 where to_days(字段名) = to_days(now())

First, the database design
1, query optimization, should try to avoid full table scan, you should first consider indexing by the column involved in where and order;

2, should be avoided to a null value is determined fields in the where clause, will cause the engine to give up using the index and a full table scan, such as: select id from t where num is null may be provided on a default value of 0 num, to ensure num table column value is not null, then this query: select id from t where num = 0;

3, not all indexes are valid query, SQL query optimization is performed according to data in the table, when the index lists a large number of duplicate data, the query may not be to take advantage of the index, such as a table has a field sex, male, female almost half each, even if the sex was built in the query efficiency index also no effect;

4, the index is not possible, the corresponding index can certainly improve the efficiency of select, but also reduces the efficiency of insert and update, because it is possible when the insert or update will rebuild the index, the index needs to be carefully considered how to build, As the case may be. An index number of a table is best not more than six, if too much you should consider some of the less frequently used to build the index of the column if necessary;

5, should be avoided as much as possible to update the index data columns, because the order of the index column is a physical data storage order recorded in the table, once the column will result in the adjustment value changes in the order of recording the entire table, will consume considerable resources. If applications require frequent updates index data columns, you need to consider whether the index should be built for the index;

6, make use of numeric fields, numeric fields containing information if only so as not to design for the character, which will reduce the performance of queries and connections, and will increase the storage overhead. This is because the engine when processing queries and connections one by comparing each character in the string, and for numeric comparison purposes only once is enough;

7, as far as possible the use of varchar / nvarchar instead of char / nchar, because first of all variable-length fields small storage space, you can save storage space, followed by the query, in a relatively small field of search efficiency obviously higher;

8, instead make use of a temporary table variable table. If the table variable contains a large amount of data, please note that the index is very limited (only the primary key index);

9, avoid the frequent create and delete temporary tables, system tables to reduce the consumption of resources;

10, the temporary table is not unusable, they can make appropriate use of certain routines more effective, e.g., when a reference to a data set to be repeated a large table or when the table used. However, for a one-time event, it is best to use export table;

11, in the new temporary table, if one inserts a large amount of data, you can use select into instead of create table, to avoid a large number of log, in order to increase speed; if small data resources to ease the system tables, you should create table, and INSERT;

12, if you use the temporary table, make sure all the temporary table explicit deleted at the end of the stored procedure, first truncate table, then drop table, to avoid locking the system tables a long time.

Two, SQL statements, aspect
1, should be avoided in the where clause = or <> operator, otherwise the engine to give up using the index and a full table scan!;

2, should be avoided or used to join condition in the where clause, will cause the engine to give up using the index and a full table scan, such as: select id from t where num = 10 or num = 20 can do a query: select id from t where num = 10 union all select id from t where num = 20;

3, in should be used with caution and not in, otherwise it will lead to a full table scan, such as: select id from t where num in (1,2,3) for continuous values ​​between can not use in the: select id from t where num between 1 and 3;

4, the following query will result in a full table scan: select id from t where name like '% abc%'

5, if a parameter in the where clause would lead to a full table scan. Because SQL at runtime will only resolve local variables, but the optimizer can not defer the choice of access plan to run; it must choose at compile time. If, however, establish access plan at compile time, the value of the variable is unknown, and therefore can not be used as an index entry selected. As the following statement will perform full table scan: select id from t where num = @ num can be changed to force the query using the index: select id from t with (index (index name)) where num = @ num;

6, should be avoided for operating fields in the where clause expression, which will cause the engine to give up using the index and full table scan. Such as: select id from t where num / 2 = 100 should be changed to: select id from t where num = 100 * 2;

7, should be avoided for a function operation in the fields where clause that will cause the engine to give up using the index and full table scan. Such as: select id from t where substring (name, 1,3) = 'abc'-name beginning with the abc id, select id from t where datediff (day, createdate,' 2005-11-30 ') = 0-' 2005-11-30 'id generated should read: select id from t where name like' abc% 'select id from t where createdate> =' 2005-11-30 'and createdate <' 2005-12-1 '

8. Do not functions, arithmetic operations, or other expressions in the where clause "=" left, or the system may not work properly indexed.

9, do not write the query does not make sense, such as the need to create an empty table structure: select col1, col2 into #t from t where 1 = 0 This code does not return any result sets, but consumes system resources, should be changed like this: create table #t (...)

10, often used in place exists is a good choice: select num from a where num in (num from b select) is replaced with the following statement: select num from a where exists (select 1 from b where num = a.num )

11, anywhere Do not use select * from t, with a specific list of fields instead of "*", do not return any of the fields with less than.

12, try to avoid using a cursor, because the poor efficiency of the cursor, if the cursor operation more than 10,000 lines, you should consider rewriting.

13, returned to the client to avoid large amounts of data, if the data is too large, you should consider the corresponding demand is reasonable.

14, try to avoid large transaction operations, improve system concurrency.

Three, Java aspects (key elements)
1, as little as possible made objects;

2, put the location and reasonable system design. Much data, and a small amount of data manipulation must be separated. A large amount of data manipulation, certainly not get the ORM framework;

3, the link database using JDBC operation data;

4, control memory, so that data streams together, but not all read memory reprocessing, but side edge reading process;

5, the rational use of memory to cache some data;

Fourth, how to optimize the database, how to improve database performance?

1, the hardware performance tuning

Most likely to affect the performance of disk and network throughput, the solution is to expand the virtual memory, and to ensure that there is enough space can be expanded; the unnecessary services on the database server is shut off; to separate the database server and the primary domain server; the SQL throughput is the maximum tone database server; SQL running on a machine having one or more processors.

2, tune the database

If the query frequency table is relatively high, the index; indexing, search operations tried all queries to the table, where indexed in accordance with the selection criteria, try to establish that there is one and only one clustered index key integer the data on the physical order on the data page, shorten look, the establishment of a non-clustered indexes for all columns that are frequently used in the query, the query can cover the maximum; but the index is not too much, execute UPDATE DELETE INSERT statement need for maintenance overhead of these indexes increased dramatically; avoid too many index keys in the index; avoid the use of large data type as the index; there are a few key index to ensure that every row.

3, using stored procedures (note: Alibaba specification has been developed specifically prohibit the use of stored procedures, just listed here, not as an optimization method!)

Implementation of the application, can be employed in the operation of the database stored procedures as much as possible implemented by a stored procedure, because the stored procedure is located on the database server is a disposable design, coding, testing, and reused need to be performed the application task can be performed simply stored procedure, and returns only the result or value set, so that not only allows a modular program, while improving the response speed, reduce network traffic, and accepts an input via the input parameters, such that in the application logic to complete consistency of implementation.

4, application structure and algorithm

Build query the index is just a prerequisite to improve the speed, response speed also depends on the use of the index. Because people in the use of SQL often fall into a misunderstanding that too focused on results obtained are correct, especially when the amount of data is not particularly large database operations, indexing, and whether to use an index of the quality and speed of response to the program small, so the programmer when writing the program ignores the possible differences between the performance of different implementations of this difference in performance when large amounts of data, or particularly large or complex database environments (e.g., online transaction processing OLTP or decision support system DSS), which has been particularly evident. In practice found that SQL is often undesirable from an inappropriate index design, the connection is not fully optimized conditions and not where clause. After they are proper optimization, its speed has been noticeably improved!

 

Guess you like

Origin www.cnblogs.com/itzyz/p/11082037.html