【发布时间】:2011-08-05 05:51:07
【问题描述】:
这里是查询:
select timespans.id as timespan_id, count(*) as num
from reports, timespans
where timespans.after_date >= '2011-04-13 22:08:38' and
timespans.after_date <= reports.authored_at and
reports.authored_at < timespans.before_date
group by timespans.id;
这里是表格定义:
创建表`报告`( `id` int(11) NOT NULL auto_increment, `source_id` int(11) 默认 NULL, `url` varchar(255) 默认为 NULL, `lat` 十进制(20,15) 默认 NULL, `lng` 十进制(20,15) 默认 NULL, `内容`文本, `notes` 文本, `authored_at` 日期时间默认 NULL, `created_at` 日期时间默认 NULL, `updated_at` 日期时间默认 NULL, `数据`文本, `title` varchar(255) 默认 NULL, `author_id` int(11) 默认 NULL, `orig_id` varchar(255) 默认 NULL, 主键(`id`), KEY `index_reports_on_title`(`title`), KEY `index_content_on_reports` (`content`(128)) 创建表`时间跨度`( `id` int(11) NOT NULL auto_increment, `after_date` 日期时间默认 NULL, `before_date` 日期时间默认 NULL, `after_offset` int(11) 默认 NULL, `before_offset` int(11) 默认 NULL, `is_common` tinyint(1) 默认 NULL, `created_at` 日期时间默认 NULL, `updated_at` 日期时间默认 NULL, `is_search_chunk` tinyint(1) 默认为 NULL, `is_day` tinyint(1) 默认 NULL, 主键(`id`), KEY `index_timespans_on_after_date`(`after_date`), KEY `index_timespans_on_before_date`(`before_date`)这里是解释:
+----+-------------+------------+-------+---------- -------------------------------------------------- --+-------------------+---------+------ -+--------+---------------------------------------------------- --------+ |编号 |选择类型 |表|类型 |可能的键 |关键 | key_len |参考 |行 |额外 | +----+-------------+------------+-------+---------- -------------------------------------------------- --+-------------------+---------+------ -+--------+---------------------------------------------------- --------+ | 1 |简单 |时间跨度 |范围 | index_timespans_on_after_date,index_timespans_on_before_date | index_timespans_on_after_date | 9 |空 | 84 |使用哪里;使用临时的;使用文件排序 | | 1 |简单 |报告 |全部 |空 |空 |空 |空 | 183297 |使用位置 | +----+-------------+------------+-------+---------- -------------------------------------------------- --+-------------------+---------+------ -+--------+---------------------------------------------------- --------+这是我在 authored_at 上创建索引后的解释。如您所见,索引实际上并没有被使用(我认为...)
+----+-------------+------------+-------+---------- -------------------------------------------------- --+-------------------+---------+------ -+--------+---------------------------------------------------- ---------+ |编号 |选择类型 |表|类型 |可能的键 |关键 | key_len |参考 |行 |额外 | +----+-------------+------------+-------+---------- -------------------------------------------------- --+-------------------+---------+------ -+--------+---------------------------------------------------- ---------+ | 1 |简单 |时间跨度 |范围 | index_timespans_on_after_date,index_timespans_on_before_date | index_timespans_on_after_date | 9 |空 | 86 |使用哪里;使用临时的;使用文件排序 | | 1 |简单 |报告 |全部 | index_reports_on_authored_at |空 |空 |空 | 183317 |检查每条记录的范围(索引图:0x8)| +----+-------------+------------+-------+---------- -------------------------------------------------- --+-------------------+---------+------ -+--------+---------------------------------------------------- ---------+报告表中有大约 142k 行,而时间跨度表中的行数要少得多。
现在查询大约需要 3 秒。
奇怪的是,如果我在reports.authored_at 上添加索引,它实际上会使查询慢得多,大约20 秒。我原以为它会做相反的事情,因为它可以很容易地找到范围两端的报告,然后把其余的扔掉,而不必检查所有报告。
有人可以澄清一下吗?我被难住了。
【问题讨论】:
-
请说明结果和表格定义,tkx
-
reports.authored_at上确实应该有一个索引。 EXPLAIN 在该列被索引后会说什么?
标签: mysql