Solved

Slow SQL using >date as Criteria

Posted on 2014-01-06
6
672 Views
Last Modified: 2014-01-06
Hi Experts,

I'm linking to a SQL Server table, and importing data. Right now is there only 100.000 records. But in a few Years there will be Millions.

If i just extract all records, then it takes a few sec but if i then say "SELECT * FROM tbl WHERE REG_DATE>[some date]" it gets really slow.

Why is that and is there a way to make it faster?
0
Comment
Question by:DCRAPACCESS
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 39760331
I'd try adding an index to the date field if not there. This will make the filtering a lot quicker. Select all doesn't need to do anything but bring it all back. While it appears quick, what you see is the first screen - the rest of the data is still loading many seconds later. The moment you sort or filter - a whole lot of work has to happen before you see the first records which is what makes it appear slower.


Kelvin
0
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 39760336
Further to last post, I suspect you are using a normal Access select query. Try doing this as a "pass through" query - you need to write the SQL in SQL Server syntax. This then makes the SQL server do the filter rather than Access and is usually much faster.

Kelvin
0
 

Author Comment

by:DCRAPACCESS
ID: 39760393
Hi Kelvin,

Can you give a sample of a "pass through" query?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 500 total points
ID: 39760422
SELECT * FROM tbl WHERE REWG_DATE > '1-jan-2014'

Placed in the body of a pass through query will work.

Then open it as you would an Access query. If the date comes from a parameter or similar - use VBA to contract the SQL and set the pass through query SQL to be whatever you want then open it. They're not much different to Access queries (can only use then for select statements (and not crosstabs)), but must use SQL Server syntax - which is similar but not the same as Access - not that dates are encased in single quotes - as it text.


Kelvin
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39760462
To create a pass-through query, you need to convert your Access SQL string to T-SQL.  You then create a new query and paste in the SQL string.  The QBE cannot display pass-through queries because a pass-through query is ALWAYS coded in the syntax of the target server.  So, if the query is to run on an Oracle server, you use Oracle syntax.  DB2 server, DB2 syntax, SQL Server, T-SQL, etc.

This is not usually necessary though since Access makes every effort to pass through ALL queries (you can defeat this if you don't understand the process though).  Although there is certainly some overhead associated with the ODBC driver getting involved to convert the "Access" query before sending it off to the server, Access DOES send all queries off to the server for processing and the server returns only the records requested.  So, regardless of whether the query is a pass-through or Jet/ACE, the heavy lifting still gets done on the server.  The pass-through just saves some pre-processing.  The thing you lose with pass-through queries is that they are not updateable and in my mind that is a huge loss.

The only time I have ever actually had to use a pass through query is for bulk updates.  Access wraps each update in a transaction in order to allow you the ability to cancel.  That takes a huge amount of workspace and extra time.  With a pass-through query, you don't get the option to change your mind about applying the updates, once the query is sent, the action happens.

I have found that adding appropriate indexes and occasionally creating views that perform common joins provide sufficient responsiveness.
0
 

Author Closing Comment

by:DCRAPACCESS
ID: 39760467
Works :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now