【发布时间】:2011-03-29 12:38:36
【问题描述】:
有一个查询在 250,000 行表上耗时过长。我需要加快速度:
create table occurrence (
occurrence_id int(11) primary key auto_increment,
client_id varchar(16) not null,
occurrence_cod varchar(50) not null,
entry_date datetime not null,
zone varchar(8) null default null
)
;
insert into occurrence (client_id, occurrence_cod, entry_date, zone)
values
('1116', 'E401', '2011-03-28 18:44', '004'),
('1116', 'R401', '2011-03-28 17:44', '004'),
('1116', 'E401', '2011-03-28 16:44', '004'),
('1338', 'R401', '2011-03-28 14:32', '001')
;
select client_id, occurrence_cod, entry_date, zone
from occurrence o
where
occurrence_cod = 'E401'
and
entry_date = (
select max(entry_date)
from occurrence
where client_id = o.client_id
)
;
+-----------+----------------+---------------------+------+
| client_id | occurrence_cod | entry_date | zone |
+-----------+----------------+---------------------+------+
| 1116 | E401 | 2011-03-28 16:44:00 | 004 |
+-----------+----------------+---------------------+------+
1 row in set (0.00 sec)
表格结构来自商业应用,不能更改。
优化它的最佳索引是什么?还是更好的查询?
编辑:
它是每个客户端的 E401 代码的最后一次出现,并且仅当最后一次出现是该代码时。
【问题讨论】:
-
解释计划是什么样的?
-
@gnuchu 我无权访问生产环境来发布解释。我正在创建对外部客户端的查询。
标签: mysql sql optimization indexing