Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to select records by what they begin with? 2

Status
Not open for further replies.
Dec 8, 2003
17,047
GB
Hi,

I want to select some records based upon the first 3 characters of one of their fields.

All of the following commands work perfectly for me, and on my small local dataset (10 or so records out of 40), there's no noticeable difference in speed:

Code:
SELECT * FROM Page WHERE name LIKE 'bus%';

SELECT * FROM Page WHERE name REGEXP '^bus';

SELECT * FROM Page WHERE LEFT(name, 3)='bus';

SELECT * FROM Page WHERE INSTR(name, 'bus')=1;

SELECT * FROM Page WHERE LOCATE('bus', name)=1;

SELECT * FROM Page WHERE SUBSTRING(name, 1, 3)='bus';

What I want to know is, which method would be better when scaled up to a larger number of records (perhaps 1000)?

My hunch is that INSTR would be faster than LIKE or REGEXP, but not sure about the other string ones.

Any advice?

Thanks!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
I just tried each of these against a MySQL 4.1.20 database on a table with 32k records. For me, LIKE was the fastest at .033 seconds, followed by LEFT, then SUBSTRING. The slowest, REGEXP, took .067 seconds.

-----------------------------------------
I cannot be bought. Find leasing information at
 
A table with 1.77 million records returns all results in a maximum of 0.02 seconds, the field is non indexed.

The server is quad core xeon with 8gb of ram all laid over to mysql.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
scaled up, INSTR and LOCATE will be the slowest

i believe LEFT will utilize an index (and thus be as fast as LIKE without the leading wildcard), and SUBSTRING starting at position 1 might also

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top