Considerations when using index

When to use an index (When should it be used?)
In view of creating an index requires additional disk space (example above requires additional 277,778 disk blocks), and too many indexes can cause problems generated by the file system size limitations, so What are indexed field, under what circumstances the use of the index needs to be carefully considered.

Since the index is only used to accelerate data query, then clearly establish field is only used to output indexes wastes disk space and insertion occurs, the processing time of deletion, so in this case should be avoided. Moreover Given the characteristics of the binary search, data base or independence is very important. Built on the base of the index for the field 2, the data will be split in half, while the base 1000 will be about 1,000 records returned. Low base binary search efficiency will be reduced to a linear order, and query optimizer may be the case when the base is smaller than the number of records in a proportion (e.g. 30%) will avoid using the index table original query directly, so this index under the wasted space.



MySQL, will <, <=, =,> ,> =, BETWEEN, IN , and not like in the beginning of the statement% _ using the index.


 1, a single table of data too, but will affect the speed index

2, after the condition where, order by, group by filtration, etc. Thus, the latter preferably together with an index field. According to the actual situation, select the PRIMARY KEY, UNIQUE, INDEX index, etc., but not better, moderation

  3, the joint inquiry, when the sub-query and other multi-table operations related fields to be indexed

shortcomings index

    says so much index of good things, it is really like the legendary so good Why? Of course, there will be a drawback.

    1. While the index greatly increased query speed, while it will reduce the speed of updating the table, such as table INSERT, UPDATE, and DELETE. Because when you update table, MySQL is not only to save the data, but also save about index file

    2. The establishment of the index file index takes up disk space. Normally this problem is not too serious, but if you're in a big table on a variety of combinations to create the index, the index file will be expanded soon.


How to determine whether you need to create an index?

1, more frequently as the query field of

the know. What is frequently teach it? Analysis of all SQL statements that you execute. The best one they are listed. Then analyzed and found some fields when the query will be used in most of the SQL statement, then the decisive index for him.

2, the uniqueness of the field is not too bad for indexing

What is the uniqueness of the bad fields. The status field, a type field. Only those fields stores a fixed number of values, such as a user login status, status message and the like. This related to the characteristics of the index scan. For example: Find index key values for certain data by A and B, find an article by A consistent data, the data in this page X above, and then continue to scan and found to comply with the data A Y appears in the page above , then the storage engine will discard the data page of X, Y and data stored on the page, until the completion of all the data to find the corresponding a, and then find the B field, discovered X page above there are corresponding data fields B, then he X scans the page again, equal to X page will be scanned twice or even more times. By analogy, it may be repeated several times to read the same data page, discarded in reading, no doubt to the storage engine dramatically increases the burden of IO.

3, update the field too often not suitable for creating an index

When you create an index for this field, when you update the field data again, the database is automatically updated his index, so when the field is updated when too often it is the constant update the index, impact on performance can be imagined. Probably retrieved dozens of fields will be updated once was relatively compliant indexing. If a field is updated many times the same period of time, then the index can not be decisive for him.

4, where conditions do not appear in the fields should be indexed

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/12509637.html