Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

More efficient than LEFT JOIN

I have a table with data that is refreshed regularly but I still need to
store the old data.  I have created a seperate table with a foreign key
to the table and the date on which it was replaced.  I'm looking for an
efficient way to select only the active data.

Currently I use:

SELECT ...
FROM DataTable AS D
LEFT OUTER JOIN InactiveTable AS I ON I.Key = D.Key
WHERE D.Key IS NULL

However I am not convinced that this is the most efficient, or the most
intuitive method of acheiving this.

Can anyone suggest a more efficient way of getting this information
please.

Many thanks.


*** Sent via Developersdex http://www.droptable.com ***

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Wotherspoon
02-15-06 12:24 PM


Re: More efficient than LEFT JOIN
Hi, Brian

I think that you wanted to write "WHERE I.Key IS NULL" (instead of
"WHERE D.Key IS NULL"). In this case, you can use something like this:

SELECT ...
FROM DataTable WHERE Key NOT IN (
SELECT Key FROM InactiveTable
)

Razvan

PS. I assume that the "Key" column does not allow NULL-s.


Report this thread to moderator Post Follow-up to this message
Old Post
Razvan Socol
02-15-06 02:24 PM


Re: More efficient than LEFT JOIN
On Wed, 15 Feb 2006 12:41:09 GMT, Brian Wotherspoon wrote:

>
>
>I have a table with data that is refreshed regularly but I still need to
>store the old data.  I have created a seperate table with a foreign key
>to the table and the date on which it was replaced.  I'm looking for an
>efficient way to select only the active data.
>
>Currently I use:
>
>SELECT ...
>FROM DataTable AS D
>LEFT OUTER JOIN InactiveTable AS I ON I.Key = D.Key
>WHERE D.Key IS NULL
>
>However I am not convinced that this is the most efficient, or the most
>intuitive method of acheiving this.
>
>Can anyone suggest a more efficient way of getting this information
>please.
>
>Many thanks.

Hi Brian,

The most intuitive way, IMO, is

SELECT   ...
FROM     DataTable AS D
WHERE    NOT EXISTS
(SELECT *
FROM   InActiveTable AS I
WHERE  I.Key = D.Key)

The most efficient is either the above or your own LEFT OUTER JOIN query
(but do change D.Key to I.Key in the IS NOT NULL check!!) - but it'll be
only efficient if the I.Key column is indexed.

--
Hugo Kornelis, SQL Server MVP

Report this thread to moderator Post Follow-up to this message
Old Post
Hugo Kornelis
02-16-06 01:24 AM


Re: More efficient than LEFT JOIN
try to bring it up a level.

for instance, you are probably creating a temp table?  Perhaps create
the temp table with closer to the data you really need.
if you are looking for only one cusotmer, then only pull that one
customer. or,  for a specfiic time period, then only that time period.

also, make sure you have an index on inactivetable.key.

if you knew that inactivetable started at some timeframe for all
records, then you could create a composite index on
inactivedata.timestamp plus key.

would it be worth putting an "inactive data datestamp" at the customer
level? perhaps if you have enough rows.


Report this thread to moderator Post Follow-up to this message
Old Post
Doug
03-01-06 01:29 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 05:37 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006