1

I have a Problem with the following Query. It works and gives me the needed result, BUT it is quite slow, because of my subselects with joins. My TSQL skills are not good enough to optimize this Query. Maybe some of you can help.

SELECT
    Id AS InstanceId,
    (SELECT COUNT(DISTINCT tiq.Id) AS Expr1
     FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
     WHERE  (tpr.ProcessResultId = 32)) AS Received,
    (SELECT COUNT(DISTINCT tiq.Id) AS Expr1
     FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
     WHERE        (tiq.InMsgStateFlags = 1)) AS Queued,
    (SELECT COUNT(DISTINCT tiq.Id) AS Expr1
     FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
     WHERE  (tiq.InMsgStateFlags = 2)) AS Processed,
    (SELECT COUNT(DISTINCT tiq.Id) AS Expr1
     FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
     WHERE  (tiq.InMsgStateFlags = 128)) AS Error,
    (SELECT COUNT(DISTINCT tiq.Id) AS Expr1
     FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
     WHERE  (tiq.InMsgStateFlags = 256)) AS Rejected,
    (SELECT COUNT(tiq.Id) AS Expr1
     FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
     WHERE  (tpr.ProcessResultId = 16)) AS Duplicates,
    (SELECT COUNT(DISTINCT tiq.Id) AS Expr1
     FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
     WHERE  (tpr.ProcessResultId = 2)) AS Nack 
FROM
    dbo.tInstanceConfig AS tic 
WHERE
    (InstanceGroupId = 1)

This Query runs as a View.

If you Need further Information, let me know.

Here are the table Informations

tProcessLog

    [Id] [bigint] IDENTITY(1,1) NOT NULL,
[InstanceConfigId] [int] NOT NULL,
[InputQueueEntryId] [bigint] NOT NULL,
[OutputQueueEntryId] [bigint] NULL,
[ProcessingDtm] [datetime] NOT NULL,
[ProcessResultId] [int] NOT NULL,
[SeverityId] [int] NOT NULL,
[LogText] [varchar](max) NOT NULL,
[IsHidden] [bit] NOT NULL,
[ReceiverId] [int] NOT NULL,

tInputQueueEntry

    [Id] [bigint] IDENTITY(1,1) NOT NULL,
[Created] [datetime] NOT NULL,
[DataTypeId] [int] NOT NULL,
[CodePage] [int] NOT NULL,
[InMsgStateFlags] [int] NOT NULL,
[ContentData] [nvarchar](max) NOT NULL,
[ContentHash] [nvarchar](32) NOT NULL,

tInstanceConfig

    [Id] [int] IDENTITY(1,1) NOT NULL,
[IsActive] [bit] NOT NULL,
[Type] [varchar](50) NOT NULL,
[Description] [varchar](50) NOT NULL,
[ComponentConfig] [xml] NOT NULL,
[InstanceGroupId] [int] NOT NULL,

UPDATE

Finally, i think there is no way to get much more Performance. I used the MSSQL Tuning Advisor which had some recommendations, now the query tooks ~500ms. That is okay for me.

Thanks to all answers ! I doesnt help the Performance issue, but i learned cool new tsql stuff :)

9
  • 2
    I think you can use one JOIN instead of all inner-selects with use COUNT(DISTINCT CASE WHEN <someConditions> THEN tiq.Id END) ;).
    – shA.t
    Sep 29, 2015 at 6:30
  • 1
    i believe that only 1 return value is accepted in a subselect :(
    – S.L.
    Sep 29, 2015 at 6:31
  • 1
    Could you add what execution plans this produces? And what are indexes on your table? It would be helpful. Sep 29, 2015 at 6:46
  • 2
    What happens if you remove all DISTINCT? (not the correct result of course but is there a big difference in speed?)
    – adrianm
    Sep 29, 2015 at 6:50
  • 2
    You should rewrite this query using joins. If this is not possible, run this query in SSMS and view "actual execution plan". It might suggest some indexes that you can use to optimize query.
    – Salman A
    Sep 29, 2015 at 6:57

3 Answers 3

3

Like this?

SELECT
    tic.Id AS InstanceId,
    count.*
FROM
    dbo.tInstanceConfig AS tic 
    OUTER APPLY
    (
        SELECT 
            COUNT(DISTINCT CASE WHEN tpr.ProcessResultId = 32 THEN tiq.Id END) AS Received,
            COUNT(DISTINCT CASE WHEN tiq.InMsgStateFlags = 1 THEN tiq.Id END) AS Queued,
            COUNT(DISTINCT CASE WHEN tiq.InMsgStateFlags = 2 THEN tiq.Id END) AS Processed,
            COUNT(DISTINCT CASE WHEN tiq.InMsgStateFlags = 128 THEN tiq.Id END) AS Error,
            COUNT(DISTINCT CASE WHEN tiq.InMsgStateFlags = 256 THEN tiq.Id END) AS Rejected,
            COUNT(DISTINCT CASE WHEN tpr.ProcessResultId = 16 THEN tiq.Id END) AS Duplicates,
            COUNT(DISTINCT CASE WHEN tpr.ProcessResultId = 2 THEN tiq.Id END) AS Nack
        FROM   
            dbo.tInputQueueEntry AS tiq 
            INNER JOIN dbo.tProcessLog AS tpr ON tpr.InstanceConfigId = tic.Id AND tpr.InputQueueEntryId = tiq.Id
    ) count
WHERE
    (InstanceGroupId = 1)
8
  • 1
    I will try, give me a second
    – S.L.
    Sep 29, 2015 at 6:33
  • 1
    Msg 156, Level 15, State 1, Line 16 Incorrect syntax near the keyword 'FROM'.
    – S.L.
    Sep 29, 2015 at 6:34
  • 2
    There's a typo in the code. COUNT(DISTINCT CASE WHEN tpr.ProcessResultId = 2 THEN tiq.Id END) AS Nack, last comma is redundant. Sep 29, 2015 at 6:35
  • 1
    Now it works, but it is 2 times slower than my initial query :) 2 seconds
    – S.L.
    Sep 29, 2015 at 6:36
  • 1
    @S.L. Oh, may be the tProcessLog table is too big to run those 7 duplicate Count() for null entries
    – Eric
    Sep 29, 2015 at 6:40
