views:

51

answers:

1

Hi

I have this kind of request :

SELECT myTable.ID,
myTable.Adress,
-- 20 more columns of all kind of type
FROM myTable
WHERE EXISTS(SELECT * FROM myLink 
    WHERE myLink.FID = myTable.ID 
    and myLink.FID2 = 666)

myLink has a lot of rows.

Do you think it's faster to do like this :

INSERT INTO @result(ID) SELECT myLink.FID 
FROM myLink 
WHERE myLink.FID2 = 666

UPDATE @result SET Adress = myTable.Adress,
    -- 20 more columns of all kind of type 
    FROM myTable 
    WHERE myTable.ID = @result.ID
+2  A: 

You will want to select as few rows as possible. Using a where clause to filter data is a good idea, because then the server has to return less data. In the second version, you're selecting only one item of myLink, and then working on it, whereas in the first example youve got a select *, which is usually a bad idea.

Is there anything wrong with:

SELECT myTable.etc from myTable,myLink where myLink.FID2= 666 AND myLink.ID=myTable.ID 

This constructs a join and filters in one step. Perhaps a different kind of join would work more efficiently, but the code sure looks shorter.

Karl
When you use exists you can use "select*" there is no difference http://blog.sqlauthority.com/2008/02/26/sql-server-select-1-vs-select-an-interesting-observation/And I don't like to do a join only to filter data cause you can get duplicate rows.
remi bourgarel