我不明白为什么一个字段上的相同查询使用索引,而另一个字段上却不使用索引。这两个字段都有索引。一个字段是INT,另一个字段是TIMESTAMP。某种魔术。救命!
这是表格:
CREATE TABLE `fb_posts` (
`postID` BIGINT(20) NOT NULL AUTO_INCREMENT,
`url` VARCHAR(255) NOT NULL,
`time` INT(11) NOT NULL,
`partnerID` INT(11) NOT NULL,
`aTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`postID`),
INDEX `time` (`time`),
INDEX `aTime` (`aTime`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPRESSED
AUTO_INCREMENT=67640377;
说明查询:
1。
mysql> explain SELECT
-> `fb_posts`.`partnerID`
-> FROM
-> `fb_posts`
-> WHERE
-> `fb_posts`.`time` > 1551107400
-> AND `fb_posts`.`time` <= 1551108000
->
-> ;
+----+-------------+----------+-------+---------------+------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+------+---------+------+------+-----------------------+
| 1 | SIMPLE | fb_posts | range | time | time | 4 | NULL | 1 | Using index condition |
+----+-------------+----------+-------+---------------+------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
2。
mysql> explain SELECT
-> `fb_posts`.`partnerID`
-> FROM
-> `fb_posts`
-> WHERE
-> `fb_posts`.`aTime` > 1551107400
-> AND `fb_posts`.`aTime` <= 1551108000
-> ;
+----+-------------+----------+------+---------------+------+---------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+----------+-------------+
| 1 | SIMPLE | fb_posts | ALL | aTime | NULL | NULL | NULL | 69567867 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+----------+-------------+
1 row in set, 4 warnings (0.00 sec)
为什么?
答案 0 :(得分:0)
优化器创建方式。他们使用统计信息来找出 最适合此查询。
尝试查看统计信息并阅读手册“ PROCEDURE ANALYSE()”
SELECT * FROM `fb_posts` PROCEDURE ANALYSE();