【发布时间】:2016-07-23 21:51:41
【问题描述】:
这是我的桌子:
CREATE TABLE IF NOT EXISTS `test_dates` (
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `test_log` (
`id` int(10) unsigned NOT NULL,
`timest` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `test_dates`
ADD PRIMARY KEY (`date`);
ALTER TABLE `test_log`
ADD PRIMARY KEY (`id`),
ADD KEY `emissione` (`timest`);
我有这个查询来计算每个日期的日志:
SELECT d.date, COUNT(l.id)
FROM test_dates d
LEFT JOIN test_log l ON l.timest>=d.date AND l.timest<d.date + INTERVAL 1 DAY
GROUP BY d.date
test_dates 表在日期列中编入索引,test_log 表在时间列中编入索引。
但是在解释这个查询时,我得到了查询类型“ALL”和 NULL 键。
+-----+--------------+--------+-------------+--------+----------------+----------+----------+------+--------+-----------+------------------------------------------------+--+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | |
+-----+--------------+--------+-------------+--------+----------------+----------+----------+------+--------+-----------+------------------------------------------------+--+
| 1 | SIMPLE | d | NULL | index | PRIMARY | PRIMARY | 3 | NULL | 705 | 100.00 | Using index | |
| 1 | SIMPLE | l | NULL | ALL | emissione | NULL | NULL | NULL | 98256 | 100.00 | Range checked for each record (index map: 0x2) | |
+-----+--------------+--------+-------------+--------+----------------+----------+----------+------+--------+-----------+------------------------------------------------+--+
为什么mysql不能使用表索引?
日志表大约有100000行,查询很慢。
【问题讨论】:
-
大概是查询慢了?至少,有关查询性能的问题需要针对所有相关表(最好也是样本数据)的 CREATE 语句以及 EXPLAIN 的结果。
标签: mysql optimization indexing