?
Solved

Speed up Select Top n... Query

Posted on 2017-05-17
9
Medium Priority
?
76 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 35

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 35

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 35

Accepted Solution

by:
ste5an earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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.
Suggested Courses

764 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