我有以下表格(为简洁起见,删除了一些字段):
CREATE TABLE `wp_bp_activity` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`type` varchar(75) NOT NULL,
`is_activity_comment` tinyint(1) DEFAULT NULL,
`hide_sitewide` tinyint(1) DEFAULT '0',
`is_spam` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `type` (`type`),
KEY `hide_sitewide` (`hide_sitewide`),
KEY `is_spam` (`is_spam`),
KEY `is_activity_comment` (`user_id`,`is_spam`,`hide_sitewide`,`is_activity_comment`)
)
和
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) NOT NULL DEFAULT '',
`user_pass` varchar(64) NOT NULL DEFAULT '',
`display_name` varchar(250) NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
UNIQUE KEY `user_login` (`user_login`),
KEY `user_login_key` (`user_login`)
)
当我运行此查询时:
EXPLAIN SELECT DISTINCT a . * , u.user_login, u.display_name
FROM wp_bp_activity a
LEFT JOIN wp_users u ON a.user_id = u.ID
WHERE a.is_spam =0
AND a.hide_sitewide =0
AND a.is_activity_comment =0
ORDER BY a.date_recorded DESC
我得到这个结果:
id|select_type|table|type |possible_keys |key |key_len|ref |rows |Extra
1 |SIMPLE |a |ref |hide_sitewide,is_spam,is_activity_comment|hide_sitewide|2 |const |97718|Using where; Using temporary; Using filesort|
1 |SIMPLE |u |eq_ref|PRIMARY |PRIMARY |8 |dbname.a.user_id|1|
我对此感到困惑。为什么查询使用hide_sitewide
索引?为什么不使用is_activity_comment
索引,尤其是当 where 子句中的许多字段存在于 `is_activity_comment` 索引中时?