Home > Uncategorized > SQL performance: Nested select statements.

SQL performance: Nested select statements.

This is a tip for restructuring nested select statements to make them run faster in SQL server. I can’t vouch for this in all situations, but It shortened a long SQL query for me from 1 hour to 7 minutes.
 
Where you have
Select * from xyz where id in
 (
   Select * from abc where id in
   (
    select id from def
   }
 )
 
I recommend re-writing as
 
   Select * into #abcdef from abc where id in
   (
    select id from def
   }
 
Select * from xyz where id in
 (
   select id from #abcdef
 )
 
 
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: