在我的表格中发送电子邮件
mysql> explain select * from Emails where email_id IN (2029000006126024, 2029000004564319, 2029000000692079, 2029000004564202);
+----+-------------+---------------+------------+------+----------------------------------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+------+----------------------------------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | Emails | NULL | ALL | Emails_FK1_IDX,Emails_IDX2 | NULL | NULL | NULL | 785984 | 57.70 | Using where |
+----+-------------+---------------+------------+------+----------------------------------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
并且已经为列“email_id”创建了索引,但查询正在针对完整表运行。
并创建表查询
CREATE TABLE `Emails` (
`EMAIL_REPORT_ID` bigint(19) NOT NULL,
`CAMPAIGN_EMAIL_ID` bigint(19) NOT NULL,
`SENT_DATE` bigint(19) DEFAULT NULL,
`EMAIL_ID` bigint(19) NOT NULL,
`BOUNCE` int(10) DEFAULT '0',
`BOUNCED_DATE` bigint(19) DEFAULT NULL,
`SPAM` tinyint(1) DEFAULT '0',
`OPT_OUT` tinyint(1) DEFAULT '0',
`IGNORED` int(10) DEFAULT '0',
`CMP_CONTENT_ID` bigint(19) DEFAULT NULL,
`RESTRICT` int(10) DEFAULT '0',
`TIME_ZONE` varchar(100) DEFAULT NULL,
`GMT_DEVIATION` varchar(10) DEFAULT NULL,
`CMP_COMMENTS_COUNT` int(10) DEFAULT '0',
`IGNORED_BY_TRAP` int(10) DEFAULT '0',
PRIMARY KEY (`EMAIL_REPORT_ID`),
KEY `Emails_FK1_IDX` (`CAMPAIGN_EMAIL_ID`),
KEY `Emails_FK2_IDX` (`CONTACT_ID`),
KEY `Emails_FK3_IDX` (`RECIPIENT_SELECTION_ID`),
KEY `Emails_FK4_IDX` (`CMP_CONTENT_ID`),
KEY `Emails_IDX1` (`SENT_DATE`),
KEY `Emails_IDX2` (`CAMPAIGN_EMAIL_ID`),
KEY `Emails_IDX3` (`EMAIL_REPORT_ID`,`CAMPAIGN_EMAIL_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
请专家们解决这个问题。
答案 0 :(得分:0)
如果估计您搜索的值可能与表的大部分匹配,MySQL将不会使用该索引。
没有记录特定的阈值,但根据我的经验,当您的查询匹配表的大约20%,并且优化器可以从查询逻辑中推断出这一点时,它决定执行表更有效-scan并过滤掉它检查的行而不是使用索引。
如果您不同意,可以使用FORCE INDEX
hint告诉优化器表扫描成本太高,如果有适用的索引,则应该更喜欢使用索引。