【问题标题】:How can I optimize this further?我该如何进一步优化呢?
【发布时间】:2019-01-02 22:09:43
【问题描述】:

我的桌子大约有。 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;

我认为我索引了正确的东西;桌子太大了吗?有没有人建议如何更快地从此表中获取信息?

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    以下索引将提高该查询的性能:

    create index ix1 on stat_page (pageId, timestamp);
    

    此“复合”索引的此查询优势。

    【讨论】:

    • @DStanley 我认为切换列的顺序会改善其他查询,但不会改善这个。此查询与列 pageId 具有相等性 (=),这应该极大地受益于将列放在首位。
    • 太好了,谢谢,我不知道为什么我没有想到这一点。干杯。
    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 2018-05-29
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    相关资源
    最近更新 更多