查询一个属性大于另一个属性的行所需的索引是什么?

时间:2017-07-21 12:10:55

标签: sql-server indexing

我有桌子

create table resources (
    id bigint primary key,
    value nvarchar(255),
    updateId bigint,
    pushUpdateId bigint
);

有很多行。 我需要创建哪些索引来加速查询:

select id from resources where updateId > pushUpdateId;

1 个答案:

答案 0 :(得分:1)

如上所述,没有索引会有所帮助,因为在同一行中有两列(嗯,有索引类型,但在SQL Server中实现它们并不容易)。如果没有多条匹配条件的记录,则可以创建计算列和索引:

create table resources (
    id bigint primary key,
    value nvarchar(255),
    updateId bigint,
    pushUpdateId bigint,
    compare_updateId_pushUpdateId as (case when updateId > pushUpdateId then 1 else 0 end)
);

然后,您可以创建索引:

create index idx_resources_2 on resources(compare_updateId_pushUpdateId, id);

然后将查询短语为:

select id
from resources
where compare_updateId_pushUpdateId = 1;
相关问题