Solved

Recordset ORDER BY using specific Index

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

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 85
ID: 22743878
I missed the Foreign field ...
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

726 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