I have a table in Access with 1 field called HostName, it is a text field, with 100 char max. I use it to store DNS host names. The field is setup as the primary key. If I do the following query it returns the expected results, but takes about 8 seconds to complete on a table with 1 million records:
SELECT TOP 1 HostsRev.HostName
FROM HostsRev
WHERE (((HostsRev.HostName)>=”test”))
ORDER BY HostsRev.HostName;
If I remove the “ORDER BY” part, it returns in less than 1 second, but doesn’t always return what I would expect — not the first record that is >= to “test”.
I am doing the query via ADO from a C++ app, but I’ve tested in Access also, by creating a query, and get the same results.
What I need is to quickly find the first record, if any, that starts with a given string. I also tried using LIKE query but that had the same results. I need to do this because if I search on images.google.com, I need to know if the list contains google.com but not images.google.com (I actually store the host names in reverse string order to make this work correctly, and reverse the strings before doing the lookup).
The issue is that the TOP command on it’s own does not apply sorting to the data, so without the ORDER BY it will return in a different order and thus give different results, you could try the following instead:
Not sure if this will give any better performance though but worth a go : )