cancel
Showing results for 
Search instead for 
Did you mean: 

Rewriting using joins

Former Member
0 Kudos
1,615

Hi,

The below query retrieves 5000 ids.Need to fine tune in such a way that it takes less time to execute.

select top 5000 ids 
  from table1 t1
  join table2 t2 on t1.class=t2.class 
                and t1.pid = t2.pid 
                and isnull(t.bkngpur, '') !='ABC'
                and (t.rspid = p.spid
                 or (isnull(t.rspid,'') = isnull(p.spid,'')))
  left join table3 t3 on t.cpid1 = c.cpid 
  left join table4 t4 on t.cpid1 = cp.cpid
 where status in ('A','B')
       and Tid != 'PR%' 
       and exDate > (select curdate from table5)
       and p.rep = 1
       and (rrtor in( 'ABC','DEF') 
            or (( Tid in (select tsid 
                            from table5
                           where bid in (select lbid 
                                           from table6
                                          where pbid in ('abc','def', 'ghi')) 
                                 or t4.indc = 1))
                 or (ids in (select tsid 
                               from table7
                              where bid in ( select lbid
                                               from table6
                                              where pbid where pbid in ('abc','def', 'ghi')
                                                    or t4.indc =1)
                             )
                ))
        and t3.col1 in (NULL,'XYZ)
        and t1.tpid in ('P','Q','R','S')
 order by 1

Thanks in advance

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

Thanks for your response.

1.This Query only gives a list of ids.

ids

1 2 3 . . . . 5000

Well this query is taking ~7 seconds to complete.Need to rewrite the query using joins instead of sub queries.

Breck_Carter
Participant

Please answer the questions that Mark and Mikel asked. If you don't answer those questions, there isn't much chance you will get an answer.

"Rewriting the query using joins instead of subqueries" may or MAY NOT help, since the query optimizer often does that automatically... that is like asking for painkillers without answering any medical questions.