1

テーブル (visit_url) に対する次のクエリで mysql がインデックスを使用していないのはなぜでしょうか。

> describe select sv.url from visit_url sv, result_query rq where rq.result_id = sv.result_id and rq.user_id = 'fred';
+----+-------------+-------+--------+---------------+---------+---------+------------------------+--------+-------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref                    | rows   | Extra       |
+----+-------------+-------+--------+---------------+---------+---------+------------------------+--------+-------------+
|  1 | SIMPLE      | vu    | ALL    | result_id     | NULL    | NULL    | NULL                   | 506553 |             |
|  1 | SIMPLE      | rq    | eq_ref | PRIMARY       | PRIMARY | 32      |           vu.result_id |      1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+------------------------+--------+-------------+

私のインデックスは次のとおりです。

mysql> show index in visit_url;
+-----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| visit_url |          1 | result_id |            1 | result_id   | A         |      168851 |     NULL | NULL   |      | BTREE      |         |               |
| visit_url |          1 | url       |            1 | url         | A         |      253276 |     NULL | NULL   | YES  | BTREE      |         |               |
+-----------+------------+-----------+--------------+-------------+-----------+-------------+-------- --+--------+------+------------+---------+---------------+

mysql> show index in result_query;
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table        | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| result_query |          0 | PRIMARY   |            1 | result_id   | A         |     2718272 |     NULL | NULL   |      | BTREE      |         |               |
| result_query |          1 | query_idx |            1 | query       | A         |      271827 |     NULL | NULL   |      | BTREE      |         |               |
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

visit_url には 506k のエントリがあり、result_query には 2mil のエントリがあります

どうもありがとう!

4

2 に答える 2

0

次のように、where 句から分離された明示的な結合を使用してクエリを記述してみてください。

select sv.url from visit_url sv 
  join result_query rq on rq.result_id = sv.result_id
  where rq.user_id = 'fred';
于 2013-06-28T00:16:24.900 に答える