Firebird:为选择最后一条记录创建什么索引?

Firebird: What index to create for selecting the last record?

我 select 中记录子集的最后一条记录(通过时间戳字段) table 并且想创建一个有用的索引来加速查询。令我惊讶的是,我无法定义 Firebird 愿意使用的索引。此外,我没有在互联网上找到任何关于我的问题的示例或提示。所以,也许我在这里能找到一些帮助。

我有一个table这样的

create table MDE_ST (
  ID            integer not null,
  RESOURCE_ID   integer not null,
  STARTTIME     timestamp not null,
  ...
  primary key (ID),
  foreign key (RESOURCE_ID) references RESOURCES
);

和这样的查询

select *
from MDE_ST
where RESOURCE_ID = ?
order by STARTTIME desc
rows 1

我的问题是,查询真的很慢。 Firebird 总是使用资源上的外键索引来执行查询。但是只有几十个资源,每个资源有上万条记录。所以 Firebird 在资源记录中进行了某种自然扫描。

我试试索引

create index MDE_ST_ASC on MDE_ST (RESOURCE_ID, STARTTIME);

create desc index MDE_ST_DESC on MDE_ST (RESOURCE_ID, STARTTIME);

但没有任何变化。 Firebird 总是使用外键的索引。在您提问之前:索引统计数据是最新的。

我的问题:我必须创建什么索引来加速查询?

尝试

create desc index MDE_ST_DESC on MDE_ST (STARTTIME);

INDEX

All indexes in Firebird are uni-directional. An index may be constructed from the lowest value to the highest (ascending order) or from the highest value to the lowest (descending order). The keywords ASC[ENDING] and DESC[ENDING] are used to specify the direction of the index. The default index order is ASC[ENDING]. It is quite valid to define both an ascending and a descending index on the same column or key set.

A descending index can be useful on a column that will be subjected to searches on the high values (“newest”, maximum, etc.)

当您使用 "ORDER BY " DESCENDING 时,您应该对此列使用降序索引。

换句话说,索引的方向应该与"order by"的方向相同。