Our customer complains that the following SQL view causes blocking of their primary information system. I need to find if it is true (as they do also other things in the time and they are not sure), and I would like to fix the situation.
Background: The following is the simplified legacy SQL code that is used to extract the data from the Navision (now Microsoft Dynamics-Nav or how they call it) installed on Microsoft SQL Server 2008.
The view is used from a SSIS package to extract the data to txt files with fixed column width. The resulting files are imported to another software that is otherwise not directly related to Navision. The package is launched as a job via the SQL Server Agent. It takes about 10 minutes to run.
How can I find whether the data extraction process blocks the other activities? Is the SELECT always blocking automatically? If yes, can the view be improved somehow to avoid blocking?
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))
This is the real code where I tried to preserve all features that I think could cause the problems (the repeated parts removed). Is there any StackExchange forum where I can post the full code for review?
To avoid blocking is the first steps. Can you think about any improvement concerning the performance? (I am not good in SQL programming.)
Thanks a lot for your time,
Petr
To some extent, this depend what the other processes are doing. What kind of locks are they requesting.
You could try adding
WITH (Nolock)after each of the table definitions in the above query.ie