The index will lead to failure of sentence

1, when used like keyword fuzzy query,% placed in front of the index does not work, only "%" not in the first position, the index take effect (like '% text' - the index does not work)
2, using the joint index when only the query used in these fields in the first field, the index take effect
3, use the OR keyword query, the query statement OR keywords only, and both before and after the conditions of the OR when the column is an index, which take effect, otherwise the index does not take effect.
4, to avoid the use in the where clause! = Or <> operator, otherwise the engine will give up using the index and a full table scan.
5, query optimization, should try to avoid full table scan, you should first consider the establishment of an index on where and order by the column involved.
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 NUM ID from where T / 2 = 100
  should read:
  SELECT ID from where NUM = 100 * T 2
. 7, to avoid a function of the field operating in the where clause, will cause the engine to give up using the index and full table scan.
8. Do not functions, arithmetic operations, or other expressions in the where clause "=" left, or the system may not work properly indexed.
9, not all indexes are valid query, sql query optimization is performed according to the data in the table, when the index lists a large number of duplicate data, sql query not to take advantage of the index, such as a table has a field
  sex, male , female almost half, even if established in the sex index also no effect on the query efficiency.
10, 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 to insert or update when will rebuild the index, so the need to carefully consider how to build the index, 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.
11, make use of numeric field, if only with numeric information field as much as possible 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.
12, mysql query using only one index where clause so if the index has been used, then order by the column will not use the index.
   So do not use the default database sorting operation can meet the requirements of the case of the sort, try not to sort multiple columns contain, if these columns need to build the best composite index.
13 is, order by the index, the problem does not work (in addition to the primary key index):
  1, if only the select query index field, order by index field index will be used, or else the whole table arrangement;

  2, if there are conditions where, for example, where vtype = 1 order by vtype asc. Order by this index will be used!

Two, four, index

PRIMARY, INDEX, UNIQUE these three is a Class
PRIMARY primary key. It is unique and can not be empty.
INDEX index, ordinary
UNIQUE unique index. It does not allow duplicate.
FULLTEXT is a full-text index for an article, retrieve textual information.

Third, the commonly used SQL Optimization:
1. Optimize group by statement
default, MySQL for all the group by col1, col2 sort. This order is specified in the query by col1, col2 similar. If the query includes group by the user consumption but want to avoid the sort of result, you can use the sort order by null prohibited
2. In some cases, you can use the connection to alternative sub-queries. Because using join, MySQL does not need to create a temporary table in memory.
3. If you want to use the index in the query containing or in, each condition between or columns must use the index, if the index does not, you should consider increasing the index
select * from table where condition 1 = '' or condition 2 = 'tt'

----------------

Original link: https: //blog.csdn.net/qq_42695926/article/details/83900198

Guess you like

Origin www.cnblogs.com/xiely/p/12036574.html