カラム「SetTime」「SiteName」をwhereに、カラム「SetTime」をorder byに使用したクエリを使用しています。
「SetTime」と「SiteName」は複合インデックスなのですがexplainすると「using index」ではなく「using index condition」となります。
ためしにkeyだけを「SetTime」だけのインデックスに指定してみましたが結果は変わりませんでした。
なぜusing indexとならないのでしょうか。
どうすればusing indexにできるのでしょうか。
クエリとexplain
force indexで複合インデックスを指定したクエリ
select * from dhashdiff force index(analyze_join_index) where SetTime<=637410929158399725&& SiteName='コジマネット' order by SetTime limit 1000; +----+-------------+-----------+------------+-------+--------------------+--------------------+---------+------+---------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-----------+------------+-------+--------------------+--------------------+---------+------+---------+----------+-----------------------+ | 1 | SIMPLE | dhashdiff | NULL | range | analyze_join_index | analyze_join_index | 266 | NULL | 1951470 | 10.00 | Using index condition | +----+-------------+-----------+------------+-------+--------------------+--------------------+---------+------+---------+----------+-----------------------+
force indexで複合インデックス、force keyでSetTimeを指定したクエリ
select * from dhashdiff force index(analyze_join_index) force key(settime_index) where SetTime<=637410929158399725&& SiteName='コジマネット' order by SetTime limit 1000; +----+-------------+-----------+------------+-------+----------------------------------+---------------+---------+------+---------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-----------+------------+-------+----------------------------------+---------------+---------+------+---------+----------+-----------------------+ | 1 | SIMPLE | dhashdiff | NULL | range | settime_index,analyze_join_index | settime_index | 8 | NULL | 1951470 | 10.00 | Using index condition | +----+-------------+-----------+------------+-------+----------------------------------+---------------+---------+------+---------+----------+-----------------------+
###create table
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | dhashdiff | CREATE TABLE `dhashdiff` ( `SiteImgUrl` varbinary(2083) NOT NULL, `DHashDif` smallint(5) unsigned NOT NULL, `SetTime` bigint(19) unsigned NOT NULL, `ASIN` varbinary(10) NOT NULL, `SiteName` varchar(64) COLLATE utf8mb4_general_ci NOT NULL, PRIMARY KEY (`SiteImgUrl`,`ASIN`,`SiteName`), KEY `image_asin` (`ASIN`,`SiteImgUrl`), KEY `settime_index` (`SetTime`), KEY `asin` (`ASIN`), KEY `analyze_join_index` (`SetTime`,`SiteName`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci | +-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
回答2件
あなたの回答
tips
プレビュー