dbTalk Databases Forums  

fast query to check whether records of a given date exists in a table

comp.databases.ms-access comp.databases.ms-access


Discuss fast query to check whether records of a given date exists in a table in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
colmkav
 
Posts: n/a

Default fast query to check whether records of a given date exists in a table - 07-12-2012 , 04:14 AM






I want to write a query that finds out whether records from a date exist in a table. At the moment I am using

SELECT [Processing Date] FROM [Data] WHERE [Processing Date] = @ContextDate

It works but I was hoping there was a quicker query that would stop as soon as the first ocurrence of Contextdate was found

Reply With Quote
  #2  
Old   
Bob Quintal
 
Posts: n/a

Default Re: fast query to check whether records of a given date exists in a table - 07-12-2012 , 05:08 AM






colmkav <colmjkav (AT) yahoo (DOT) co.uk> wrote in
news:3eab4f12-2cfc-4c2e-84b6-fceca77b818a (AT) googlegroups (DOT) com:

Quote:
I want to write a query that finds out whether records from a date
exist in a table. At the moment I am using

SELECT [Processing Date] FROM [Data] WHERE [Processing Date] =
@ContextDate

It works but I was hoping there was a quicker query that would
stop as soon as the first ocurrence of Contextdate was found

Make sure that the date column is indexed.That will improve processing
time more that any other technique.


--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #3  
Old   
Bob Barrows
 
Posts: n/a

Default Re: fast query to check whether records of a given date exists in a table - 07-12-2012 , 05:50 AM



colmkav wrote:
Quote:
I want to write a query that finds out whether records from a date
exist in a table. At the moment I am using

SELECT [Processing Date] FROM [Data] WHERE [Processing Date] =
@ContextDate
Is this a saved query? Or a passthrough? The "@ContextDate" makes it look
like a T-SQL statement rather than a JetSQL one. Hmm, can a VBA variable
name begin with @? I'll need to check that later.


Quote:
It works but I was hoping there was a quicker query that would stop
as soon as the first ocurrence of Contextdate was found
In addition to the indexing suggestion, using EXISTS will cause the query to
return a True result upon the first encounter with a record that meets the
requirements (an EXISTS subquery is one of the only times selstar (select *)
is acceptable in production code - since an EXISTS subquery returns a
true/false result instead of an actual record, the wildcard has no impact on
performance):

In VBA code:
if exists (select * from data WHERE [Processing Date] = @ContextDate) then
...

or, in a query:
select ...
where exists (select * from data WHERE [Processing Date] = @ContextDate)

Reply With Quote
  #4  
Old   
colmkav
 
Posts: n/a

Default Re: fast query to check whether records of a given date exists in a table - 07-12-2012 , 07:38 AM



On Thursday, July 12, 2012 11:14:51 AM UTC+2, colmkav wrote:
Quote:
I want to write a query that finds out whether records from a date exist in a table. At the moment I am using

SELECT [Processing Date] FROM [Data] WHERE [Processing Date] = @ContextDate

It works but I was hoping there was a quicker query that would stop as soon as the first ocurrence of Contextdate was found
Thanks for the replies. The making it an index certainly works much faster (1/8of the original time!). My only problem (short term anyway) is I might not be allowed to change the live table as its used by other users.

On the exists query what is the actual SQL that should be used? I tried

select [Processing Date] FROM data
where exists (select * from data WHERE [Processing Date] = @ContextDate) ;

But this is actually slower (and also other dates.

Reply With Quote
  #5  
Old   
Bob Barrows
 
Posts: n/a

Default Re: fast query to check whether records of a given date exists in a table - 07-12-2012 , 09:44 AM



colmkav wrote:
Quote:
On Thursday, July 12, 2012 11:14:51 AM UTC+2, colmkav wrote:
I want to write a query that finds out whether records from a date
exist in a table. At the moment I am using

SELECT [Processing Date] FROM [Data] WHERE [Processing Date] =
@ContextDate

It works but I was hoping there was a quicker query that would stop
as soon as the first ocurrence of Contextdate was found


On the exists query what is the actual SQL that should be used? I
tried

select [Processing Date] FROM data
where exists (select * from data WHERE [Processing Date] =
@ContextDate) ;

But this is actually slower (and also other dates.
Ummm, why would you use EXISTS when filtering data from the same table? Just
put the criterion in the WHERE clause.

select [Processing Date] FROM data
WHERE [Processing Date] = @ContextDate

Yes, we're back to where you started. In what context are you using this
query? VBA code? You say you want to find " ... out whether records from a
date exist in a table ." ... in order to do what? Decide whether to run some
code or not? Decide whether to show or hide a control in a form or report?
What's the context?

Anyways. use EXISTS when you wish to filter the results from a table based
on values in a different table without using a JOIN.

select [Processing Date] FROM data
where exists (select * from SomeOtherTable WHERE [SomeDate] =@ContextDate)

A join might not work well because the other table might have multiple rows
that meet the criteria and you only want the single row to be returned from
the first table.

Reply With Quote
  #6  
Old   
Bob Barrows
 
Posts: n/a

Default Re: fast query to check whether records of a given date exists in a table - 07-13-2012 , 08:45 AM



Bob Barrows wrote:
Quote:
Anyways. use EXISTS when you wish to filter the results from a table
based on values in a different table without using a JOIN.

That should say:

Anyways, use an EXISTS subquery in a query when you wish to filter the
results from a table based on values in a different table without using a
JOIN.

EXISTS can also be used in VBA code, as I described in my first post.

Reply With Quote
  #7  
Old   
hans.updyke@gmail.com
 
Posts: n/a

Default Re: fast query to check whether records of a given date exists in a table - 07-14-2012 , 05:05 PM



On Thursday, July 12, 2012 3:14:51 AM UTC-6, colmkav wrote:
Quote:
I want to write a query that finds out whether records from a date exist in a table. At the moment I am using

SELECT [Processing Date] FROM [Data] WHERE [Processing Date] = @ContextDate

It works but I was hoping there was a quicker query that would stop as soon as the first ocurrence of Contextdate was found
Have you considered querying just the count of rows from your chosen Contextdate ?

SELECT Count(*) AS num_rows FROM [Data] WHERE [Processing Date] = @ContextDate

With an index on [Processing Date], the db engine wouldn't even need to look in the table because the index contains all the information it needs. It should be fast.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.