gpt4 book ai didi

sql-server-2005 - 从 .NET 应用程序到 SQL Server 的偶尔缓慢的调用

转载 作者:行者123 更新时间:2023-12-04 07:13:26 26 4
gpt4 key购买 nike

我在 SQL Server 中有一个表,它是从仍在生产中的遗留系统继承而来的,该系统根据以下代码构建。我创建了一个 SP 来查询表,如表创建语句下方的代码中所述。我的问题是,通过 Enterprise Library 4 和 DataReader 对象从 .NET 到此 SP 的调用偶尔会很慢。 SP 通过数据层中的循环结构调用,该结构指定进入 SP 以填充用户对象的参数。值得一提的是,不会在每次通过循环结构时都进行慢速调用。它通常会在一天或更长时间的大部分时间里都很好,然后开始展示,这使得调试变得非常困难。

有问题的表包含大约 500 万行。例如,缓慢的调用将需要长达 10 秒的时间,而快速的调用平均需要 0 到 10 毫秒。我在慢速调用期间检查了锁定/阻塞事务,没有找到。我在数据层创建了一些自定义性能计数器来监控调用时间。从本质上讲,当性能很差时,这对那个电话来说真的很糟糕。但是当它很好时,它真的很好。我已经能够在几台不同的开发人员机器上重现该问题,但不能在我们的开发和暂存数据库服务器上重现,这些服务器当然具有更强大的硬件。通常,通过重新启动 SQL Server 服务可以解决问题,但并非总是如此。表上有我正在查询的字段的索引,但是索引比我想要的要多。但是,由于它可能对遗留系统产生影响,我不愿删除任何索引或玩具。以前有没有人遇到过这样的问题,或者您有解决方法的建议吗?

