表上999个非聚集索引——你怎么看?

Posted Woodytu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了表上999个非聚集索引——你怎么看?相关的知识,希望对你有一定的参考价值。

对于数据获取,如果查询优化器在执行计划里选择了索引,那么SQL Server里的每个索引可以提高你的查询性能。但在另一方面,每个索引也会伤及你的性能,因为在INSERT,UPDATE和DELETE期间,每个索引需要被维护。因此对于你的工作量,尽可能创建少的索引非常重要——不然在写操作期间,你会有巨大的性能问题。

当你在表上定义了最大999个索引,SQL Server会如何反应?我从未在表上创建多大999个索引,因此详细验证下这个行为,并讨论下它引入的所有副作用,会是个很好的锻炼。

我们来创建一个可怕的表!

下面代码向你展示如何创建一个真正可怕的表:我刚创建了999次一样的非聚集索引——反复执行。

技术分享
   1 SET STATISTICS IO, TIME ON
   2 GO
   3 
   4 CREATE DATABASE TooMuchIndexes
   5 GO
   6 
   7 USE TooMuchIndexes
   8 GO
   9 
  10 -- Create a simple table
  11 CREATE TABLE PerformanceKiller
  12 (
  13     Col1 INT IDENTITY(1, 1) PRIMARY KEY NOT NULL,
  14     Col2 INT NOT NULL,
  15     Col3 CHAR(100) NOT NULL
  16 )
  17 GO
  18 
  19 -- Let‘s insert some data
  20 DECLARE @i INT = 1
  21 
  22 WHILE (@i < 100001)
  23 BEGIN
  24     INSERT INTO PerformanceKiller(Col2, Col3)
  25     VALUES (@i, REPLICATE(x, 100))
  26 
  27     SET @i += 1
  28 END
  29 GO
  30 
  31 -- Generates the DDL statement for all indexes
  32 DECLARE @i INT = 1
  33 
  34 WHILE (@i < 1000)
  35 BEGIN
  36     PRINT CREATE INDEX idx_StupidIndex + CAST(@i AS VARCHAR(3)) +  ON PerformanceKiller(Col2)
  37     SET @i += 1
  38 END
  39 GO
  40 
  41 -- Create 999 Non-Clustered Indexes...
  42 CREATE INDEX idx_StupidIndex1 ON PerformanceKiller(Col2)
  43 CREATE INDEX idx_StupidIndex2 ON PerformanceKiller(Col2)
  44 CREATE INDEX idx_StupidIndex3 ON PerformanceKiller(Col2)
  45 CREATE INDEX idx_StupidIndex4 ON PerformanceKiller(Col2)
  46 CREATE INDEX idx_StupidIndex5 ON PerformanceKiller(Col2)
  47 CREATE INDEX idx_StupidIndex6 ON PerformanceKiller(Col2)
  48 CREATE INDEX idx_StupidIndex7 ON PerformanceKiller(Col2)
  49 CREATE INDEX idx_StupidIndex8 ON PerformanceKiller(Col2)
  50 CREATE INDEX idx_StupidIndex9 ON PerformanceKiller(Col2)
  51 CREATE INDEX idx_StupidIndex10 ON PerformanceKiller(Col2)
  52 CREATE INDEX idx_StupidIndex11 ON PerformanceKiller(Col2)
  53 CREATE INDEX idx_StupidIndex12 ON PerformanceKiller(Col2)
  54 CREATE INDEX idx_StupidIndex13 ON PerformanceKiller(Col2)
  55 CREATE INDEX idx_StupidIndex14 ON PerformanceKiller(Col2)
  56 CREATE INDEX idx_StupidIndex15 ON PerformanceKiller(Col2)
  57 CREATE INDEX idx_StupidIndex16 ON PerformanceKiller(Col2)
  58 CREATE INDEX idx_StupidIndex17 ON PerformanceKiller(Col2)
  59 CREATE INDEX idx_StupidIndex18 ON PerformanceKiller(Col2)
  60 CREATE INDEX idx_StupidIndex19 ON PerformanceKiller(Col2)
  61 CREATE INDEX idx_StupidIndex20 ON PerformanceKiller(Col2)
  62 CREATE INDEX idx_StupidIndex21 ON PerformanceKiller(Col2)
  63 CREATE INDEX idx_StupidIndex22 ON PerformanceKiller(Col2)
  64 CREATE INDEX idx_StupidIndex23 ON PerformanceKiller(Col2)
  65 CREATE INDEX idx_StupidIndex24 ON PerformanceKiller(Col2)
  66 CREATE INDEX idx_StupidIndex25 ON PerformanceKiller(Col2)
  67 CREATE INDEX idx_StupidIndex26 ON PerformanceKiller(Col2)
  68 CREATE INDEX idx_StupidIndex27 ON PerformanceKiller(Col2)
  69 CREATE INDEX idx_StupidIndex28 ON PerformanceKiller(Col2)
  70 CREATE INDEX idx_StupidIndex29 ON PerformanceKiller(Col2)
  71 CREATE INDEX idx_StupidIndex30 ON PerformanceKiller(Col2)
  72 CREATE INDEX idx_StupidIndex31 ON PerformanceKiller(Col2)
  73 CREATE INDEX idx_StupidIndex32 ON PerformanceKiller(Col2)
  74 CREATE INDEX idx_StupidIndex33 ON PerformanceKiller(Col2)
  75 CREATE INDEX idx_StupidIndex34 ON PerformanceKiller(Col2)
  76 CREATE INDEX idx_StupidIndex35 ON PerformanceKiller(Col2)
  77 CREATE INDEX idx_StupidIndex36 ON PerformanceKiller(Col2)
  78 CREATE INDEX idx_StupidIndex37 ON PerformanceKiller(Col2)
  79 CREATE INDEX idx_StupidIndex38 ON PerformanceKiller(Col2)
  80 CREATE INDEX idx_StupidIndex39 ON PerformanceKiller(Col2)
  81 CREATE INDEX idx_StupidIndex40 ON PerformanceKiller(Col2)
  82 CREATE INDEX idx_StupidIndex41 ON PerformanceKiller(Col2)
  83 CREATE INDEX idx_StupidIndex42 ON PerformanceKiller(Col2)
  84 CREATE INDEX idx_StupidIndex43 ON PerformanceKiller(Col2)
  85 CREATE INDEX idx_StupidIndex44 ON PerformanceKiller(Col2)
  86 CREATE INDEX idx_StupidIndex45 ON PerformanceKiller(Col2)
  87 CREATE INDEX idx_StupidIndex46 ON PerformanceKiller(Col2)
  88 CREATE INDEX idx_StupidIndex47 ON PerformanceKiller(Col2)
  89 CREATE INDEX idx_StupidIndex48 ON PerformanceKiller(Col2)
  90 CREATE INDEX idx_StupidIndex49 ON PerformanceKiller(Col2)
  91 CREATE INDEX idx_StupidIndex50 ON PerformanceKiller(Col2)
  92 CREATE INDEX idx_StupidIndex51 ON PerformanceKiller(Col2)
  93 CREATE INDEX idx_StupidIndex52 ON PerformanceKiller(Col2)
  94 CREATE INDEX idx_StupidIndex53 ON PerformanceKiller(Col2)
  95 CREATE INDEX idx_StupidIndex54 ON PerformanceKiller(Col2)
  96 CREATE INDEX idx_StupidIndex55 ON PerformanceKiller(Col2)
  97 CREATE INDEX idx_StupidIndex56 ON PerformanceKiller(Col2)
  98 CREATE INDEX idx_StupidIndex57 ON PerformanceKiller(Col2)
  99 CREATE INDEX idx_StupidIndex58 ON PerformanceKiller(Col2)
 100 CREATE INDEX idx_StupidIndex59 ON PerformanceKiller(Col2)
 101 CREATE INDEX idx_StupidIndex60 ON PerformanceKiller(Col2)
 102 CREATE INDEX idx_StupidIndex61 ON PerformanceKiller(Col2)
 103 CREATE INDEX idx_StupidIndex62 ON PerformanceKiller(Col2)
 104 CREATE INDEX idx_StupidIndex63 ON PerformanceKiller(Col2)
 105 CREATE INDEX idx_StupidIndex64 ON PerformanceKiller(Col2)
 106 CREATE INDEX idx_StupidIndex65 ON PerformanceKiller(Col2)
 107 CREATE INDEX idx_StupidIndex66 ON PerformanceKiller(Col2)
 108 CREATE INDEX idx_StupidIndex67 ON PerformanceKiller(Col2)
 109 CREATE INDEX idx_StupidIndex68 ON PerformanceKiller(Col2)
 110 CREATE INDEX idx_StupidIndex69 ON PerformanceKiller(Col2)
 111 CREATE INDEX idx_StupidIndex70 ON PerformanceKiller(Col2)
 112 CREATE INDEX idx_StupidIndex71 ON PerformanceKiller(Col2)
 113 CREATE INDEX idx_StupidIndex72 ON PerformanceKiller(Col2)
 114 CREATE INDEX idx_StupidIndex73 ON PerformanceKiller(Col2)
 115 CREATE INDEX idx_StupidIndex74 ON PerformanceKiller(Col2)
 116 CREATE INDEX idx_StupidIndex75 ON PerformanceKiller(Col2)
 117 CREATE INDEX idx_StupidIndex76 ON PerformanceKiller(Col2)
 118 CREATE INDEX idx_StupidIndex77 ON PerformanceKiller(Col2)
 119 CREATE INDEX idx_StupidIndex78 ON PerformanceKiller(Col2)
 120 CREATE INDEX idx_StupidIndex79 ON PerformanceKiller(Col2)
 121 CREATE INDEX idx_StupidIndex80 ON PerformanceKiller(Col2)
 122 CREATE INDEX idx_StupidIndex81 ON PerformanceKiller(Col2)
 123 CREATE INDEX idx_StupidIndex82 ON PerformanceKiller(Col2)
 124 CREATE INDEX idx_StupidIndex83 ON PerformanceKiller(Col2)
 125 CREATE INDEX idx_StupidIndex84 ON PerformanceKiller(Col2)
 126 CREATE INDEX idx_StupidIndex85 ON PerformanceKiller(Col2)
 127 CREATE INDEX idx_StupidIndex86 ON PerformanceKiller(Col2)
 128 CREATE INDEX idx_StupidIndex87 ON PerformanceKiller(Col2)
 129 CREATE INDEX idx_StupidIndex88 ON PerformanceKiller(Col2)
 130 CREATE INDEX idx_StupidIndex89 ON PerformanceKiller(Col2)
 131 CREATE INDEX idx_StupidIndex90 ON PerformanceKiller(Col2)
 132 CREATE INDEX idx_StupidIndex91 ON PerformanceKiller(Col2)
 133 CREATE INDEX idx_StupidIndex92 ON PerformanceKiller(Col2)
 134 CREATE INDEX idx_StupidIndex93 ON PerformanceKiller(Col2)
 135 CREATE INDEX idx_StupidIndex94 ON PerformanceKiller(Col2)
 136 CREATE INDEX idx_StupidIndex95 ON PerformanceKiller(Col2)
 137 CREATE INDEX idx_StupidIndex96 ON PerformanceKiller(Col2)
 138 CREATE INDEX idx_StupidIndex97 ON PerformanceKiller(Col2)
 139 CREATE INDEX idx_StupidIndex98 ON PerformanceKiller(Col2)
 140 CREATE INDEX idx_StupidIndex99 ON PerformanceKiller(Col2)
 141 CREATE INDEX idx_StupidIndex100 ON PerformanceKiller(Col2)
 142 CREATE INDEX idx_StupidIndex101 ON PerformanceKiller(Col2)
 143 CREATE INDEX idx_StupidIndex102 ON PerformanceKiller(Col2)
 144 CREATE INDEX idx_StupidIndex103 ON PerformanceKiller(Col2)
 145 CREATE INDEX idx_StupidIndex104 ON PerformanceKiller(Col2)
 146 CREATE INDEX idx_StupidIndex105 ON PerformanceKiller(Col2)
 147 CREATE INDEX idx_StupidIndex106 ON PerformanceKiller(Col2)
 148 CREATE INDEX idx_StupidIndex107 ON PerformanceKiller(Col2)
 149 CREATE INDEX idx_StupidIndex108 ON PerformanceKiller(Col2)
 150 CREATE INDEX idx_StupidIndex109 ON PerformanceKiller(Col2)
 151 CREATE INDEX idx_StupidIndex110 ON PerformanceKiller(Col2)
 152 CREATE INDEX idx_StupidIndex111 ON PerformanceKiller(Col2)
 153 CREATE INDEX idx_StupidIndex112 ON PerformanceKiller(Col2)
 154 CREATE INDEX idx_StupidIndex113 ON PerformanceKiller(Col2)
 155 CREATE INDEX idx_StupidIndex114 ON PerformanceKiller(Col2)
 156 CREATE INDEX idx_StupidIndex115 ON PerformanceKiller(Col2)
 157 CREATE INDEX idx_StupidIndex116 ON PerformanceKiller(Col2)
 158 CREATE INDEX idx_StupidIndex117 ON PerformanceKiller(Col2)
 159 CREATE INDEX idx_StupidIndex118 ON PerformanceKiller(Col2)
 160 CREATE INDEX idx_StupidIndex119 ON PerformanceKiller(Col2)
 161 CREATE INDEX idx_StupidIndex120 ON PerformanceKiller(Col2)
 162 CREATE INDEX idx_StupidIndex121 ON PerformanceKiller(Col2)
 163 CREATE INDEX idx_StupidIndex122 ON PerformanceKiller(Col2)
 164 CREATE INDEX idx_StupidIndex123 ON PerformanceKiller(Col2)
 165 CREATE INDEX idx_StupidIndex124 ON PerformanceKiller(Col2)
 166 CREATE INDEX idx_StupidIndex125 ON PerformanceKiller(Col2)
 167 CREATE INDEX idx_StupidIndex126 ON PerformanceKiller(Col2)
 168 CREATE INDEX idx_StupidIndex127 ON PerformanceKiller(Col2)
 169 CREATE INDEX idx_StupidIndex128 ON PerformanceKiller(Col2)
 170 CREATE INDEX idx_StupidIndex129 ON PerformanceKiller(Col2)
 171 CREATE INDEX idx_StupidIndex130 ON PerformanceKiller(Col2)
 172 CREATE INDEX idx_StupidIndex131 ON PerformanceKiller(Col2)
 173 CREATE INDEX idx_StupidIndex132 ON PerformanceKiller(Col2)
 174 CREATE INDEX idx_StupidIndex133 ON PerformanceKiller(Col2)
 175 CREATE INDEX idx_StupidIndex134 ON PerformanceKiller(Col2)
 176 CREATE INDEX idx_StupidIndex135 ON PerformanceKiller(Col2)
 177 CREATE INDEX idx_StupidIndex136 ON PerformanceKiller(Col2)
 178 CREATE INDEX idx_StupidIndex137 ON PerformanceKiller(Col2)
 179 CREATE INDEX idx_StupidIndex138 ON PerformanceKiller(Col2)
 180 CREATE INDEX idx_StupidIndex139 ON PerformanceKiller(Col2)
 181 CREATE INDEX idx_StupidIndex140 ON PerformanceKiller(Col2)
 182 CREATE INDEX idx_StupidIndex141 ON PerformanceKiller(Col2)
 183 CREATE INDEX idx_StupidIndex142 ON PerformanceKiller(Col2)
 184 CREATE INDEX idx_StupidIndex143 ON PerformanceKiller(Col2)
 185 CREATE INDEX idx_StupidIndex144 ON PerformanceKiller(Col2)
 186 CREATE INDEX idx_StupidIndex145 ON PerformanceKiller(Col2)
 187 CREATE INDEX idx_StupidIndex146 ON PerformanceKiller(Col2)
 188 CREATE INDEX idx_StupidIndex147 ON PerformanceKiller(Col2)
 189 CREATE INDEX idx_StupidIndex148 ON PerformanceKiller(Col2)
 190 CREATE INDEX idx_StupidIndex149 ON PerformanceKiller(Col2)
 191 CREATE INDEX idx_StupidIndex150 ON PerformanceKiller(Col2)
 192 CREATE INDEX idx_StupidIndex151 ON PerformanceKiller(Col2)
 193 CREATE INDEX idx_StupidIndex152 ON PerformanceKiller(Col2)
 194 CREATE INDEX idx_StupidIndex153 ON PerformanceKiller(Col2)
 195 CREATE INDEX idx_StupidIndex154 ON PerformanceKiller(Col2)
 196 CREATE INDEX idx_StupidIndex155 ON PerformanceKiller(Col2)
 197 CREATE INDEX idx_StupidIndex156 ON PerformanceKiller(Col2)
 198 CREATE INDEX idx_StupidIndex157 ON PerformanceKiller(Col2)
 199 CREATE INDEX idx_StupidIndex158 ON PerformanceKiller(Col2)
 200 CREATE INDEX idx_StupidIndex159 ON PerformanceKiller(Col2)
 201 CREATE INDEX idx_StupidIndex160 ON PerformanceKiller(Col2)
 202 CREATE INDEX idx_StupidIndex161 ON PerformanceKiller(Col2)
 203 CREATE INDEX idx_StupidIndex162 ON PerformanceKiller(Col2)
 204 CREATE INDEX idx_StupidIndex163 ON PerformanceKiller(Col2)
 205 CREATE INDEX idx_StupidIndex164 ON PerformanceKiller(Col2)
 206 CREATE INDEX idx_StupidIndex165 ON PerformanceKiller(Col2)
 207 CREATE INDEX idx_StupidIndex166 ON PerformanceKiller(Col2)
 208 CREATE INDEX idx_StupidIndex167 ON PerformanceKiller(Col2)
 209 CREATE INDEX idx_StupidIndex168 ON PerformanceKiller(Col2)
 210 CREATE INDEX idx_StupidIndex169 ON PerformanceKiller(Col2)
 211 CREATE INDEX idx_StupidIndex170 ON PerformanceKiller(Col2)
 212 CREATE INDEX idx_StupidIndex171 ON PerformanceKiller(Col2)
 213 CREATE INDEX idx_StupidIndex172 ON PerformanceKiller(Col2)
 214 CREATE INDEX idx_StupidIndex173 ON PerformanceKiller(Col2)
 215 CREATE INDEX idx_StupidIndex174 ON PerformanceKiller(Col2)
 216 CREATE INDEX idx_StupidIndex175 ON PerformanceKiller(Col2)
 217 CREATE INDEX idx_StupidIndex176 ON PerformanceKiller(Col2)
 218 CREATE INDEX idx_StupidIndex177 ON PerformanceKiller(Col2)
 219 CREATE INDEX idx_StupidIndex178 ON PerformanceKiller(Col2)
 220 CREATE INDEX idx_StupidIndex179 ON PerformanceKiller(Col2)
 221 CREATE INDEX idx_StupidIndex180 ON PerformanceKiller(Col2)
 222 CREATE INDEX idx_StupidIndex181 ON PerformanceKiller(Col2)
 223 CREATE INDEX idx_StupidIndex182 ON PerformanceKiller(Col2)
 224 CREATE INDEX idx_StupidIndex183 ON PerformanceKiller(Col2)
 225 CREATE INDEX idx_StupidIndex184 ON PerformanceKiller(Col2)
 226 CREATE INDEX idx_StupidIndex185 ON PerformanceKiller(Col2)
 227 CREATE INDEX idx_StupidIndex186 ON PerformanceKiller(Col2)
 228 CREATE INDEX idx_StupidIndex187 ON PerformanceKiller(Col2)
 229 CREATE INDEX idx_StupidIndex188 ON PerformanceKiller(Col2)
 230 CREATE INDEX idx_StupidIndex189 ON PerformanceKiller(Col2)
 231 CREATE INDEX idx_StupidIndex190 ON PerformanceKiller(Col2)
 232 CREATE INDEX idx_StupidIndex191 ON PerformanceKiller(Col2)
 233 CREATE INDEX idx_StupidIndex192 ON PerformanceKiller(Col2)
 234 CREATE INDEX idx_StupidIndex193 ON PerformanceKiller(Col2)
 235 CREATE INDEX idx_StupidIndex194 ON PerformanceKiller(Col2)
 236 CREATE INDEX idx_StupidIndex195 ON PerformanceKiller(Col2)
 237 CREATE INDEX idx_StupidIndex196 ON PerformanceKiller(Col2)
 238 CREATE INDEX idx_StupidIndex197 ON PerformanceKiller(Col2)
 239 CREATE INDEX idx_StupidIndex198 ON PerformanceKiller(Col2)
 240 CREATE INDEX idx_StupidIndex199 ON PerformanceKiller(Col2)
 241 CREATE INDEX idx_StupidIndex200 ON PerformanceKiller(Col2)
 242 CREATE INDEX idx_StupidIndex201 ON PerformanceKiller(Col2)
 243 CREATE INDEX idx_StupidIndex202 ON PerformanceKiller(Col2)
 244 CREATE INDEX idx_StupidIndex203 ON PerformanceKiller(Col2)
 245 CREATE INDEX idx_StupidIndex204 ON PerformanceKiller(Col2)
 246 CREATE INDEX idx_StupidIndex205 ON PerformanceKiller(Col2)
 247 CREATE INDEX idx_StupidIndex206 ON PerformanceKiller(Col2)
 248 CREATE INDEX idx_StupidIndex207 ON PerformanceKiller(Col2)
 249 CREATE INDEX idx_StupidIndex208 ON PerformanceKiller(Col2)
 250 CREATE INDEX idx_StupidIndex209 ON PerformanceKiller(Col2)
 251 CREATE INDEX idx_StupidIndex210 ON PerformanceKiller(Col2)
 252 CREATE INDEX idx_StupidIndex211 ON PerformanceKiller(Col2)
 253 CREATE INDEX idx_StupidIndex212 ON PerformanceKiller(Col2)
 254 CREATE INDEX idx_StupidIndex213 ON PerformanceKiller(Col2)
 255 CREATE INDEX idx_StupidIndex214 ON PerformanceKiller(Col2)
 256 CREATE INDEX idx_StupidIndex215 ON PerformanceKiller(Col2)
 257 CREATE INDEX idx_StupidIndex216 ON PerformanceKiller(Col2)
 258 CREATE INDEX idx_StupidIndex217 ON PerformanceKiller(Col2)
 259 CREATE INDEX idx_StupidIndex218 ON PerformanceKiller(Col2)
 260 CREATE INDEX idx_StupidIndex219 ON PerformanceKiller(Col2)
 261 CREATE INDEX idx_StupidIndex220 ON PerformanceKiller(Col2)
 262 CREATE INDEX idx_StupidIndex221 ON PerformanceKiller(Col2)
 263 CREATE INDEX idx_StupidIndex222 ON PerformanceKiller(Col2)
 264 CREATE INDEX idx_StupidIndex223 ON PerformanceKiller(Col2)
 265 CREATE INDEX idx_StupidIndex224 ON PerformanceKiller(Col2)
 266 CREATE INDEX idx_StupidIndex225 ON PerformanceKiller(Col2)
 267 CREATE INDEX idx_StupidIndex226 ON PerformanceKiller(Col2)
 268 CREATE INDEX idx_StupidIndex227 ON PerformanceKiller(Col2)
 269 CREATE INDEX idx_StupidIndex228 ON PerformanceKiller(Col2)
 270 CREATE INDEX idx_StupidIndex229 ON PerformanceKiller(Col2)
 271 CREATE INDEX idx_StupidIndex230 ON PerformanceKiller(Col2)
 272 CREATE INDEX idx_StupidIndex231 ON PerformanceKiller(Col2)
 273 CREATE INDEX idx_StupidInd

以上是关于表上999个非聚集索引——你怎么看?的主要内容,如果未能解决你的问题,请参考以下文章

如何在视图上实现聚集索引

在具有聚集列存储索引的表上创建触发器 - 错误

sqlserver的索引

索引的概述

求高手解答mysql 的索引怎么用

10-04索引的概述