Re: SubQuerying Vs Joining

From: Andrew J. Kelly (sqlmvpnooospam_at_shadhawk.com)
Date: 10/30/03


Date: Wed, 29 Oct 2003 19:01:48 -0500

Awah,

First off the one that is most likely to be fastest is not shown. This
would be an EXISTS statement.

     Select * from users AS b where EXISTS (select * from
 users_site_passes AS a where a.User_id = b.User_ID AND a.date_pass_issued
>= 'January 1, 2003')

Exists will stop looking after the first match is found where as the other
two might do more work. To answer your original question though they may in
fact be the same. Sometimes the optimizer will create similar plans on
queries such as these. Here are some general comments though:

Get in the habit of using the ANSI JOIN syntax like this:

  Select users.* from users INNER JOIN users_site_passes
ON users.user_id = users_site_passes.user_id and
users_site_passes.date_pass_issued

When dealing with DATE strings you should use the ANSI syntax as well to
avoid issues where sql server can misinterpret the date. It goes like
this: 'yyyymmdd' Always has 8 chars and no dashes, slashes etc.

-- 
Andrew J. Kelly
SQL Server MVP
"Awah Teh" <awaht@digicentriq.com> wrote in message
news:%238AEccnnDHA.644@TK2MSFTNGP11.phx.gbl...
> Which is most effective (consider the query below for an example)?
> Please give me some explanation (FMI:) as to why one is faster over the
> other.
>
>
> --Both Queries are designed to get the users information of users that
> received passes in the Year of 2003
>
>     --** ***************
>     --** QUERY 1
>     --** ***************
>     Select * from users where user_id in (select user_id from
> users_site_passes where date_pass_issued >= 'January 1, 2003')
>
>
>     --** ***************
>     --** QUERY 2
>     --** ***************
>     Select users.* from users, users_site_passes where users.user_id =
> users_site_passes.user_id and users_site_passes.date_pass_issued >=
'January
> 1, 2003'
>
> Thanks In Advance
> A-
>
> -- 
> Awah Teh
> Chief Executive Officer
> DigicentriQ Technologies, LLC
> awaht@digicentriq.com
> www.digicentriq.com
> 877 675 4742
> 805 732 9421
>
>


Relevant Pages

  • Re: CONTAINS performance
    ... That said, and with the query plan, I can start to give you more ... relational join in the context of the free-text optimization, ... SQL Server tables. ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Indexing Service, Openquery and sp_executesql
    ... SQL Server version and sp are you running? ... > data from the indexing service catalog when pasted into ... > query analyzer, but failed when put against sp_executesql ... I would choose Microsoft Indexing ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Problem using Access or Query Designer to run queries in SQL Serve
    ... >or Query Designer within Enterprise Manager, it works and I get data back. ... >ODBC Call Failed [ODBC SQL Server Driver] Timeout Expirederror in Access ... >[ODBC SQL Server Driver] Timeout Expired ...
    (microsoft.public.sqlserver.odbc)
  • Re: Transaction Isolation Level
    ... This means that while I initiate a transaction selecting all new orders it ... My query is executed on a SQL server 2005. ... the isolation level you should use is snapshot isolation. ...
    (comp.databases.ms-sqlserver)
  • Extreme performance issues (SQL Server 2000/ADO.NET/C#)
    ... This process runs very quickly if run through Query ... same exact stored procedures and views, run in the same exact order, through ... system that runs SQL Server (a 4-cpu Xeons system with 2gigs of physical ... When I execute these steps manually through query analyser,, ...
    (microsoft.public.dotnet.framework.adonet)