Original: http://database.51cto.com/art/201011/233234.htm
MySQL single-column index is what we often see in MySQL database, the difference between MySQL single-column index and composite index may be a lot of people do not know very well, the following for you to analyze the main differences, for your reference to learn.
To visually compare the two, build a table:
CREATE TABLEMyindex (I_testidINT not NULLAuto_increment, Vc_nameVARCHAR( -) not NULL, vc_cityVARCHAR( -) not NULL, I_ageINT not NULL, I_schoolidINT not NULL,PRIMARY KEY(I_testid));
There are 5 records of Vc_name= "Erquan" in these 10,000 records, except that the City,age,school combination is different.
Look at this T-sql:
SELECT from WHERE vc_name='erquan' and vc_city=' Zhengzhou ' and I_age=
First consider building a MySQL single-column index:
An index was established on the Vc_name column. When executing T-SQL, MYSQL quickly locks the target on the 5 records of Vc_name=erquan and takes it out to a middle result set. In this result set, the first rule out vc_city not equal to "Zhengzhou" record, and then exclude i_age not equal to 25 of the record, and finally filtered out the only qualified records.
Although the index is built on the vc_name, MySQL does not have to scan the whole table when querying, but the efficiency is improved, but there is a certain distance from our request. Similarly, the MySQL single-column indexes established separately in vc_city and i_age are similar in efficiency.
To further extract the efficiency of MySQL, it is necessary to consider building a composite index. is to build the Vc_name,vc_city,i_age into an index:
ALTER TABLE ADD INDEX name_city_age (Vc_name (ten
When building a table, the length of the vc_name is 50, why use 10 here? Because the length of the name does not typically exceed 10, this speeds up the index query, reduces the size of the index file, and increases the update speed of the INSERT.
When executing T-SQL, MySQL does not need to scan any records to find a unique record.
There must be someone to ask, if you set up a single-column index on the vc_name,vc_city,i_age, so that the table has 3 single-column index, query and the above combination index efficiency? Very different, far below our combined index. Although there are three indexes at this point, MySQL can only use one of the single-column indexes that it considers to be the most efficient.
The establishment of such a composite index is actually equivalent to establishing a separate
1. Vc_name,vc_city,i_age 2. vc_name,vc_city
Such a combination of three indexes! Why is there no such combination index as vc_city,i_age? This is because the MySQL composite index is the result of the "leftmost prefix". The simple understanding is only from the left to the beginning of the combination. It is not just that the combined index is used for queries that contain these three columns, and several of the following T-SQL is used:
SELECT * from Myindex whree vc_name= and vc_city=SELECT* from Myindex whree vc_name=
And the next few are not used:
SELECT * from Myindex whree i_age= vc_city=SELECT* from Myindex whree vc_city=
MySQL single-column index and composite index difference introduction (GO)