CREATE TABLE [dbo].[product_performance_quarterly](
[performance_id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[product_id] [int] NULL,
[month] [int] NULL,
[year] [int] NULL,
[performance] [decimal](18, 6) NULL,
[gross_or_net] [char](15) NULL,
[vehicle_type] [char](30) NULL,
[quarterly_or_monthly] [char](1) NULL,
[stamp] [datetime] NULL CONSTRAINT [DF_product_performance_quarterly_stamp] DEFAULT (getdate()),
[eA_loaded] [nchar](10) NULL,
[vehicle_type_id] [int] NULL,
[yearmonth] [char](6) NULL,
[gross_or_net_id] [tinyint] NULL,
CONSTRAINT [PK_product_performance_quarterly_4_19_04] PRIMARY KEY CLUSTERED
(
[performance_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[product_performance_quarterly] WITH NOCHECK ADD CONSTRAINT [FK_product_performance_quarterlyProduct_id] FOREIGN KEY([product_id])
REFERENCES [dbo].[products] ([product_id])
GO
ALTER TABLE [dbo].[product_performance_quarterly] CHECK CONSTRAINT [FK_product_performance_quarterlyProduct_id]

CREATE PROCEDURE [eA.Analytics.Calculations].[USP.GetCalculationData]
(
@PRODUCTID INT, --products.product_id
@BEGINYEAR INT, --year to begin retrieving performance data
@BEGINMONTH INT, --month to begin retrieving performance data
@ENDYEAR INT, --year to end retrieving performance data
@ENDMONTH INT, --month to end retrieving performance data
@QUARTERLYORMONTHLY VARCHAR(1), --do you want quarterly or monthly data?
@VEHICLETYPEID INT, --what product vehicle type are you looking for?
@GROSSORNETID INT --are your looking gross of fees data or net of fees data?
)
AS
BEGIN

SET NOCOUNT ON

DECLARE @STARTDATE VARCHAR(6),
@ENDDATE VARCHAR(6),
@vBEGINMONTH VARCHAR(2),
@vENDMONTH VARCHAR(2)

IF LEN(@BEGINMONTH) = 1
SET @vBEGINMONTH = '0' + CAST(@BEGINMONTH AS VARCHAR(1))
ELSE
SET @vBEGINMONTH = @BEGINMONTH

IF LEN(@ENDMONTH) = 1
SET @vENDMONTH = '0' + CAST(@ENDMONTH AS VARCHAR(1))
ELSE
SET @vENDMONTH = @ENDMONTH

SET @STARTDATE = CAST(@BEGINYEAR AS VARCHAR(4)) + @vBEGINMONTH
SET @ENDDATE = CAST(@ENDYEAR AS VARCHAR(4)) + @vENDMONTH

--because null values for gross_or_net_id and vehicle_type_id are represented in
--multiple ways (true null, empty string, or 0) in the PPQ table, need to account for all possible variations if
--a -1 is passed in from the .NET code, which represents an enumerated value that
--indicates that the value(s) should be true null.

IF @VEHICLETYPEID = '-1' AND @GROSSORNETID = '-1'
SELECT
PPQ.YEARMONTH, PPQ.PERFORMANCE
FROM PRODUCT_PERFORMANCE_QUARTERLY PPQ
WITH (NOLOCK)
WHERE
(PPQ.PRODUCT_ID = @PRODUCTID)
AND (PPQ.YEARMONTH BETWEEN @STARTDATE AND @ENDDATE)
AND (PPQ.QUARTERLY_OR_MONTHLY = @QUARTERLYORMONTHLY)
AND (PPQ.VEHICLE_TYPE_ID IS NULL OR PPQ.VEHICLE_TYPE_ID = '0' OR PPQ.VEHICLE_TYPE_ID = '')
AND (PPQ.GROSS_OR_NET_ID IS NULL OR PPQ.GROSS_OR_NET_ID = '0' OR PPQ.GROSS_OR_NET_ID = '')
ORDER BY PPQ.YEARMONTH ASC

IF @VEHICLETYPEID <> '-1' AND @GROSSORNETID <> '-1'
SELECT
PPQ.YEARMONTH, PPQ.PERFORMANCE
FROM PRODUCT_PERFORMANCE_QUARTERLY PPQ
WITH (NOLOCK)
WHERE
(PPQ.PRODUCT_ID = @PRODUCTID)
AND (PPQ.YEARMONTH BETWEEN @STARTDATE AND @ENDDATE)
AND (PPQ.QUARTERLY_OR_MONTHLY = @QUARTERLYORMONTHLY)
AND (PPQ.VEHICLE_TYPE_ID = @VEHICLETYPEID )
AND (PPQ.GROSS_OR_NET_ID = @GROSSORNETID)
ORDER BY PPQ.YEARMONTH ASC

IF @VEHICLETYPEID = '-1' AND @GROSSORNETID <> '-1'
SELECT
PPQ.YEARMONTH, PPQ.PERFORMANCE
FROM PRODUCT_PERFORMANCE_QUARTERLY PPQ
WITH (NOLOCK)
WHERE
(PPQ.PRODUCT_ID = @PRODUCTID)
AND (PPQ.YEARMONTH BETWEEN @STARTDATE AND @ENDDATE)
AND (PPQ.QUARTERLY_OR_MONTHLY = @QUARTERLYORMONTHLY)
AND (PPQ.VEHICLE_TYPE_ID IS NULL OR PPQ.VEHICLE_TYPE_ID = '0' OR PPQ.VEHICLE_TYPE_ID = '')
AND (PPQ.GROSS_OR_NET_ID = @GROSSORNETID)
ORDER BY PPQ.YEARMONTH ASC

IF @VEHICLETYPEID <> '-1' AND @GROSSORNETID = '-1'
SELECT
PPQ.YEARMONTH, PPQ.PERFORMANCE
FROM PRODUCT_PERFORMANCE_QUARTERLY PPQ
WITH (NOLOCK)
WHERE
(PPQ.PRODUCT_ID = @PRODUCTID)
AND (PPQ.YEARMONTH BETWEEN @STARTDATE AND @ENDDATE)
AND (PPQ.QUARTERLY_OR_MONTHLY = @QUARTERLYORMONTHLY)
AND (PPQ.VEHICLE_TYPE_ID = @VEHICLETYPEID)
AND (PPQ.GROSS_OR_NET_ID IS NULL OR PPQ.GROSS_OR_NET_ID = '0' OR PPQ.GROSS_OR_NET_ID = '')
ORDER BY PPQ.YEARMONTH ASC

END

最佳答案

我已经看到过时的索引会发生这种情况。它也可能是参数嗅探问题,其中不同的查询计划被用于进入存储过程的不同参数。

您应该捕获慢速调用的参数,并在每次运行缓慢时查看它们是否相同。

您也可以尝试运行调整向导,看看它是否推荐任何索引。

在您可以证明更新和插入发生得太慢(修改索引加上锁定/争用所需的时间)或者您的磁盘空间不足之前,您不想担心索引过多。

关于sql-server-2005 - 从 .NET 应用程序到 SQL Server 的偶尔缓慢的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com