如何使mysql多列索引快速执行

时间:2015-04-03 18:50:52

标签: mysql

我有一个我想要应用索引的查询.Query是

select * from table where (id1='12' AND id2='17') OR (id1='17' AND id2='12');

现在我尝试使用(id1,id2)应用索引但是解释查询显示“ALL”为“type” 当我尝试(id2,id1)或id1和id2仍然响应是相同的。所以如何索引id1和id2来处理上面的查询。

mysql> explain select * from bothTable where (id1=12 and id2=17) OR (id1=17 and id2=12);
+----+-------------+------------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table            | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+------------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | bothTable | ALL  | NULL          | NULL | NULL    | NULL |   67 | Using where |
+----+-------------+------------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> create index index_id1 ON bothTable (id1,id2);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select * from bothTable where (id1=12 and id2=17) OR (id1=17 and id2=12);
+----+-------------+------------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table            | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+------------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | bothTable | ALL  | index_id1     | NULL | NULL    | NULL |   67 | Using where |
+----+-------------+------------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

使用union它会给我低于输出:

+----+--------------+------------------+------+---------------+------+---------+------+------+-------------+
| id | select_type  | table            | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+--------------+------------------+------+---------------+------+---------+------+------+-------------+
|  1 | PRIMARY      | bothTable | ALL  | index_id1     | NULL | NULL    | NULL |   67 | Using where |
|  2 | UNION        | bothTable | ALL  | index_id1     | NULL | NULL    | NULL |   67 | Using where |
| NULL | UNION RESULT | <union1,2>       | ALL  | NULL          | NULL | NULL    | NULL | NULL |             |
+----+--------------+------------------+------+---------------+------+---------+------+------+-------------+
3 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:0)

create index id1_id2 on table(id1, id2);

另外,不要把&#39;&#39;如果你的id1和id2列是整数列,那么请尝试这样:

select * from table where (id1=12 AND id2=17) OR (id1=17 AND id2=12);

示例:

mysql> explain select * from ff where (id1 = '12' and id2 = '17') or (id1 = '17' and id2 = '12');
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | ff    | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> create index id1_id2 on ff(id1,id2);
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select * from ff where (id1 = 12 and id2 = 17) or (id1 = 17 and id2 = 12);
+----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                    |
+----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
|  1 | SIMPLE      | ff    | index | id1_id2       | id1_id2 | 10      | NULL |    7 | Using where; Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
1 row in set (0.00 sec)

修改 因为那似乎仍然不起作用,试试这个:

explain 
select * from ff where (id1 = 12 and id2 = 17)
union
select * from ff where (id1 = 17 and id2 = 12);