Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
regata
Starting Member
4 Posts |
Posted - 2012-02-08 : 21:53:06
|
Hii need adviseI have 3 tables lets say1: ITEMS [id,name]2: CUSTOMERS [id,name]3: SALE [date,customername,itmid,quantity,discount ... ]The question which one is better in perfermance1st one================================================SELECT S.Date, (select i.name from items i where i.id = S.Itmid) as ITEM, (select c.name from customers c where c.id = S.customername) as CUSTOMER, S.QUANTITY, S.DISCOUNT, . . .FROM STOCK AS S 2nd one================================================SELECT S.Date, i.name as ITEM, c.name as CUSTOMER, S.QUANTITY, S.DISCOUNT, . . .FROM STOCK AS Sjoin items i on i.id = S.Itmidjoin customers c on c.id = S.customername3rd one================================================SELECT S.Date, i.name as ITEM, c.name as CUSTOMER, S.QUANTITY, S.DISCOUNT, . . .FROM STOCK S,items i, customers cwhere i.id = S.Itmid and c.id = S.customernamei have few records in database so far and i don't see any difference but what if record count grows lets say over 100000... and sotnxNurlan |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-08 : 22:02:08
|
i think 2nd and 3 rd options will be better (they both are equivalent only difference being only written in old join syntax and other in ANSI join syntax. I prefer 2 as its ANSI join syntax and adds more clarity to the query.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-02-09 : 03:58:46
|
The first one will throw errors if the subqueries ever return more than one row. The 2nd is the recommended, the 3rd is the really old way of writing joins.--Gail ShawSQL Server MVP |
 |
|
|
|
|
|
|