MySQL Forums
Forum List  »  General

Fastest way to find if any rows match
Posted by: Jan Hansen
Date: May 18, 2006 07:10AM

I'm interested in knowing what the quickest way might be to find out if any rows match a given select query. Note that I'm _not_ interested in seeing the rows themselves, nor even getting a count. All I need to know is whether there is 1 or more matching row. I have a MyISAM table with 11 million rows, containing a BIGINT id column (primary key) and 3 VARCHAR(255) columns, two of them having indexes of the first 31 characters. I'm running queries like "SELECT COUNT(id) FROM sentences WHERE current LIKE 'whatever%'", and when there are lots of matches (in the thousands), the query is taking an age to return (often in minutes as opposed to the usual milliseconds), even though EXPLAIN SELECT shows that it is in fact using the index.

SELECT COUNT() would normally be the way to determine this, I imagine, but it does more than I need - as I say, once I know there are ANY matches, my question is answered, I don't need to know how many matches there are. And it seems that getting a COUNT where there are lots of matches is very slow and prone to severe holdups.

Is there any MySQL shortcut which effectively aborts as soon as it has found that there is a match in the index, without going any further? I wondered at first whether simply using LIMIT might be the answer as below

SELECT(id) FROM sentences WHERE current LIKE 'whatever%' LIMIT 1;

but no, it still takes an age.

Options: ReplyQuote


Subject
Written By
Posted
Fastest way to find if any rows match
May 18, 2006 07:10AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.