我有一个大约100列的表。该表包含大约400万行。 我在主键上创建了一个聚簇索引
CREATE TABLE [dbo].[Table1](
[Col1] [int] IDENTITY(1,1) NOT NULL,
[Col2] [int] NOT NULL,
[Col3] [int] NOT NULL,
[Col4] [int] NOT NULL,
[Col5] [int] NULL,
[Col6] [int] NULL,
[Col7] [varchar](1) NULL,
[Col8] [varchar](1) NULL,
[Col9] [int] NULL,
[Col10] [varchar](100) NULL,
[Col11] [int] NULL,
[Col12] [varchar](100) NULL,
[Col13] [varchar](20) NULL,
[Col14] [int] NULL,
[Col15] [int] NULL,
[Col16] [varchar](80) NULL,
[Col17] [int] NULL,
[Col18] [varchar](50) NULL,
[Col19] [int] NULL,
[Col20] [varchar](50) NULL,
[Col21] [float] NULL,
[Col22] [float] NULL,
[Col23] [float] NULL,
[Col24] [float] NULL,
[Col25] [float] NULL,
[Col26] [float] NULL,
[Col27] [float] NULL,
[Col28] [float] NULL,
[Col29] [float] NULL,
[Col30] [float] NULL,
[Col31] [float] NULL,
[Col32] [float] NULL,
[Col33] [int] NULL,
[Col34] [varchar](50) NULL,
[Col35] [int] NULL,
[Col36] [datetime] NULL,
[Col37] [int] NULL,
[Col38] [datetime] NULL,
[Col39] [int] NULL,
[Col40] [datetime] NULL,
[Col41] [datetime] NULL,
[Col42] [int] NULL,
[Col43] [datetime] NULL,
[Col44] [datetime] NULL,
[Col45] [varchar](1) NULL,
[Col46] [int] NULL,
[Col47] [varchar](50) NULL,
[Col48] [int] NULL,
[Col49] [varchar](100) NULL,
[Col50] [int] NULL,
[Col51] [varchar](100) NULL,
[Col52] [int] NULL,
[Col53] [varchar](120) NULL,
[Col54] [varchar](255) NULL,
[Col55] [varchar](255) NULL,
[Col56] [datetime] NULL,
[Col57] [varchar](20) NULL,
[Col58] [varchar](1) NULL,
[Col59] [varchar](255) NULL,
[Col60] [varchar](255) NULL,
[Col61] [varchar](80) NULL,
[Col62] [varchar](50) NULL,
[Col63] [varchar](1) NULL,
[Col64] [varchar](50) NULL,
[Col65] [varchar](1) NULL,
[Col66] [varchar](4) NULL,
[Col67] [varchar](60) NULL,
[Col68] [varchar](8000) NULL,
[Col69] [int] NULL,
[Col70] [varchar](80) NULL,
[Col71] [varchar](1) NULL,
[Col72] [datetime] NULL,
[Col73] [int] NULL,
[Col74] [varchar](50) NULL,
[Col75] [varchar](255) NULL,
[Col76] [varchar](255) NULL,
[Col77] [varchar](255) NULL,
[Col78] [varchar](255) NULL,
[Col79] [varchar](255) NULL,
[Col80] [varchar](1) NULL,
[Col81] [varchar](1) NULL,
[Col82] [varchar](50) NULL,
[Col83] [varchar](200) NULL,
[Col84] [int] NULL,
[Col85] [varchar](120) NULL,
[Col86] [int] NULL,
[Col87] [varchar](120) NULL,
[Col88] [varchar](255) NULL,
[Col89] [varchar](1) NULL,
[Col90] [varchar](80) NULL,
[Col91] [varchar](255) NULL,
[Col92] [varchar](20) NULL,
[Col93] [varchar](255) NULL,
[Col94] [varchar](8000) NULL,
[Col95] [varchar](255) NULL,
[Col96] [varchar](255) NULL,
[Col97] [varchar](255) NULL,
[Col98] [char](1) NULL,
[Col99] [varchar](255) NULL,
[Col100] [datetime] NULL,
[Col101] [int] NULL,
[Col102] [float] NULL,
PRIMARY KEY CLUSTERED
(
[Col1] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
当我从此表中选择*时,执行时间超过12分钟。 我也使用过NoLock。
我在这里缺少什么?我应该创建哪些非聚集索引以使其更快地运行?
提前致谢。
答案 0 :(得分:1)
基本上在对具有大量行的表执行选择时要遵循黄金法则
1)仅查询您需要的行
2)仅查询您需要的列
但是在这张巨大的桌子上应用select *显然会耗费时间。
答案 1 :(得分:0)
如果将主键放在where条件中,则索引将有用。否则,它将立即获取每条记录,并且有基础设施修复工作