2

我有一个表,我正在为 where 子句搜索中经常使用的字段创建非聚集索引。我正在使用包含子句创建索引并将所有字段放入其中。

前任:

Create NonClustered Index IX_DocumentDate on Documents
(
    DocumentDate     
)
Include(
    JurisdictionID,
    JudgementTypeID,
    IDate,
    EfileDate,
    UserID,
    RecordingDateTime,
    ApprovedBy,
    ACEfileBankAccountID,
    LastStatusChangedDateTime,
    ACEfileCreditCardID,
    EfiledByUserID,
    ITypeID,
    IGroupID,
    InstrumentID,
    OldInstrumentID,
    [CreatedByJurisdictionID],
    CreatedByAccountID,
    [DocumentStatusID]      
      ,[Remarks]
      ,[InternalNotes]
      ,[ParentDocumentID]
      ,[FilingNumber]
      ,[StampData]      
      ,[Receipt]
      ,[ReceiptNo]
      ,[IsReEfiled]      
      ,[ImportedFromInstrumentID]
)

而不是仅仅创建索引。这些字段都用在Select. 只是想知道我做错了吗?当我这样做时,我会获得巨大的绩效奖金。如果这没问题,那么为什么 Microsoft 不将其用作默认选项并将表的所有字段都包含在 中include

4

3 回答 3

5

是的。一般来说,在 SQL 中,拥有一种模式总是不好的——很多取决于使用情况,而且会有所不同。

在上述情况下,这可能是也可能不是明智的、需要的或有益的。不要忘记您付出了代价——磁盘大小(=IO​​ 和更多 RAM 使用)以及更新和插入速度(较慢)。

通常,应谨慎使用 INCLUDE。从没有开始,然后在有问题的地方使用它,它会有所帮助。

于 2012-12-27T06:16:14.730 回答
3

You are storing your entire table twice, once in the index, once in the main table. This is generally regarded as a waste of space. It also reduces the efficiency of the index; there is more data to read than there would be if you restrained yourself to no included columns, or only a few included columns. It might well be sensible to include the primary document ID column in the index (though it is not clear which column is the primary document ID), but the majority of the columns you included are clearly superfluous to the index.

So, the reason it isn't the default behaviour is because it is not as efficient as an index without included columns. So, don't include any columns in the index until you have a need to do so, and can measure that it improves the performance.

于 2012-12-27T06:27:40.417 回答
0

您可以为一列尝试聚集索引,可能使用主键然后检查性能。您可以将聚集索引的性能与所有列的索引进行比较。最佳实践表明您应该根据要求拥有最少的索引。

于 2012-12-27T10:38:04.657 回答