To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

HOME News MS SQL Oracle DB2 Access MySQL PHP Scripts Books Links DBA Talk


Go Back   Database Journal Forums > Miscellaneous > General Database Discussions

General Database Discussions Discuss any database topic not covered in any other forum on this site

Reply Post New Thread
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 04-04-2005, 03:21 AM
jpeg jpeg is offline
Registered User
 
Join Date: Jun 2003
Posts: 5
SQL Server : update command with subqueries

Hello everybody.

I wrote an SQL update query for SQL Server 2000 but found it not fast enough. The query is :

UPDATE mesures SET valeur=valeur*0.12516 WHERE champ_id in (select C.id from graphique G LEFT join champs C ON C.graphique_id=G.id WHERE G.extracteur='R30') AND instant_id in (select id from instant_mesure where jour>=20030301 and jour<20040509)

The 2 SELECT sub-queries are very fast but the UPDATE 'total' query is too slow and I sometimes have a timeout while executing with ODBC. Note that 'mesures.champ_id' and 'mesures.instant_id' are the primary key of the 'mesures' table.

Do you think it can be wroten using another faster syntax ?

Thanks for your help.
Reply With Quote
  #2  
Old 04-04-2005, 03:46 AM
Rawhide's Avatar
Rawhide Rawhide is offline
Moderator
 
Join Date: Feb 2003
Posts: 1,048
Here are some optimizer tips:

1. You should never use "In" with subqueries. It's very bad for performance. Use "Exists" clauses instead when you can.

2. When doing an Exists clause, use "Select 1" instead of "Select fieldname".

3. For select queries (as in your subqueries) use the "with(nolock)" optimizer hint. This will prevent it from waiting for a table or row to be unlocked before reading.

Code:
UPDATE m
SET valeur=valeur*0.12516
From mesures As m
WHERE Exists (Select 1
    From graphique As G with(nolock)
    Left Join champs As C with(nolock) On C.graphique_id = G.id
    WHERE G.extracteur = 'R30'
    And C.id = m.champ_id)
And Exists (Select 1
    From instant_mesure with(nolock)
    Where jour >= 20030301
    And jour < 20040509
    And id = m.instant_id)
The subqueries may be very fast when running them by themselves, but when they are used in an "In" clause, they run once for every record in mesures. This is when you can really see the performance difference.

If you are still seeing speed issues, view the Estimated Execution Plan in Query Analyzer.
__________________
My SQL Scripts:
Dynamic defrag of all indexes for a given table (version 2)
Calculate Easter for Any Year
Function to strip HTML/ASP/PHP tags from text

Select * From Users Where Clue > 0
-------------------------------
(0 row(s) affected)
Reply With Quote
Reply Post New Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 10:01 PM.


DatabaseJournal Recent Articles


 » Oracle Flashback Version Query—Trick ...

 » Migrating Web Applications for use with SQ...

 » Oracle's pipeline delivers many new products

 » PostgreSQL 8.4 Revs Up Database Admin, Sec...

 » Universal SQL Editor 1.1.7 Released

Search Database Journal:
 








Acceptable Use Policy

WebMediaBrands

internet.comMediabistrojusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Shopping | E-mail Offers

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.