Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建索引SQL Server 2008_Sql_Sql Server_Sql Server 2008_Sql Server 2012 - Fatal编程技术网

创建索引SQL Server 2008

创建索引SQL Server 2008,sql,sql-server,sql-server-2008,sql-server-2012,Sql,Sql Server,Sql Server 2008,Sql Server 2012,最近我被投入到数据库微调中。我对SQL Server有一些想法,并决定创建一些索引 提到这个 但是我不明白其他类型的索引,比如包括,和选项,会有什么帮助。我试着在谷歌上搜索,但没能看到一个简单的描述 CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber ON dbo.Presidents (PresidentNumber) INCLUDE (President,YearsInOffice,RatingPoints) WHERE ElectoralVote

最近我被投入到数据库微调中。我对SQL Server有一些想法,并决定创建一些索引

提到这个

但是我不明白其他类型的索引,比如
包括
选项,会有什么帮助。我试着在谷歌上搜索,但没能看到一个简单的描述

CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
INCLUDE (President,YearsInOffice,RatingPoints)
WHERE ElectoralVotes IS NOT NULL

CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = ROW )

CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = PAGE )

我应该使用上述哪种情况?它们会提高性能吗?

我不能说使用数据压缩选项,但包含选项肯定可以提高性能。如果仅选择PresidentNumber和一个或多个President、YearsInOffice或RatingPoints列,并且ElectorVotes不为null,则查询将从索引本身获取值,而不必接触基础表。如果您的表有其他列,并且您在查询中包含其中一列,那么它将必须从表和索引中检索值

Select PresidentNumber, President, YearsInOffice, RatingPoints
From Presidents
Where RatingPoints > 50

上述查询将仅从IX_NC_PresidentNumber读取,而不必从Presidents表中提取数据,因为查询中的所有列都包含在索引中

Select top 20 PresidentNumber, President, YearsInOffice, PoliticalParty
From Presidents
where ElectoralVotes IS NOT NULL
此查询将使用索引IX_NC_PresidentNumber和Presidents表,因为查询中的PoliticalParty列不包括在索引中

Select PresidentNumber, President, YearsInOffice, RatingPoints
From Presidents
Where RatingPoints > 50

由于查询中的where子句与索引中使用的where子句不匹配,并且行数没有限制,因此此查询很可能最终执行表扫描

数据压缩也将有助于提高查询性能,因为在压缩之后,当您运行查询时,加载的页面/数据块会更少,因为I/O会减少,所以减少I/O始终是一个不错的选择

请在这里添加示例查询。这将很容易帮助我思考和理解:)看起来不错。所以我们需要为SELECT语句中的所有列添加索引以获得良好的性能?避免表扫描。不,不需要将所有列添加到索引以避免表扫描。您需要在where子句中使用的列上有索引,以避免表扫描。由于索引中包含指向行的指针,因此在索引中包含所有列所带来的性能提升相对较小。最大的好处是在过滤柱上有索引。