简体   繁体   English

使用具有中等数据量的表提高SQL Server查询性能

[英]Improve SQL Server query performance with table having medium data

I have a table with 6000 records but it could be larger in the future. 我有一张有6000条记录的表,但将来可能会更大。 I have attached screen dump of my current SQL query which takes 12 to 14 seconds to load 6000 rows of data. 我已附加了当前SQL查询的屏幕转储,这需要12到14秒才能加载6000行数据。

There are already indexes on the columns which are queried most commonly 列上已经有最常查询的索引

How can I improve query performance or modify query for the same? 如何提高查询性能或修改查询?

CREATE TABLE [dbo].[Item]
  (
     [Srno]          [BIGINT] IDENTITY(1, 1) NOT NULL,
     [ITEMCode]      [BIGINT] NOT NULL,
     [BranchId]      [BIGINT] NOT NULL,
     [ITEM_No]       [VARCHAR](50) NULL,
     [ITEM_Desc]     [VARCHAR](200) NULL,
     [DeptId]        [BIGINT] NULL,
     [CatId]         [BIGINT] NULL,
     [SizeId]        [BIGINT] NULL,
     [CostPrice]     [DECIMAL](18, 3) NULL,
     [SalesPrice]    [DECIMAL](18, 2) NULL,
     [ITEM_InStock]  [BIGINT] NULL,
     [UserId]        [BIGINT] NULL,
     [Active]        [BIT] NOT NULL,
     [IsDeleted]     [BIT] NOT NULL,
     [Quantity]      [INT] NULL,
     [IsFavourite]   [BIT] NULL,
     [IsProductLink] [BIT] NULL,
     CONSTRAINT [PK_Item_1] PRIMARY KEY CLUSTERED ( [ITEMCode] ASC, [BranchId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  )
ON [PRIMARY]

ALTER TABLE [dbo].[Item]
  WITH CHECK ADD CONSTRAINT [FK_Item_Item] FOREIGN KEY([ITEMCode], [BranchId]) REFERENCES [dbo].[Item] ([ITEMCode], [BranchId])

GO

ALTER TABLE [dbo].[Item]
  CHECK CONSTRAINT [FK_Item_Item]

GO

ALTER TABLE [dbo].[Item]
  ADD CONSTRAINT [DF_Item_ITEM_UnitPrice] DEFAULT ((0)) FOR [CostPrice]

GO

ALTER TABLE [dbo].[Item]
  ADD CONSTRAINT [DF_Item_ITEM_RETAILPRICE] DEFAULT ((0)) FOR [SalesPrice]

GO

ALTER TABLE [dbo].[Item]
  ADD CONSTRAINT [DF_Item_ITEM_InStock] DEFAULT ((0)) FOR [ITEM_InStock]

GO

ALTER TABLE [dbo].[Item]
  ADD CONSTRAINT [DF_Item_Active] DEFAULT ((0)) FOR [Active]

GO

ALTER TABLE [dbo].[Item]
  ADD CONSTRAINT [DF_Item_DelFlag] DEFAULT ((0)) FOR [IsDeleted]

GO 

I have updated query and removed join of Department table but still it takes 10 Seconds 我已经更新查询并删除了Department表的联接,但仍然需要10秒

   SELECT Item.ITEMCode                 AS ItemId,
       ISNULL(Item.ITEM_No, '0')     AS ItemNo,
       isnull(Item.Barcode, '')      AS Barcode,
       Item.ITEM_Desc                AS ItemName,
       isnull(item_Imagepath, '')    AS ItemImage,
       0                             AS availableQty,
       Item.SalesPrice               AS Price,
       isnull(Item.TaxApply, 0)      AS isTax,
       Item.TaxType,
       ISNULL(Item.ITM_Type, '0')    AS ITMType,
       Item.Profit_Type              AS ProfitType,
       Item.Profit_Amt               AS ProfitAmt,
       Item.CostPrice,
       ISNULL(Item.ITEM_Remarks, '') AS Remark
FROM   Item      

在此处输入图片说明

If it takes 10 seconds to read 6000 (small) records then I can only assume/hope that your connection is introducing a lot of delays here!?! 如果读取6000条(小型)记录需要10秒钟,那么我只能假定/希望您的连接在这里引入了很多延迟!

Could you try the following and see how long this takes? 您能否尝试以下方法,看看需要多长时间?

SET STATISTICS TIME ON

SELECT * INTO #test FROM [Item]

If this runs quickly then I'd go look for ways to speed up your connection between your computer and the Azure severs. 如果运行速度很快,那么我将寻找加快您的计算机与Azure服务器之间连接的方法。 (network packet size?) (网络数据包大小?)

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

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