Search code examples
sqlquery-optimization

How can I speed up this SQL query that is timing out?


Is there a better way to rewrite the code? The query takes forever to execute. Thanks

 SELECT * from w..ts
    where not si in
    (
    select si from g..sa
    )
    and ci in
    (
    Select ClientID from w..tcwhere ds= 'AZ'
    )
    and s> 2
    and sd between '1/1/2010' and GETDATE()
    order by si

Solution

  • Consider joins or applies instead.

    SELECT * from webservice..tService
    left join table t1 on t1.col1 = tService.col2
    inner join table t2 on t2.col1 = tService.col2
    where t1.ServiceID is null
    and ServiceStep > 2
    and ServiceDate between '1/1/2010' and GETDATE()
    order by ServiceID