2

DECLARE @Received INT,@Queued INT,@Processed INT,@Error INT,@Rejected INT,@Duplicates INT,@Nack INT

SET @Received =  SELECT COUNT(DISTINCT tiq.Id) AS Expr1
         FROM   
            dbo.tInputQueueEntry AS tiq 
         INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InputQueueEntryId = tiq.Id
         INNER JOIN dbo.tInstanceConfig AS tic ON tpr.InstanceConfigId = tic.Id
         WHERE  (tpr.ProcessResultId = 32)

SET @Queued =  SELECT COUNT(DISTINCT tiq.Id) AS Expr1
         FROM   
            dbo.tInputQueueEntry AS tiq 
        INNER JOIN
            dbo.tProcessLog AS tpr ON tpr.InputQueueEntryId = tiq.Id
         INNER JOIN dbo.tInstanceConfig AS tic ON tpr.InstanceConfigId = tic.Id
         WHERE        (tiq.InMsgStateFlags = 1) 

SET @Processed =  SELECT COUNT(DISTINCT tiq.Id) AS Expr1
                 FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
                        dbo.tProcessLog AS tpr ON tpr.InputQueueEntryId = tiq.Id
                        INNER JOIN dbo.tInstanceConfig AS tic ON tpr.InstanceConfigId = tic.Id
                 WHERE  (tiq.InMsgStateFlags = 2)   

SET @Error   = SELECT COUNT(DISTINCT tiq.Id) AS Expr1
                 FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
                        dbo.tProcessLog AS tpr ON tpr.InputQueueEntryId = tiq.Id
                        INNER JOIN dbo.tInstanceConfig AS tic ON tpr.InstanceConfigId = tic.Id 
                 WHERE  (tiq.InMsgStateFlags = 128)

SET @Rejected = SELECT COUNT(DISTINCT tiq.Id) AS Expr1
             FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
                    dbo.tProcessLog AS tpr ON tpr.InputQueueEntryId = tiq.Id
                    INNER JOIN dbo.tInstanceConfig AS tic tpr.InstanceConfigId = tic.Id
             WHERE  (tiq.InMsgStateFlags = 256) 

SET @Duplicates = SELECT COUNT(tiq.Id) AS Expr1
                 FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
                        dbo.tProcessLog AS tpr ON tpr.InputQueueEntryId = tiq.Id
                        INNER JOIN dbo.tInstanceConfig AS tic ON tpr.InstanceConfigId = tic.Id
                 WHERE  (tpr.ProcessResultId = 16)  
SET @Nack = SELECT COUNT(DISTINCT tiq.Id) AS Expr1
			 FROM   dbo.tInputQueueEntry AS tiq INNER JOIN
					dbo.tProcessLog AS tpr ON tpr.InputQueueEntryId = tiq.Id
					INNER JOIN dbo.tInstanceConfig AS tic ON tpr.InstanceConfigId = tic.Id
			 WHERE  (tpr.ProcessResultId = 2)                 

SELECT
    Id AS InstanceId,
    @Received AS Received,
    @Queued AS Queued,
    @Processed AS Processed,
    @Error AS Error,
    @Rejected AS Rejected,
    @Duplicates AS Duplicates,
    @Nack AS Nack 
FROM
    dbo.tInstanceConfig AS tic 
WHERE
    (InstanceGroupId = 1)

7
  • 1
    thanks ! but i cannot execute this query. there are several Syntax errors
    – S.L.
    Sep 29, 2015 at 9:33
  • 1
    you Need braces around each select ... but there is one error left .. he doesnt know "tic.Id" in each select
    – S.L.
    Sep 29, 2015 at 9:35
  • 1
    Msg 4104, Level 16, State 1, Procedure Test, Line 37 The multi-part identifier "tic.Id" could not be bound.
    – S.L.
    Sep 29, 2015 at 9:36
  • 1
    check now i have added one join condition Sep 29, 2015 at 9:59
  • 2
    Sorry i just forgot to add filter condition in all variable output, if you want this logic then can add : and (InstanceGroupId = 1) in each declare variable query. Sep 29, 2015 at 10:32
1

Cant you GROUP BY using InMsgStateFlags and get the count(1) value since other conditions are same? Just write one SELECT statement.

1
  • 2
    do you have any example ? I have no idea what i have to do
    – S.L.
    Sep 29, 2015 at 6:42

Your Answer

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

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