Solved

Slow SQL using >date as Criteria

Posted on 2014-01-06
6
680 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 35

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Display label on subreport when NO DATA on subreport 4 18
Create macro from runcode 30 22
backup programme - VBA 3 22
Connection to multiple databases 13 16
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 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

8 Experts available now in Live!

Get 1:1 Help Now