我想运行这些查询:
select url from weixin_kol_status where created_at>'2015-12-11 00:00:00' and created_at<'2015-12-11 23:59:59';
和
select url from weixin_kol_status where userid in ('...') and created_at>'2015-12-11 00:00:00' and created_at<'2015-12-11 23:59:59';
...使用此表定义:
CREATE TABLE `weixin_kol_status` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `url` varchar(512) NOT NULL, `created_at` datetime NOT NULL, `title` varchar(512) NOT NULL DEFAULT '', `text` text, `attitudes_count` int(11) NOT NULL DEFAULT '0', `readcount` int(11) NOT NULL DEFAULT '0', `reposts_count` int(11) NOT NULL DEFAULT '0', `comments_count` int(11) NOT NULL DEFAULT '0', `userid` varchar(32) NOT NULL, `screen_name` varchar(32) NOT NULL, `type` tinyint(4) NOT NULL DEFAULT '0', `ext_data` text, `is_topline` tinyint(4) NOT NULL DEFAULT '0', `is_business` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `idx_url` (`url`(255)), KEY `idx_userid` (`userid`), KEY `idx_name` (`screen_name`), KEY `idx_created_at` (`created_at`) ) ENGINE=InnoDB AUTO_INCREMENT=328727437 DEFAULT CHARSET=utf8 | rows = 328727437;
查询需要几分钟.如何优化查询?我怎样才能使用覆盖指数?
执行计划是:
explain select id from weixin_kol_status where created_at>='2015-12-11 00:00:00' and created_at<='2015-12-11 23:59:59'\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: weixin_kol_status type: range possible_keys: idx_created_at key: idx_created_at key_len: 5 ref: NULL rows: 1433704 Extra: Using where; Using index 1 row in set (0.00 sec)
和
explain select id from weixin_kol_status where created_at='2015-12-11 00:00:00'\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: weixin_kol_status type: ref possible_keys: idx_created_at key: idx_created_at key_len: 5 ref: const rows: 1 Extra: Using index 1 row in set (0.00 sec)
但为什么第一个查询Extra: Using where; Using index
,第二个查询Extra: Using index
.第一个查询没有使用覆盖索引吗?