0

我们的客户抱怨以下 SQL 视图导致他们的主要信息系统阻塞。我需要确定它是否属实(因为他们当时也在做其他事情,他们不确定),我想解决这个问题。

背景:以下是简化的旧版 SQL 代码,用于从安装在 Microsoft SQL Server 2008 上的 Navision(现在为 Microsoft Dynamics-Nav 或他们如何称呼它)中提取数据。

该视图用于从 SSIS 包中将数据提取到具有固定列宽的 txt 文件中。生成的文件被导入到另一个与 Navision 没有直接关系的软件中。该包通过 SQL Server 代理作为作业启动。运行大约需要 10 分钟。

如何确定数据提取过程是否阻塞了其他活动?SELECT 总是自动阻塞吗?如果是,是否可以以某种方式改善视图以避免阻塞?

CREATE VIEW [A_Company$my_view] AS
SELECT ItemLedgerEntry.[Document No_] AS DocumentNo,
    ...
    CAST(CONVERT(DECIMAL(14,2),
                 ROUND((SELECT SUM([Sales Amount (Actual)])
                        FROM [A_Company$Value Entry] AS ValueEntry
                        WHERE ValueEntry.[Item Ledger Entry No_] = ItemLedgerEntry.[Entry No_]),
                       2)) AS VARCHAR(14)) AS SalesAmount,
    ...
    Dim1.[Dimension Value Code] AS Dim1,
    ...
    COALESCE((select top 1 [Group Code]
              from [A_Company$Statistic Group Accom]
              where [Type] = 1
                  and [Sales Code] = ItemLedgerEntry.[Source No_]
                  and [Ship-to Code] = ItemLedgerEntry.[Source No_ 3]
                  and [Starting Date] <= ItemLedgerEntry.[Posting Date]
              order by [Starting Date] desc),
             (select top 1 [Group Code]
              from [A_Company$Statistic Group Accom]
              where [Type] = 1
                  and [Sales Code] = ItemLedgerEntry.[Source No_]
                  and [Ship-to Code] = ''
                  and [Starting Date] <= ItemLedgerEntry.[Posting Date]
              order by [Starting Date] desc), ''
              ) as StatisticGroup

FROM [A_Company$Item Ledger Entry] AS ItemLedgerEntry
    LEFT OUTER JOIN [A_Company$Item] AS Item ON (Item.[No_] = ItemLedgerEntry.[Item No_])
    ...
    LEFT OUTER JOIN [A_Company$Salesperson_Purchaser] AS Salesperson_Purchaser
                 ON (Salesperson_Purchaser.[Code] = Customer.[Salesperson Code])

    LEFT OUTER JOIN [A_Company$Ledger Entry Dimension] AS Dim1
      ON (ItemLedgerEntry.[Entry No_] = Dim1.[Entry No_]) AND (Dim1.[Table ID] = 32) AND
         (Dim1.[Dimension Code] = (SELECT [Shortcut Dimension 1 Code] FROM [A_Company$General Ledger Setup]))
    ...
    LEFT OUTER JOIN [A_Company$Ledger Entry Dimension] AS Dim8
      ON (ItemLedgerEntry.[Entry No_] = Dim8.[Entry No_]) AND (Dim8.[Table ID] = 32) AND
         (Dim8.[Dimension Code] = (SELECT [Shortcut Dimension 8 Code] FROM [A_Company$General Ledger Setup]))

WHERE ((ItemLedgerEntry.[Invoiced Quantity] <> 0)
      AND (ItemLedgerEntry.[Entry Type] = 1))

这是我试图保留所有我认为可能导致问题的功能的真实代码(删除了重复的部分)。是否有任何 StackExchange 论坛,我可以在其中发布完整的代码以供审查?

避免阻塞是第一步。你能想到性能方面的任何改进吗?(我不擅长 SQL 编程。)

非常感谢您的时间,彼得

4

1 回答 1

2

在某种程度上,这取决于其他进程在做什么。他们要求什么样的锁。

您可以尝试WITH (Nolock)在上述查询中的每个表定义之后添加。

IE

FROM [A_Company$Item Ledger Entry] AS ItemLedgerEntry WITH (NOLOCK)
于 2012-08-30T08:00:07.957 回答