简体   繁体   中英

how to index mysql table on two similar indexes contents?

I have a mysql database table with the following columns : a1, a2, a3, a4, b1, b2, b3 b4.

I need to do heavy search on the records therefore I would like to index the table properly. Sometimes, I need to search (a1,a2,a3,b1,b2), sometimes I need to search (a1,a2,a3,b1,b3,b4).

Shall I do two indexes like

INDEX  search1  (a1,a2,a3,b1,b2),
INDEX  search2  (a1,a2,a3,b1,b3,b4),

Or, is there any good way for me do the INDEX just once and it can work well in both cases?

The answer depends a bit on what your query really looks like. If the first four columns ( a1 , a2 , a3 , b1 ) are highly selective, then either is fine. The index should be used for both queries. Of course, it will be more optimized for the query that matches all the columns.

There isn't a way, though, to have "one" index that handles both sets of columns.

If these sets of columns are the only columns used in the query, then the indexes "cover" their respective queries. This means that all the information for the query is in the index and doesn't need the original data pages. If you want such covering indexes, but only want one, then you could add all the columns to one of the indexes:

a1, a2, a3, b1, b3, b4, b2

The next consideration is how often the table is being updated. Maintaining two indexes requires twice the index time as maintaining one index. Is this an issue for the queries? If so, you might want to lean towards only one index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM