简体   繁体   English

繁忙的表,索引或不索引

[英]Busy table, to index or not to index

SQL Server, very busy table, holds activity for around 6-10K users at any time and have inserts/updates/deletes on each hit. SQL 服务器,非常繁忙的表,随时为大约 6-10K 用户保存活动,并且每次点击都有插入/更新/删除。

I didn't use index on this table as any data written doesn't stay more than 10 minutes.我没有在这张表上使用索引,因为写入的任何数据都不会停留超过 10 分钟。

But while watching activity monitor I've noticed that SQL Server suggests adding an index to that table.但是在观看活动监视器时,我注意到 SQL 服务器建议向该表添加索引。

I know keeping index on a table has its costs do you think I should add index?我知道在表上保留索引有其成本,您认为我应该添加索引吗?

Update about details: SQL 2008 R2更新细节: SQL 2008 R2

Table桌子

     [id] [bigint] IDENTITY(1,1) NOT NULL,
     [siteid] [int] NOT NULL,
     [last_seen] [datetime] NOT NULL,
     [ua] [varchar](250) NOT NULL,
     [ip] [varchar](15) NOT NULL,
     [t_id] [int] NOT NULL

Suggested index建议索引

ON [dbo].[onlines] ([ua],[ip],[t_id])

Having a good clustering key can actually speed up even your inserts (read Kimberly Tripp's excellent blog post The Clustered Index Debate Continues for a great and thorough explanation, why that is so), And from the looks of it.拥有一个好的集群键实际上甚至可以加快您的插入速度(阅读 Kimberly Tripp 的优秀博客文章The Clustered Index Debate Continues以获得一个很好而彻底的解释,为什么会这样),并且从它的外观来看。 you have no clustering key at all.你根本没有聚类键。

So I would definitely recommend adding a primary/clustering key on your id column - it's perfectly suited for a good clustering key: narrow, static, unique, ever-increasing:所以我肯定会推荐在你的id列上添加一个主键/集群键 - 它非常适合一个好的集群键:窄,static,独特,不断增加:

CREATE UNIQUE CLUSTERED INDEX CIX_YourTableName
  ON dbo.YourTableName(ID)

That should speed up everything - inserts, updates, deletes and select, too.这应该会加速一切——插入、更新、删除和 select。

Whether you need additional indices (and which columns to index) depends on your SELECT queries - since you didn't really tell us anything about those, we can only guess into the blue.....您是否需要其他索引(以及要索引的列)取决于您的 SELECT 查询 - 因为您并没有真正告诉我们有关这些的任何信息,我们只能猜测蓝色.....

Basically, what you need to do:基本上,你需要做的:

  • establish a base line - measure your performance建立基线 - 衡量你的表现
  • then apply an index that makes sense - include columns used in WHERE clauses and/or ORDER BY expressions然后应用一个有意义的索引 - 包括 WHERE 子句和/或 ORDER BY 表达式中使用的列
  • measure again and compare再次测量并比较

There's really no magic formula to decide whether you need an index, what it is, and how much it'll help your scenario (or how much it'll hurt INSERT performance) - you need to measure yourself, on your database, your hardware, your environment.真的没有什么神奇的公式可以决定你是否需要一个索引,它是什么,以及它对你的场景有多大帮助(或者它会对 INSERT 性能有多大影响)——你需要在你的数据库、你的硬件上衡量自己, 你的环境。

Adding an index costs during writes to the table.在写入表期间添加索引成本。 (INSERT, UPDATE, DELETE operations). (插入、更新、删除操作)。 It can improve performance on reads (SELECT, UPDATE, DELETE operations).它可以提高读取性能(SELECT、UPDATE、DELETE 操作)。 So, if you have more reads than writes then yes, an index can help you.因此,如果您的读取次数多于写入次数,那么可以,索引可以帮助您。 If you read much less than you write then an index can help you as you don't have to read as much (an index is basically a small subset of the table, and if your select only hits that subset then you can speed up reads considerably in some cases)如果你读的比写的少,那么索引可以帮助你,因为你不必读那么多(索引基本上是表的一个小子集,如果你的 select 只命中那个子集,那么你可以加快读取速度在某些情况下相当)

I would add the index and monitor the performance, if it doesn't work out for you then remove the index.我会添加索引并监控性能,如果它不适合你然后删除索引。 Ultimately, the only way to know for sure is to try it and compare the results.最终,唯一确定的方法是尝试并比较结果。

The time the data exists doesn't tell you much about the need of indexing.数据存在的时间并不能告诉您索引的需要。 But the rate of updates vs reads does.但是更新率与读取率确实如此。 Inserts are fastest without the index.没有索引的插入是最快的。 Updates and deletes may be not, because they may benefit from having the index when searching for the record to alter.更新和删除可能不是,因为在搜索要更改的记录时,它们可能会受益于索引。 Selects also benefit from having an index.选择也受益于索引。

So it is usually better to have indices, even when there are many updates to a table.所以通常最好有索引,即使一个表有很多更新。 The general exception is a table for logging, to which you usually only write unless that one time when you actually need the log.一般的例外是用于记录的表,您通常只在其中写入,除非您确实需要日志。

But to be sure, you'd best put an index on there and monitor the performance hit or boost.但可以肯定的是,您最好在其中放置一个索引并监控性能的影响或提升。

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

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