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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query performance

Author  Topic 

R44
Starting Member

8 Posts

Posted - 2011-08-26 : 07:33:00
I am working DB performance. which qurey will run fast

select EID, empname,DATE from emp inner join EmpDetails on emp.eid=EmpDetails.eid
where DATE between '2008-01-04'and '2010-01-04'

select EID, empname,DATE from emp inner join EmpDetails on emp.eid=EmpDetails.eid
and DATE between '2008-01-04'and '2010-01-04'

Kristen
Test

22859 Posts

Posted - 2011-08-26 : 07:41:15
same.

If [DATE} is a column in EmpDetails then I usually put the criteria test in the JOIN clause (which becomes more important when it is an OUTER join, but I do it on INNER joins too for consistency)

Your string-dates should be '20100104' (not '2010-01-04') - i.e. leave the hyphens out, otherwise your date will be considered ambiguous (4th January or 1st April) by SQL Server, and you may not get the one you want (e.g. if server configuration changes, or the Client uses a SET LANGUAGE command, or similar).

Don't use "DATE" as a column name, its a reserved word. Or if you cannot uise a different column name put it in [DATE]
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-08-26 : 09:45:35
also use aliases. So your select statement would be more understandable to someone else as:

SELECT
e.[EID] AS [EmployeeID]
, ed.[DATE] AS [EmployeeDetailsDate]
FROM
emp AS e
JOIN EmpDetails AS ed ON ed.[EID] = e.[EID]
WHERE
ed.[DATE] BETWEEN '20080104', '20100104'

Also -- as this is 2005 I suspect that your [DATE] column is really a DATETIME?

If that's the case then the BETWEEN query will only pick up dates between 20080104 and MIDNIGHT 20100104 (missing out all of the 4th of Jan 2010)

I'd be tempted to rewrite it as:

WHERE
ed.[DATE] >= '20080104'
AND ed.[Date] < '20100105'


Also -- In counterpoint to Kristen I prefer to describe INNER JOINS only in terms of their implicit table relationships (Foreign key to primary key) and leave filters in the WHERE clause (unless that results in poor readability or the query is really complicated). This is a completely personal thing though. The engine will generate exactly the same plan in this case.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -