我的桌子大约有。 121,246,211行。记录是简单的页面印象信息。
以下是架构:
create table stat_page
(
id int auto_increment primary key,
pageId int not null,
timestamp int not null
)
collate = utf8_unicode_ci;
create index pageIdIndex
on stat_page (pageId);
create index timestampIndex
on stat_page (timestamp);
此查询需要15秒:
select count(*)
from stat_page
where `timestamp` > 1543622400;
此查询将花费近7分钟的时间:
select count(*)
from stat_page
where `timestamp` > 1543622400
and pageId = 87;
我以为我索引了正确的事情;桌子太大了吗?有没有人建议如何更快地从该表中获取信息?
答案 0 :(得分:6)
以下索引将提高该查询的性能:
create index ix1 on stat_page (pageId, timestamp);
此查询受益于此“复合”索引。