Current location - Recipe Complete Network - Complete cookbook of home-style dishes - Does the gender field need an index?
Does the gender field need an index?
Theoretical articles will tell you that fields with high repetition rate are not suitable for indexing. Don't say that there are only two values in the gender field. Users personally test that there are 26 possibilities for using pinyin initials in a field. After adding an index, the speed of using the index is slower than that of not using the index!

A table may involve two data structures (files), one is the table itself, which stores the data in the table, and the other is the index. What is the index? It is to arrange one or several fields (combined index) according to the law, and then attach the physical address of the data to the row (table) where the fields are located. For example, we have a field named "Age". If we want to select all rows of a certain age, we may need to scan the whole table. However, if the index is established at this age, an arrangement will be established in the index according to the age value, so that it can be quickly located in the index without scanning the whole table.

Why gender is not suitable for indexing? Because accessing the index requires extra IO overhead, all you get from the index is the address. If you really want to access the data, you still need the IO table once. If you want to get several data from the 6.5438+0 million rows of data in the table, it is very worthwhile to use the index to locate and access the index quickly. However, if you take 500,000 rows of data, such as gender fields, from 6,543,800 rows of data, then you need to access the index 500,000 times and then the table 500,000 times, and the total cost will not be less than a complete scan of the table directly.

Of course, nothing is absolute. If the gender field is set as the clustered index of the table, it will definitely increase the query speed of this field by about half. A clustered index refers to which field the data in a table is sorted by. Therefore, there can only be one clustered index, and using a clustered index will not pay extra IO overhead. Of course, you must be willing to use such a valuable resource as clustered index in the gender field.

————————————————

Original link:/win32 fanex/article/details/79513857.