0

I have following query, which is working as expected but taking approx 3 seconds to execute. Reason is large number of records. Can somebody please suggest any steps in order to improve performance?

Explanation :

  1. Check to see value using Comp id and Default_Comp = 1
  2. If not found, ignore the Default_Comp and check only based on Comp id
  3. Still not found, ignore the join with table 2 and try to get by Comp id.

My code:

DECLARE @Finished_Comp VARCHAR(MAX) = NULL;

SELECT @Finished_Comp = MIN(tbl2.Finished_Comp) 
FROM Table1 tbl1 
INNER JOIN Table2 tbl2 ON tbl1.Sav_ID = tbl2.Sav_ID
WHERE Comp_ID = @Comp_ID AND tbl1.Default_Comp = 1 

IF @Finished_Comp IS NULL 
BEGIN   
    SELECT @Finished_Comp = MIN(tbl2.Finished_Comp) 
    FROM Table1 tbl1 
    INNER JOIN Table2 tbl2 ON tbl1.Sav_ID = tbl2.Sav_ID 
    WHERE Comp_ID = @Comp_ID 
END

IF @Finished_Comp IS NULL  
BEGIN 
    SELECT @Finished_Comp = MIN(Finished_Comp) 
    FROM Table1 tbl1 
    WHERE Comp_ID = @Comp_ID AND @Finished_Comp != '' 
END

I tried to use COALESCE, but it's returning wrong results for Finished_Comp

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Manish Dhariwal
  • 61
  • 1
  • 1
  • 9
  • 3
    Please do post your table's **full** DDL along with any indexes on them. – Thom A Jan 21 '19 at 10:22
  • @Larnu : Thank you for your response. I am not able to generate DDL at this time. No indexes are there on table. Can you please suggest simplified query or something. That would be much appreciated. COALESCE is not working as expected here. Thank you. – Manish Dhariwal Jan 21 '19 at 10:25
  • 3
    Indexes are going to be the thing that helps here. Without them, you're forcing your RDBMS to scan the table; something which is only going to get slower and slower the more data you have. – Thom A Jan 21 '19 at 10:26
  • @Larnu : Agreed. What happens here is, 3 times query needs to be executed in above case. However, i'm trying to do something so that i can get result in one go,if possible. – Manish Dhariwal Jan 21 '19 at 10:28
  • Possible duplicate of [Improve an application performance by adding indexes to a database](https://stackoverflow.com/questions/28529696/improve-an-application-performance-by-adding-indexes-to-a-database) – Peter B Jan 21 '19 at 10:40
  • 1
    @PeterB : The question you have suggested as a duplicate talks about general steps regarding performance. e.g. Indexes. I strongly believe the query can be changed to some extent so that no multiple queries need to be executed. Problem is i'm just not able to figure out how. Also, if you look at questions this way, not a single question is new on this forum. Every time tagging question as a duplicate doesn't help. Really. – Manish Dhariwal Jan 21 '19 at 12:58
  • Start by adding indexes on Comp_ID + Sav_ID, optionally including other fields (Default_Comp, Finished_Comp) - maybe you don't need redesign your queries at all. What are primary keys, BTW? – Arvo Jan 21 '19 at 14:39

1 Answers1

0

You say in the comments

I strongly believe the query can be changed to some extent so that no multiple queries need to be executed.

Yes you're right.

SELECT @Finished_Comp = COALESCE(MIN(CASE WHEN tbl1.Default_Comp = 1 THEN tbl2.Finished_Comp END), 
                                 MIN(tbl2.Finished_Comp), 
                                 MIN(CASE WHEN tbl1.Finished_Comp <> '' THEN tbl1.Finished_Comp END))
FROM   Table1 tbl1
       LEFT JOIN Table2 tbl2
              ON tbl1.Sav_ID = tbl2.Sav_ID
WHERE  tbl1.Comp_ID = @Comp_IDV 

But at best this will only reduce execution time to a third of current (for the case that all three queries need to be executed).

You should consider adding indexes on

  • Table1 - Comp_ID, Sav_ID INCLUDE (Default_Comp, Finished_Comp)
  • Table2 - Sav_ID INCLUDE (Finished_Comp)

For potentially much larger improvements.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • Excellent. This resolved the issue. The code block was the main issue and now whole stored procedure is running much faster. It looks like no indexes needed as of now. Thanks a lot. – Manish Dhariwal Jan 22 '19 at 07:50