Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

slow subquery

Posted on 2002-07-20
4
636 Views
Last Modified: 2008-03-06
I am no expert so this is probably embarrassingly easy for you experts out there but why is this simple 1st statement so much quicker than the simple 2nd (with subquery)?

1st:

DECLARE @tmpDueDate datetime
SET @tmpDueDate = 7/18/2002
DECLARE @PolicyID int
SET @PolicyID = 20
DECLARE @AccountID int

SELECT @AccountID = (SELECT  top 1 AccountID FROM tblAccounts
          WHERE DueDate > @tmpDueDate AND PolicyID = @PolicyID
          AND (TransactionTypeID = 1 or transactiontypeid = 19)  
          AND TransactionStatusID <> 2 and TransactionstatusID <> 4
          AND Contra = 0 And paymentmethodid = 1 ORDER BY DueDate)

          UPDATE tblAccounts SET TransactionStatusID = 1
          WHERE AccountID = @AccountID
2nd:

DECLARE @tmpDueDate datetime
SET @tmpDueDate = 7/18/2002
DECLARE @PolicyID int
SET @PolicyID = 20
DECLARE @AccountID int

UPDATE tblAccounts SET TransactionStatusID = 1
          WHERE AccountID = (SELECT top 1 AccountID FROM tblAccounts
          WHERE DueDate > @tmpDueDate AND PolicyID = @PolicyID
          AND (TransactionTypeID = 1 or transactiontypeid = 19)  
          AND TransactionStatusID <> 2 and TransactionstatusID <> 4
          AND Contra = 0 And paymentmethodid = 1 ORDER BY DueDate)


tblAccounts is indexed on PolicyID, AccountID AND DueDate and contains c. 3 million rows. 1st takes 1 second, 2nd takes 150 seconds, both to do 1 update! For 2nd, estimated execution plan shows 34% of query taken up with Hash Match/Inner Join (whatever they are! - all rows read) and 25% with a sort.
0
Comment
Question by:dlisk
  • 3
4 Comments
 
LVL 1

Accepted Solution

by:
tnewc59 earned 100 total points
ID: 7167351
The second example will require a full table scan.

This is because it will look at the tblAccounts table, one record at a time to see if the row matches on AccountID.

The first method is executing the update for only a single accountid.  So the tblAccounts does not need to be scanned for each match.
0
 
LVL 1

Expert Comment

by:tnewc59
ID: 7167354
The following select operation will experience the same problem:

SELECT myTable.Column1, myTable.Column2
FROM myTable
WHERE myTable.Column1 IN (
      SELECT mySecondTable.Column1
      FROM mySecondTable
      WHERE mySecondTable.Column2 < 1000)

This same query could be re-written more efficiently as:
SELECT myTable.Column1, myTable.Column2
FROM myTable INNER JOIN
      (
      SELECT mySecondTable.Column1
      FROM mySecondTable
      WHERE mySecondTable.Column2 < 1000
      ) as mySubQueryTable on myTable.Column1 = mySubQueryTable.Column1


The second is more efficient as the sub query will be executed and assembled only once, but the first example will require the query to be executed 'x' times.  Where 'x' is equal to the number of rows in myTable.

This is the same concept that is slowing down your second update.
0
 
LVL 1

Expert Comment

by:tnewc59
ID: 7167356
The sort time is the time it takes to execute the 'order by' portion of the query.

The problem with using a 'top' with an order by is that your query will not return until the full result set is ordered on the 'order by'.  

When possible, I try to write my queries that utilize 'top' without an 'order by' clause.
0
 

Author Comment

by:dlisk
ID: 7167566
Thanx for your time tnewc59.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question