私は、完了までに約14秒かかっているかなり単純なクエリを持っています。ここではここクエリmySQLの特定のクエリのインデックスを最適化する
SELECT *
FROM opportunities
WHERE cid = 7785
AND STATUS != 4
AND otype != 200
AND links > 0
AND ontopic != 'F'
ORDER BY links DESC
LIMIT 0, 100;
は、テーブルスキーマが
CREATE TABLE `opportunities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` int(11) NOT NULL,
`url` varchar(900) CHARACTER SET utf8 NOT NULL,
`status` tinyint(4) NOT NULL,
`links` int(11) NOT NULL,
`otype` int(11) NOT NULL,
`reserved` tinyint(4) NOT NULL,
`ontopic` varchar(3) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `cid` (`cid`,`url`),
KEY `cid1` (`cid`),
KEY `url` (`url`),
KEY `otype` (`otype`),
KEY `reserved` (`reserved`),
KEY `ontopic` (`ontopic`),
KEY `status` (`status`),
KEY `links` (`links`),
KEY `ontopic_links` (`ontopic`,`links`),
KEY `cid_status_otype_links_ontopic` (`cid`,`status`,`otype`,`links`,`ontopic`)
) ENGINE=InnoDB AUTO_INCREMENT=13022832 DEFAULT CHARSET=latin1
です
...私は場所に正しいインデックスを持っていると思うが、私はわかりませんEXPLAINコマンドの結果は次のとおりです
id: 1
select_type: Simple
table: opportunities
partitions: null
type: range
possible_keys: cid,cid1,otype,ontopic,status,links,ontopic_links,cid_status_otype_links_ontopic
key: links
keylen: 4
ref: null
rows: 1531552
filtered: 0.33
Extra: Using index condition; Using where
思考/質問
私はクエリを実行するために、「リンク」キーを使用していることがそれを正しく読んでいますか?なぜ私のクエリのすべての条件をカバーするcid_status_otype_links_ontopicのようなより完全なインデックスを使用しないのですか?
ありがとうございます!
としては、あなたがLIMIT 0100を削除するクエリに一致30961件の結果があります
を要求しました。興味深いことに、 "count()"コマンドはほぼ即座に戻ります。
このクエリから返されるレコード数はいくつですか? 'LIMIT'節を削除し、' COUNT(*) 'を実行して、戻ったレコード数を報告してください。 –
応答ありがとう!私は30,961結果を返します。 – user2648990