简体   繁体   English

SQL 索引,如何创建更好

[英]SQL Indexes, how to create better

I use SQL Azure and have a table with 1M records.我使用 SQL Azure 并有一个包含 1M 记录的表。 Table has columns (main):表有列(主要):

Id - int, autoincrement identity Id - int,自增标识

IMEI - unique index IMEI - 唯一索引

Sync1 - bool, not nullable Sync1 - 布尔型,不可为空

Sync2 - bool, not nullable Sync2 - 布尔型,不可为空

From these 1M records only approx 6K records have at least Sync1 == true or Sync2 == true (or both).从这 1M 条记录中,只有大约 6K 条记录至少有Sync1 == trueSync2 == true (或两者兼有)。 Most of queries are find records with at least one Sync is true大多数查询是找到至少一个 Sync 为 true 的记录

How to create indexes effectively?如何有效地创建索引? I see the following:我看到以下内容:

  1. (Id + Sync1), (Id + Sync2) (Id + Sync1), (Id + Sync2)
  2. (Id + Symc1), (Id + Sync2), (Id + Sync1 + Sync2) (Id + Symc1), (Id + Sync2), (Id + Sync1 + Sync2)
  3. (IMEI + Sync1), (IMEI + Sync2) (IMEI + Sync1), (IMEI + Sync2)

something else?别的东西?

Thank you谢谢

two indexing ideas to try:尝试两个索引想法:

1) Index each of the sync fields separately: 1)分别索引每个同步字段:

CREATE NONCLUSTERED INDEX IX_MyTable_Sync1 ON  MyTable 
    (Sync1)  
    INCLUDE (IMEI, Sync2, FieldA, FieldB, FieldC); 
CREATE NONCLUSTERED INDEX IX_MyTable_Sync2 ON  MyTable 
    (Sync2)  
    INCLUDE (IMEI, Sync1, FieldA, FieldB, FieldC); 

In this case the query should probably be written as a Union so each index can be used effectively:在这种情况下,查询可能应该写成一个联合,以便可以有效地使用每个索引:

Select * from MyTable where Sync1 is not null
UNION
Select * from MyTable where Sync2 is not null

2) A filtered index on Sync1 and Sync2: 2) Sync1 和 Sync2 上的过滤索引:

CREATE NONCLUSTERED INDEX IX_SyncNotNull ON  MyTable 
    (IMEI, Sync1, Sync2, FieldA, FieldB, FieldC)  
    WHERE (Sync1 IS NOT NULL OR Sync2 IS NOT NULL); 

But take note: you query will probably need to have this exact query criteria for the index to be used - (Sync1 is not null OR Sync2 is not null) - so have care.但请注意:您的查询可能需要对要使用的索引有这个确切的查询条件 - (Sync1 is not null OR Sync2 is not null) - 所以要小心。 I personally don't have a lot of experience with filtered indexes.我个人对过滤索引没有太多经验。

In both cases, I would probably include the fields needed in the query to be sure the index is used, but of course go ahead and try it without including other fields and see if the query plan makes use of the index or not.在这两种情况下,我可能会在查询中包含所需的字段以确保使用索引,但当然是 go 并在不包含其他字段的情况下尝试它,看看查询计划是否使用索引。 You should read up on "include" in sql server index, as well as using "covering indexes".您应该阅读 sql 服务器索引中的“包含”,以及使用“覆盖索引”。 I just mention this because your question implicitly assumes that you only need to worry about the fields Sync1 and Sync2 - and that may not be true我之所以提到这一点,是因为您的问题隐含地假设您只需要担心字段 Sync1 和 Sync2 - 这可能不是真的

Whatever strategy you try, you should always look at the estimated or actual query plans to see if the indexes are being used (you might even see what indexes SQL Server suggests and could try those suggestions too).无论您尝试何种策略,您都应该始终查看估计的或实际的查询计划,以查看是否正在使用索引(您甚至可以查看 SQL Server 建议的索引,也可以尝试这些建议)。

It is essential to know a little about how to investigate query plans for you queries - search for some information on this if you are not familiar with sql server query plans (for example, sites like this one: sql server query plans )了解如何为您的查询调查查询计划是必不可少的 - 如果您不熟悉 sql 服务器查询计划(例如,像这样的网站: sql 服务器查询计划),请搜索一些相关信息

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

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