Solved

Recordset ORDER BY using specific Index

Posted on 2008-10-17
5
1,043 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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