Solved

Recordset ORDER BY using specific Index

Posted on 2008-10-17
5
1,044 Views
Last Modified: 2013-11-29
I have an Access mdb with a table called CITY.
The PrimaryKey is a CityID, but there is also a field CITY that
is text and is Indexed with an Index called CITY.

When opening a recordset with a query like this:

    sql$="Select top 50 * from CITY WHERE (Foreign=0) ORDER BY CITY"
    Set rs = dat.OpenRecordset( sql$, dbOpenSnapshot, dbReadOnly)

It takes a long time (3-4 seconds) to return. If the ORDER BY
is left out the recordset opens immediately, but of course is
not ordered alphabetically by City name. Table is really big
(800K records).

I would have thought that the ORDER BY CITY would make the
JET Engine realize that it already has an Index CITY that could
be used to walk through these records in this order, but the
only explanation I have for the time delay is that it must be
sorting this table instead of using the built-in index.

Is there a way to force the query to use the Index we already
have so that it is much faster?
0
Comment
Question by:timfisher
[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
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 22741015
It's not so much the Order By or the Index as it is the SELECT * statement. Assuming you're using the Jet database engine (the default engine for Access), you're using a file-server type database. In order to parse your query the workstation must pull the entire CITY table across the wire, order it, then strip off the top 50 records.

If ALL of your columns are indexed ... well then you still basically pull the entire table across the wire for the workstation to parse.

If you have multiple columns in that table, and you don't need those columns, then just request the needed ones and see if things improve.

You might also try different indexes on different columns, if that's appropriate for your table schema.
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22741574
Is there no Index on Foreign?
0
 
LVL 18

Assisted Solution

by:jmoss111
jmoss111 earned 250 total points
ID: 22742314
A covering Index of City and Foreign might prevent a table scan and if the index was order by City then you should be able to lose the order by in the query
0
 
LVL 84
ID: 22743878
I missed the Foreign field ...
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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

733 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