0

I am using a SQL query and sql server in my application to display data, it gives a major performance hit. When query gets executed the CPU and DTU goes till 100% It is a major performance issue.

Query:-

SELECT *
FROM   (SELECT Alls.*,
               ROW_NUMBER()
                 OVER (
                   ORDER BY Alls.col1 DESC) AS RowNum,
               COUNT(*)
                 OVER ()                    AS TotalCount
        FROM   tab1 AllS
               LEFT JOIN tab2 FF
                      ON Alls.col2 = FF.col2
        WHERE  ( ( ( @par1 IS NULL
                     AND 1 = 1 )
                    OR ( @par1 IS NOT NULL
                         AND '1' = fun1 (col3, ',', @par1, 'exact contains') ) )
                 AND ( ( @par2 IS NULL
                         AND 1 = 1 )
                        OR ( @par2 IS NOT NULL
                             AND ( Alls.col1 BETWEEN CONVERT(DATETIME, @par2) AND CONVERT(DATETIME, @par7) ) ) )
                 AND ( ( @par3 IS NULL
                         AND 1 = 1 )
                        OR ( @par3 IS NOT NULL
                             AND col4 IN (SELECT CONVERT(INT, Item)
                                          FROM   dbo.Split(@par3, ',')) ) )
                 AND ( ( ( @par4 IS NULL
                           AND col5 = NULL )
                          OR ( @par4 IS NOT NULL
                               AND col5 = @par4 ) )
                        OR ( ( @par5 IS NULL
                               AND col6 = NULL )
                              OR ( @par5 IS NOT NULL
                                   AND col6 = @par5 ) )
                        OR ( ( @par6 IS NULL
                               AND col7 = NULL )
                              OR ( @par6 IS NOT NULL
                                   AND col7 = @par6 ) )
                           AND ( ( @par8 IS NULL
                                   AND 1 = 1 )
                                  OR ( @par8 IS NOT NULL
                                       AND col8 IS NULL ) )
                           AND ( ( ( @par9 IS NULL
                                     AND 1 = 1 )
                                    OR ( @par9 IS NOT NULL
                                         AND col9 LIKE '%' + @par9 + '%' ) )
                                  OR ( ( @par9 IS NULL
                                         AND 1 = 1 )
                                        OR ( @par9 IS NOT NULL
                                             AND col8 = @par9 ) ) ) ) )
               AND col10 = 1
               AND col11 IS NULL) AS List
WHERE  RowNum BETWEEN @startRowIndex AND ( @startRowIndex + @pageSize ) - 1
ORDER  BY col1 DESC 

Please suggest how to optimize the query.

Here is the schema:

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE tab1( [ID] [int] IDENTITY(1,1) NOT NULL, [col2] [uniqueidentifier] NOT NULL, [col5] [bit] NOT NULL, [col6] [bit] NOT NULL, [col7] [bit] NOT NULL, [col10] [bit] NOT NULL, [col1] [datetime] NOT NULL, [col4] [datetime] NOT NULL, [col3] [nvarchar](1000) NULL, [col11] [bit] NULL, [col8] [nvarchar](max) NULL, CONSTRAINT [PK_Alls] PRIMARY KEY CLUSTERED ( [col2] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ) 
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE tab2( [Id] [int] NULL, [col2] [varchar](40) NULL, [col9] [varchar](max) NULL, [col1] [datetime] NULL ) GO SET ANSI_PADDING OFF GO
7
  • Which dbms are you using?
    – jarlh
    Mar 14, 2018 at 9:14
  • 1
    As with any performance issue - you need to tell us which database (I would guess sql server but you have to confirm that) show is the schema, the indexes available and the query plan would be helpful. the 'and 1=1' is unlikely to be the cause of your problems.
    – Andrew
    Mar 14, 2018 at 9:15
  • Yes it is a sql server Mar 14, 2018 at 9:16
  • 1
    Have to agree, got rid of all of the 1 = 1; it's a complete waste of the compilers time.
    – Thom A
    Mar 14, 2018 at 10:02
  • 1
    col = null can result in unexpected results, it should be col is null
    – GuidoG
    Mar 14, 2018 at 10:24

2 Answers 2

2

AND 1 = 1 is literally useless here

@par4 IS NULL AND col5 = NULL - won't work

hit 1

AND '1' = fun1 (col3, ',', @par1, 'exact contains')

hit 2

AND col4 IN (SELECT CONVERT(INT, Item) FROM dbo.Split(@par3, ','))

hit 3

AND col9 LIKE '%' + @par9 + '%' )

hit 4

Alls.col1 BETWEEN CONVERT(DATETIME, @par2) AND CONVERT(DATETIME, @par7)

hit 5

... OR ... OR ... OR ... OR ... OR ...

and the pagination itself too.

And please take a look at the link from Martin Smith.

3
  • I'm not sure that what you labeled as Hit 2 will actually be much of a hit; provided the OP has a good splitter. If it's a TVF or CLR it shouldn't be too bad; the CONVERT is on the parameter (well the result returned from it), so it's not going to cause it's to be non-SARGable; as the indexes will be on the tables. Of course, if the OP has a Multi-line Table Value Function, it'll be a huge hit and the OP should consider using a better solution. This doesn't, however, address the rest of the problems in that query. :)
    – Thom A
    Mar 14, 2018 at 10:28
  • 1
    I did not see those functions source so can't be saying for sure whether there is a big trouble with them or not. But considering the rest of the code I doubt a lot that there is everything okay with them :) Mar 14, 2018 at 10:35
  • Potential hit 6: the schema shows only one index created on the first table (primary key column), nothing in the OP suggests indices exist for any of the other columns.
    – easuter
    Mar 14, 2018 at 10:51
0

I have removed 1=1 and i believe it was not necessary as I am getting the same result. Then I tried to optimize the OR OR conditions. The main reason is the JOIN. Now, I am searching an alternative for the LEFT JOIN

It will be pleasure to have more suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.