SQL Server Performance issue in stored procedure

mlnl4t2r  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(103)

I need to improve performance of my stored procedure. Please find my stored procedure below which works fine, but it takes more than 5 minutes for some time not always to get the result due to that it's causing timeout from API.

Please note: @Include and @BeginDate are the stored procedure input parameters:

  1. DECLARE @BeginDate datetime ='2023-09-28'
  2. DECLARE @from datetime
  3. DECLARE @to datetime
  4. DECLARE @Include BIT = 1
  5. SET @from = DATEADD(dd, DATEDIFF(dd, 0, @BeginDate), 0)
  6. SET @to = DATEADD(dd, DATEDIFF(dd, -1, @BeginDate), 0)
  7. IF OBJECT_ID('tempdb..#results') IS NOT NULL
  8. DROP TABLE #results
  9. CREATE TABLE #results
  10. (
  11. [BPID] [nvarchar](32) NOT NULL,
  12. [DId] [bigint] NULL,
  13. [Number] [nvarchar](32) NULL,
  14. [Account] [nvarchar](32) NULL,
  15. [LineOfBusinessDesc] [nvarchar](4) NULL,
  16. [LineOfBusiness] [varchar](35) NULL,
  17. [Channel] [varchar](35) NULL,
  18. [ProductLine] [varchar](35) NULL,
  19. [BusinessSegment] [varchar](35) NULL,
  20. [LineOfBusinessType] [varchar](35) NULL
  21. )
  22. INSERT INTO #results
  23. SELECT
  24. [PID], [DId], [Number], [Account],
  25. NULL, NULL, NULL, NULL, NULL
  26. FROM
  27. dbo.DailyDataInfo
  28. WHERE
  29. CreatedDate >= @from
  30. AND CreatedDate < @to
  31. IF @Include = 1
  32. BEGIN
  33. INSERT INTO #results
  34. SELECT
  35. a.[PID], [DId], a.[Number], a.[Account],
  36. a.[LineOfBusiness], b.[LineOfBusiness],
  37. b.[Channel], b.[ProductLine], b.[BusinessSegment]
  38. FROM
  39. dbo.OtherDailyData a
  40. OUTER APPLY
  41. (SELECT TOP 1 Id,
  42. x.Data.value('(//Data/Company/LineOfBusiness)[1]', 'varchar(30)') AS LineOfBusiness,
  43. x.Data.value('(//Data/Company/Channel)[1]', 'varchar(30)') AS Channel,
  44. x.Data.value('(//Data/Company/ProductLine)[1]', 'varchar(25)') AS ProductLine,
  45. x.Data.value('(//Data/Company/BusinessSegment)[1]', 'varchar(25)') AS BusinessSegment,
  46. FROM
  47. dbo.TermData x
  48. WHERE
  49. x.Reference = a.Number
  50. ORDER BY
  51. x.Id DESC) b
  52. WHERE
  53. CreatedDate >= @from AND CreatedDate < @to
  54. END
  55. SELECT * FROM #results

This is the sample table data along with Index information for dbo.DailyDataInfo: I don't see any issue with this table, based on execution plan

CreatedDate – is the non-unique, non-clustered index. There are no other indexes on this table

  1. BPID Id Number Account LineOfBusiness Createddate
  2. F886A11A6546199 1 9203919023 9203919023 HH 10/2/23 8:04 PM
  3. 1802063B1312516 2 9203919031 9203919031 KJ 10/2/23 8:04 PM
  4. a4DEEF472650CB8 3 9203905782 9203905782 KJ 10/2/23 8:04 PM
  5. 05D23BE7D263582 4 9203908786 9203908786 HHH 10/2/23 8:04 PM

This is the sample table data along with Index information for dbo.OtherDailyData : I don't see any issue with this table, based on execution plan.

Did is the primary Key and it is a clustered index. There are no other indexes on this table.

  1. BPId DId Number CreatedDate
  2. 9FE25361398013BF 64 9340733736 10/2/23 8:05 PM
  3. 20C072C8596503A 68 9340732569 10/2/23 8:01 PM
  4. 6526588B6CFC49A 72 9340733502 10/2/23 8:02 PM

This is the sample table data along with Index information for dbo.TermData - I see issue with this table, based on execution plan

  • TermId - is the primary key
  • Reference - is the non-unique, non-clustered index

