简体   繁体   English

DISTINCT 值的表索引

[英]table index for DISTINCT values

In my stored procedure, I need "Unique" values of one of the columns.在我的存储过程中,我需要其中一列的“唯一”值。 I am not sure if I should and if I should, what type of Index I should apply on the table for better performance.我不确定我是否应该,如果我应该,我应该在表上应用什么类型的索引以获得更好的性能。 No being very specific, the same case happens when I retrieve distinct values of multiple columns.不是很具体,当我检索多列的不同值时会发生同样的情况。 The column is of String(NVARCHAR) type.该列是 String(NVARCHAR) 类型。

eg例如

select DISTINCT Column1 FROM Table1;从表 1 中选择 DISTINCT Column1;

OR

select DISTINCT Column1, Column2, Column3 FROM Table1;从 Table1 中选择 DISTINCT Column1、Column2、Column3;

An index on these specific columns could improve performance by a bit, but just because it will require SQL Server to scan less data (just these specific columns, nothing else).这些特定列上的索引可以稍微提高性能,但这只是因为它需要 SQL Server 扫描更少的数据(仅这些特定列,没有别的)。 Other than that - a SCAN will always be done.除此之外 - 将始终进行扫描。 An option would be to create indexed view if you need distinct values from that table.如果您需要该表中的不同值,则可以选择创建索引视图。

CREATE VIEW Test
WITH SCHEMABINDING
AS
SELECT Column1, COUNT_BIG(*) AS UselessColumn
FROM Table1
GROUP BY Column1;
GO
CREATE UNIQUE CLUSTERED INDEX PK_Test ON Test (Column1);
GO

And then you can query it like that:然后你可以像这样查询它:

SELECT *
FROM Test WITH (NOEXPAND);

NOEXPAND is a hint needed for SQL Server to not expand query in a view and treat it as a table. NOEXPAND是 SQL Server 不展开视图中的查询并将其视为表所需的提示。 Note: this is needed for non Enterprise version of SQL Server only.注意:这仅适用于非企业版 SQL Server。

I recently had the same issue and found it could be overcome using a Columnstore index:我最近遇到了同样的问题,发现可以使用 Columnstore 索引来解决它:

    CREATE NONCLUSTERED COLUMNSTORE INDEX [CI_TABLE1_Column1] ON [TABLE1]
    ([Column1])
    WITH (DROP_EXISTING = OFF, COMPRESSION_DELAY = 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM