mysql optimization measures

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 in the where clause! = Or <> operator, otherwise the engine to give up using the index and a full table scan.
3, 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 num from where T = 0
. 4, should be avoided in the where clause to connect or condition will cause the engine to give up using the index and a full table scan, such as :
SELECT from T WHERE ID NUM = NUM = 10 or 20 is
could this query:
SELECT ID NUM WHERE T = 10 from
Union // All the results will not play weight, union will kick weight
SELECT WHERE T from ID = 20 is NUM
. 5, below the inquiry will also lead to a full table scan: select id from t where name like '% abc%' to improve efficiency, can be considered full-text search.
SELECT * from sbl_shop_order od where od.paymentType like '% re%'

6, in should be used with caution and not in, otherwise it will lead to a full table scan, such as:
the SELECT NUM in the above mentioned id from the WHERE t (, 2, 3)
for continuous values, can not use in the between:
the SELECT the above mentioned id NUM BETWEEN. 1 where T from and. 3
. 7, if the parameter used in the where clause, will 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 scans:
the SELECT from the above mentioned id = @ t where NUM NUM
can be changed to force the query using the index:
the SELECT with the above mentioned id from t (index (index name)) where NUM NUM = @
8, should be avoided at where expression clause of the fields operation, which will cause the engine to give up using the index and full table scan. Such as:
SELECT NUM ID from where T / 2 = 100
should read:
SELECT ID from where NUM = 100 * T 2
. 9, should be avoided for a function operation in the fields where clause that will cause the engine to give up using the index a full table scan. Such as:
SELECT id from the substring T WHERE (name, l, 3) = 'abc' - id name beginning with the abc
select id from t where datediff(day,createdate,'2005-11-30')=0--'2005-11-30'生成的id
应改为:
select id from t where name like 'abc%'
select id from t where createdate>='2005-11-30' and createdate<'2005-12-1'

10 Do not make functions, arithmetic operations, or other expressions in the where clause "=" left, or the system may not work properly indexed.
11, as a condition of using the index field, if the index is a composite index, you must use the index to the first field as the conditions to ensure the system uses the index, otherwise the index will not be used, and should as much as possible so that field order is consistent with the order index.
12, 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, SQL queries may not go to use the index, such as a table has a field sex, male, almost half of each female, even if the index built on sex also no effect on the query efficiency.
13, 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 table is best not more than six, if too much you should consider some of the less frequently used to build the index column if necessary.
14, 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.
15, as much 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 is clearly higher.
16, 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.
17, 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).
18, to avoid the frequent create and delete temporary tables, system tables to reduce the consumption of resources.

Guess you like

Origin www.cnblogs.com/longyao/p/11730449.html