简体   繁体   English

SQL 服务器索引包含的列

[英]SQL Server index included columns

I need help understanding how to create indexes.我需要帮助了解如何创建索引。 I have a table that looks like this我有一张看起来像这样的桌子

  • Id ID
  • Name姓名
  • Age年龄
  • Location地点
  • Education,教育,
  • PhoneNumber电话号码

My query looks like this:我的查询如下所示:

SELECT * 
  FROM table1 
 WHERE name = 'sam'
  1. What's the correct way to create an index for this with included columns?使用包含的列为此创建索引的正确方法是什么?
  2. What if the query has a order by statement?如果查询有 order by 语句怎么办?

     SELECT * FROM table1 WHERE name = 'sam' ORDER BY id DESC
  3. What if I have 2 parameters in my where statement?如果我的 where 语句中有 2 个参数怎么办?

     SELECT * FROM table1 WHERE name = 'sam' AND age > 12

The correct way to create an index with included columns?创建包含列的索引的正确方法? Either via Management Studio/Toad/etc, or SQL ( documentation ):通过 Management Studio/Toad/etc 或 SQL(文档):

CREATE INDEX idx_table_1 ON db.table_1 (name) INCLUDE (id)

What if the Query has an ORDER BY如果查询有ORDER BY怎么办

The ORDER BY can use indexes, if the optimizer sees fit to (determined by table statistics & query).如果优化器认为适合(由表统计信息和查询确定),则ORDER BY可以使用索引。 It's up to you to test if a composite index or an index with INCLUDE columns works best by reviewing the query cost.您可以通过查看查询成本来测试复合索引或具有INCLUDE列的索引是否效果最佳。

If id is the clustered key (not always the primary key though), I probably wouldn't INCLUDE the column...如果id是聚集键(但并不总是主键),我可能不会包含该列...

What if I have 2 parameters in my where statement?如果我的 where 语句中有 2 个参数怎么办?

Same as above - you need to test what works best for your query.与上面相同 - 您需要测试最适合您的查询的方法。 Might be composite, or include, or separate indexes.可能是复合的、包含的或单独的索引。

But keep in mind that:但请记住:

  • tweaking for one query won't necessarily benefit every other query调整一个查询不一定会使其他所有查询受益
  • indexes do slow down INSERT/UPDATE/DELETE statements, and require maintenance索引确实会减慢 INSERT/UPDATE/DELETE 语句的速度,并且需要维护
  • You can use the Database Tuning Advisor (DTA) for index recommendations, including when some are redundant您可以将数据库优化顾问 (DTA) 用于索引建议,包括一些冗余的索引建议

Recommended reading推荐阅读

I highly recommend reading Kimberly Tripp's " The Tipping Point " for a better understanding of index decisions and impacts.我强烈建议阅读 Kimberly Tripp 的“ The Tipping Point ”,以更好地了解指数决策和影响。

Since I do not know which exactly tasks your DB is going to implement and how many records in it, I would suggest that you take a look at the Index Basics MSDN article.由于我不知道您的数据库将执行哪些确切任务以及其中有多少记录,我建议您查看Index Basics MSDN 文章。 It will allow you to decide yourself which indexes to create.它将允许您自己决定要创建哪些索引。

If ID is your primary and/or clustered index key, just create an index on Name, Age .如果ID是您的主索引和/或聚集索引键,只需在Name, Age上创建一个索引。 This will cover all three queries.这将涵盖所有三个查询。

Included fields are best used to retrieve row-level values for columns that are not in the filter list, or to retrieve aggregate values where the sorted field is in the GROUP BY clause.包含字段最适合用于检索不在筛选器列表中的列的行级值,或检索排序字段位于GROUP BY子句中的聚合值。

If inserts are rare, create as much indexes as You want.如果插入很少见,请根据需要创建尽可能多的索引。

For first query create index for name column.对于第一个查询,为名称列创建索引。

Id column I think already is primary key...我认为 ID 列已经是主键...

Create 2nd index with name and age.使用名称和年龄创建第二个索引。 You can keep only one index: 'name, ag'e and it will not be much slower for 1st query.您只能保留一个索引:'name, ag'e,第一次查询不会慢很多。

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

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