このクエリは、特定の日付範囲内のユニーク ユーザー セッションをすべて選択します。
select distinct(accessid) from accesslog where date > '2009-09-01'
次のフィールドにインデックスがあります。
- アクセスした
- 日にち
- いくつかの他のフィールド
説明は次のようになります。
mysql> explain select distinct(accessid) from accesslog where date > '2009-09-01';
+----+-------------+-----------+-------+----------------------+------+---------+------+-------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+----------------------+------+---------+------+-------+------------------------------+
| 1 | SIMPLE | accesslog | range | date,dateurl,dateaff | date | 3 | NULL | 64623 | Using where; Using temporary |
+----+-------------+-----------+-------+----------------------+------+---------+------+-------+------------------------------+
mysql> explain select distinct(accessid) from accesslog;
+----+-------------+-----------+-------+---------------+----------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+----------+---------+------+---------+-------------+
| 1 | SIMPLE | accesslog | index | NULL | accessid | 257 | NULL | 1460253 | Using index |
+----+-------------+-----------+-------+---------------+----------+---------+------+---------+-------------+
date 句を含むクエリで accessid インデックスを使用しないのはなぜですか?
特定の日付範囲で個別の accessid のクエリを高速化するために使用できる他のインデックスはありますか?
編集 - 解決
列幅をaccessid
varchar 255 から char 32 に減らすと、クエリ時間が最大 75% 改善されました。
date+accessid
インデックスを追加しても、クエリ時間には影響しませんでした。