In terms of performance, the best approach is:
- select a result set based on your template;
- limit the size of the result set to 1.
- Check if the result was returned.
Doing this does not allow the database engine to perform a full table scan, and the query will return as soon as the first matching record is encountered.
The actual query depends on the database you are using. In MySQL, it looks something like this:
SELECT id FROM database.table WHERE column LIKE '%some pattern%' LIMIT 1;
In Oracle, it will look like this:
SELECT id FROM database.table WHERE column LIKE '%some pattern%' AND ROWNUM = 1;
source share