Data:

  1. TermId Reference Data
  2. ------------------------------------------------------------------
  3. 321432 9340703401 This is the xml column please refer the below sample value

Data column value

  1. <Data>
  2. <Allow>1</Allow>
  3. <EligibilityFlag>0</EligibilityFlag>
  4. <Company>
  5. <LineOfBusiness>CCC</LineOfBusiness>
  6. <LineOfBusinessType>
  7. <LOBType>CommercialAuto</LOBType>
  8. </LineOfBusinessType>
  9. <Channel />
  10. <ProductLine>Trad</ProductLine>
  11. <BusinessSegment>E</BusinessSegment>
  12. </Company>
  13. <Info>
  14. <Auditable>0</Auditable>
  15. </Info>
  16. </Data>

Based on my analysis on execution plan, TermData table lookup is taking too much time, especially the XML lookup - 12% on each columns retrieval.

[![Execution plan][1]][1]

[![TermData Table][2]][2]

fdbelqdn

fdbelqdn1#

First, don't use // in your XPATH. It searches the whole document. Instead path from the root / to exactly the node you need. Then consider an XML Index , or materializing those three columns .

And watch this classic video where Michael Rys walks you through all the SQLXML best practices.

mcdcgff0

mcdcgff02#

This moves the WHERE logic ahead of the OUTER APPLY...

  1. DECLARE @BeginDate datetime ='2023-09-28'
  2. DECLARE @from datetime
  3. DECLARE @to datetime
  4. DECLARE @Include BIT = 1
  5. SET @from = DATEADD(dd, DATEDIFF(dd, 0, @BeginDate), 0)
  6. SET @to = DATEADD(dd, DATEDIFF(dd, -1, @BeginDate), 0)
  7. SELECT @from, @to
  8. IF OBJECT_ID('tempdb..#results') IS NOT NULL
  9. DROP TABLE #results
  10. CREATE TABLE #results
  11. (
  12. [BPID] [nvarchar](32) NOT NULL,
  13. [DId] [bigint] NULL,
  14. [Number] [nvarchar](32) NULL,
  15. [Account] [nvarchar](32) NULL,
  16. [LineOfBusinessDesc] [nvarchar](4) NULL,
  17. [LineOfBusiness] [varchar](35) NULL,
  18. [Channel] [varchar](35) NULL,
  19. [ProductLine] [varchar](35) NULL,
  20. [BusinessSegment] [varchar](35) NULL,
  21. [LineOfBusinessType] [varchar](35) NULL
  22. )
  23. INSERT INTO #results
  24. SELECT
  25. [PID], [DId], [Number], [Account],
  26. NULL, NULL, NULL, NULL, NULL, NULL
  27. FROM
  28. dbo.DailyDataInfo
  29. WHERE
  30. CreatedDate >= @from
  31. AND CreatedDate < @to
  32. IF @Include = 1
  33. BEGIN
  34. INSERT INTO #results
  35. SELECT
  36. a.[PID], [DId], a.[Number], a.[Account],
  37. a.[LineOfBusiness], b.[LineOfBusiness],
  38. b.[Channel], b.[ProductLine], b.[BusinessSegment], NULL
  39. FROM
  40. (
  41. SELECT n.[PID], n.[DId], n.[Number],
  42. n.[Account], n.[LineOfBusiness]
  43. FROM dbo.OtherDailyData a
  44. WHERE
  45. CreatedDate >= @from AND CreatedDate < @to
  46. ) a
  47. OUTER APPLY
  48. (SELECT TOP 1 Id,
  49. x.Data.value('(//Data/Company/LineOfBusiness)[1]', 'varchar(30)') AS LineOfBusiness,
  50. x.Data.value('(//Data/Company/Channel)[1]', 'varchar(30)') AS Channel,
  51. x.Data.value('(//Data/Company/ProductLine)[1]', 'varchar(25)') AS ProductLine,
  52. x.Data.value('(//Data/Company/BusinessSegment)[1]', 'varchar(25)') AS BusinessSegment
  53. FROM
  54. dbo.TermData x
  55. WHERE
  56. x.Reference = a.Number
  57. ORDER BY
  58. x.Id DESC) b
  59. END
  60. SELECT * FROM #results
展开查看全部

相关问题