Multiple-column index would also be useful for single column?

孤街醉人 提交于 2020-01-17 01:23:14

问题


I'm confused about the order on multiple indexes groups.

Take a look at this:

. . . WHERE col1 = ? AND col2 = ? AND col3 = ?

Well in query above I have to create a three-column index on (col1, col2, col3). Now I want to know, that multiple-index is useful elsewhere? For example this query:

. . . WHERE col1 = ? AND col2 = ?

Should I create another two-column index (col1, col2) for this ^ or that three-column index is enough here too?


And what about this query:

. . . WHERE col2 = ? AND col3 = ?

Or this one:

. . . WHERE col2 = ? AND col1 = ?

Do all of them need separated indexes or one multiple index is enough for all of them?

In fact I'm trying to understand how the order of indexes work?


回答1:


MySQL has good documentation on the use of multi-column indexes.

The first thing to note is that your where conditions consist of equal conditions; also type conversions and collation differences can affect the use of indexes. The conditions are also connected by AND. OR impedes the use of an index.

From the perspective of indexing, the order of the columns (with equality) does not matter. So, for these conditions:

WHERE col1 = ? AND col2 = ? AND col3 = ?

Any of these six indexes are optimal: (col1, col2, col3), (col1, col3, col2), (col2, col1, col3), (col2, col3, col1), (col3, col1, col2), and (col3, col2, col1). They "cover" the where clause.

As a side note: at most one inequality can use an index optimally, and that is the last column used in the index.

That where clause can also make use of smaller indexes, such as (col1), or (col2, col3). In this case, some filtering is done using the index and for the rest, the engine needs to look up data in the data pages to fetch the needed fields.

An index can also be used for where conditions that use just a subset of the keys -- but the keys have to be used from left to right. So, an index on (col1, col2, col3) can be used for these conditions:

where col1 = ? and col2 = ? and col3 = ?
where col1 = ? and col2 = ?
where col2 = ? and col1 = ?
where col1 = ?

It won't be used for conditions that don't have col3. And, it can be used (partially) for a condition such as:

where col1 = ? and col2 = ? and col4 = ?



回答2:


As you have index created on (col1, col2, col3) it should work fine for the below cases since for those two condition col1 is the first key in the index

. . . WHERE col1 = ? AND col2 = ? AND col3 = ?

. . . WHERE col1 = ? AND col2 = ?

But . . . WHERE col2 = ? AND col3 = ? OR WHERE col2 = ? AND col1 = ? would not work likewise. In this case, you have to create a separate index though.

But in your case, I feel it would be best if you go for creating separate single index for each column rather composite index.




回答3:


You can use Composite index only from left to right.

so if you have a index on col1, col2, col3

it works for

col1, col2 , col3
col1, col2
col1

but not for

col2, col3
col3

sample

i have create a table with 3 columns and 10000 rows

INSERT INTO tab3
SELECT NULL,seq,seq+1,seq+2 FROM seq_1_to_10000;

EXPLAIN without index

MariaDB []> explain select * from tab3 where col1 = 100 AND col2 = 101 AND col3 = 102;
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | tab3  | ALL  | NULL          | NULL | NULL    | NULL | 9991 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.04 sec)

create index over 3 columns

MariaDB []> ALTER TABLE tab3 add key idx_tab3 (col1,col2,col3);
Query OK, 0 rows affected (0.27 sec)
Records: 0  Duplicates: 0  Warnings: 0

try select again, use only 1 ROW

MariaDB []> explain select * from tab3 where col1 = 100 AND col2 = 101 AND col3 = 102;
+------+-------------+-------+------+---------------+----------+---------+-------------------+------+-------------+
| id   | select_type | table | type | possible_keys | key      | key_len | ref               | rows | Extra       |
+------+-------------+-------+------+---------------+----------+---------+-------------------+------+-------------+
|    1 | SIMPLE      | tab3  | ref  | idx_tab3      | idx_tab3 | 15      | const,const,const |    1 | Using index |
+------+-------------+-------+------+---------------+----------+---------+-------------------+------+-------------+
1 row in set (0.00 sec)

try WHERE with col1 and col2, also works

MariaDB []> explain select * from tab3 where col1 = 100 AND col2 = 101;
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-------------+
| id   | select_type | table | type | possible_keys | key      | key_len | ref         | rows | Extra       |
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-------------+
|    1 | SIMPLE      | tab3  | ref  | idx_tab3      | idx_tab3 | 10      | const,const |    1 | Using index |
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-------------+
1 row in set (0.00 sec)

try WHERE with col1 and col3, not works perfect

MariaDB []> explain select * from tab3 where col1 = 100 AND col3 = 102;
+------+-------------+-------+------+---------------+----------+---------+-------+------+--------------------------+
| id   | select_type | table | type | possible_keys | key      | key_len | ref   | rows | Extra                    |
+------+-------------+-------+------+---------------+----------+---------+-------+------+--------------------------+
|    1 | SIMPLE      | tab3  | ref  | idx_tab3      | idx_tab3 | 5       | const |    1 | Using where; Using index |
+------+-------------+-------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)


来源:https://stackoverflow.com/questions/37630123/multiple-column-index-would-also-be-useful-for-single-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!