如何加快使用聚簇索引扫描的简单查询?

时间:2013-12-04 00:03:10

标签: sql sql-server performance indexing

我有一个简单的内连接返回10k行,但它需要大约6秒。我可以通过索引改进或其他方式加快速度吗?

过去我使用guid或递增int作为主键,并且从未遇到过比这更大的数据集的问题,我怀疑Stock.StockId varchar(50)是我的问题。我意识到我可以将其更改为int但是认为可能有一种替代方法可以在此列更改后保存更新所有数据源,fk引用等。我今天在stockBinQuantity上引入了群集密钥,将查询时间从10秒减少到6秒,但仍然太慢。

我的数据量 - 表行计数
斌:1009
库存:11414
股票数量:8456

表格列和索引
http://i41.tinypic.com/w14txi.jpg http://i43.tinypic.com/2iuyl4o.jpg

查询和执行计划 http://i43.tinypic.com/10xa9m9.jpg

表创建语句

-- STOCK
CREATE TABLE [dbo].[Stock](
    [StockId] [varchar](50) NOT NULL,
    [Alias] [varchar](50) NULL,
    [Description] [varchar](max) NOT NULL,
    [PartNumber] [varchar](250) NULL,
    [StockImage] [image] NULL,
    [IsRestricted] [bit] NOT NULL,
    [Notes] [varchar](max) NULL,
 CONSTRAINT [PK_Stock] PRIMARY KEY CLUSTERED 
(
    [StockId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[Stock] ADD  CONSTRAINT [DF_Stock_Restricted]  DEFAULT ((0)) FOR [IsRestricted]
GO

-- BIN
CREATE TABLE [dbo].[Bin](
    [BinId] [int] IDENTITY(1,1) NOT NULL,
    [BinCode] [varchar](50) NULL,
    [WarehouseCode] [varchar](50) NOT NULL,
    [Name] [varchar](max) NOT NULL,
 CONSTRAINT [PK_Bin_1] PRIMARY KEY CLUSTERED 
(
    [BinId] 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

ALTER TABLE [dbo].[Bin]  WITH CHECK ADD  CONSTRAINT [FK_Bin_Warehouse] FOREIGN KEY([WarehouseCode])
REFERENCES [dbo].[Warehouse] ([WarehouseCode])
GO

ALTER TABLE [dbo].[Bin] CHECK CONSTRAINT [FK_Bin_Warehouse]
GO

-- STOCKBINQUANTITY
CREATE TABLE [dbo].[StockBinQuantity](
    [StockBinQuantityId] [int] IDENTITY(1,1) NOT NULL,
    [StockId] [varchar](50) NOT NULL,
    [BinId] [int] NOT NULL,
    [QuantityOnHand] [int] NOT NULL,
    [QuantityOnOrder] [int] NOT NULL,
    [ReorderPoint] [int] NOT NULL,
 CONSTRAINT [PK_StockBinQuantity] PRIMARY KEY NONCLUSTERED 
(
    [StockBinQuantityId] 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

ALTER TABLE [dbo].[StockBinQuantity]  WITH CHECK ADD  CONSTRAINT [FK_StockBin_Stock] FOREIGN KEY([StockId])
REFERENCES [dbo].[Stock] ([StockId])
GO

ALTER TABLE [dbo].[StockBinQuantity] CHECK CONSTRAINT [FK_StockBin_Stock]
GO

ALTER TABLE [dbo].[StockBinQuantity]  WITH CHECK ADD  CONSTRAINT [FK_StockBinQuantity_Bin] FOREIGN KEY([BinId])
REFERENCES [dbo].[Bin] ([BinId])
GO

ALTER TABLE [dbo].[StockBinQuantity] CHECK CONSTRAINT [FK_StockBinQuantity_Bin]
GO

1 个答案:

答案 0 :(得分:1)

感谢大家的评论 - 你是正确的,因为修改select语句会加快查询速度。在我的案例中,除了Stock上的图像列之外的每一列都提供了一个小时的查询运行时间,你帮我确定了这一个减缓它的字段。

我还找到How to improve performance in SQL Server table with image fields?,我会调查。

因此,在你的帮助下,我已经解决了为什么它在sql中很慢而且我可以忽略那个字段。现在我需要改进我的linq语句以反映这一点。

我同意这个密钥是多余的,但它在一些地方被现有的代码和表引用,并没有明显减慢我的查询速度。