Solved

Recordset ORDER BY using specific Index

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now