MySQL:如何索引我的表以优化此查询?

时间:2012-07-11 23:43:40

标签: mysql

我的表包含与对象相关的值的历史记录,如下所示:

create table History (
    object_id  bigint  NOT NULL,
    value      int     NOT NULL,
    date       bigint  NOT NULL
);

如何对其进行索引以优化以下查询:

select * from History
    where object_id = ? and date < ?
    order by date desc
    limit ?

1 个答案:

答案 0 :(得分:3)

创建复合索引object_id + date

CREATE INDEX object_id_date ON History(object_id, `date`);