Solved

Speed up Select Top n... Query

Posted on 2017-05-17
9
48 Views
Last Modified: 2017-05-17
I have table called Jobs. It has approx 200,000 records. The Primary Key field is JobNo. I have ascending and descending indexes on this field. For any given JobNo I need to be able to quickly find the next or previous JobNo's. So to find the next JobNo i use the following query:

SELECT TOP 1 JobNo  From Jobs WHERE JobNo > 193574 ORDER BY JobNo

This executes instantly and gives the correct result. To find the previous record, I am using this:

SELECT TOP 1 JobNo  From Jobs WHERE ((JobNo<193574)) ORDER BY JobNo Desc

This takes 10 seconds to open even though the JobNo field has a descending Index on it. I am thinking that Access is finding the PrimaryKey ascending index and doesn't look any further. So it doesn't find the descending Index. Can anyone suggest a way I can speed this up at all?

Ian
0
Comment
Question by:Merlin-Eng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 42138720
You did already what is necessary to optimize your query. So maybe a compact and repair helps..

Post the execution plan..

JetShowPlan, use the correct registry branch:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Debug]
 "JETSHOWPLAN"="ON"
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 42138736
test this queries

SELECT Min(JobNo)  From Jobs WHERE JobNo > 193574


SELECT Max(JobNo)  From Jobs WHERE JobNo  < 193574
0
 

Author Comment

by:Merlin-Eng
ID: 42138788
That registry setting applies to 32bit installations. I found the correct setting for my 64bit system. I believe I got set, but how do I actually view the execution plan?
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 42138791
did you try the queries I posted?
0
 

Author Comment

by:Merlin-Eng
ID: 42138802
@Rey Obrero:

Instantly:  SELECT Min(JobNo)  From Jobs WHERE JobNo > 193574

10 Secs: SELECT Max(JobNo)  From Jobs WHERE JobNo  < 193574

So it's the same behaviour.
0
 
LVL 34

Expert Comment

by:ste5an
ID: 42138838
It writes a file SHOWPLAN.OUT either in the current (database file) folder or documents folder.
0
 

Author Comment

by:Merlin-Eng
ID: 42138849
Ok. I found it now thanks. Here's what it gave me:


--- Query1 ---

- Inputs to Query -
Table 'JOBS'
    Database '\\avatar\jobscopy\cauldron.accdb'
- End inputs to Query -

01) Restrict rows of table JOBS
      using rushmore
      for expression "Jobs.JobNo<193574"
02) Sort result of '01)'
03) Compute Top of result of '02)'
0
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 42138863
The "using rushmore" indicates that it already uses it most effective algorithm to collect the data. So there is nothing you can do about this query.

Just check your indices again and run a compact and repair. And post a concise and complete sample file.
0
 

Author Closing Comment

by:Merlin-Eng
ID: 42138998
I compacted and repaired the front and back end databases and I decided to re-create the linked tablef object for the jobs table..... It fixed the problem.  So thank you for your help.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

729 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