Solved

Recordset ORDER BY using specific Index

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
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 …

895 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

17 Experts available now in Live!

Get 1:1 Help Now