| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Member Join Date: Mar 2008
Posts: 79
![]() | MySQL: Could this be faster? (multiple LIKE)Code: // searching for: at least one of (key1, key2, key3) in
// at least one of (field1, field2, field3)
SELECT * FROM table_name WHERE (field1 LIKE '%key1%'
OR field1 LIKE '%key2%'
OR field1 LIKE '%key3%'
// other field
OR field2 LIKE '%key1%'
OR field2 LIKE '%key2%'
OR field2 LIKE '%key3%'
// other field
OR field3 LIKE '%key1%'
OR field3 LIKE '%key2%'
OR field3 LIKE '%key3%');
// searching for: everyone of (key1, key2, key3) in
// everyone of: (field1, field2, field3)
SELECT * FROM table_name WHERE (field1 LIKE '%key1%'
AND field1 LIKE '%key2%'
AND field1 LIKE '%key3%'
// other field
AND field2 LIKE '%key1%'
AND field2 LIKE '%key2%'
AND field2 LIKE '%key3%'
// other field
AND field3 LIKE '%key1%'
AND field3 LIKE '%key2%'
AND field3 LIKE '%key3%');
// searching for: none of (key1, key2, key3) in
// none of: (field1, field2, field3)
SELECT * FROM table_name WHERE (field1 NOT LIKE '%key1%'
AND field1 NOT LIKE '%key2%'
AND field1 NOT LIKE '%key3%'
// other field
AND field2 NOT LIKE '%key1%'
AND field2 NOT LIKE '%key2%'
AND field2 NOT LIKE '%key3%'
// other field
AND field3 NOT LIKE '%key1%'
AND field3 NOT LIKE '%key2%'
AND field3 NOT LIKE '%key3%'); |
| |
| | #2 (permalink) |
| Senior Member ![]() Join Date: Apr 2006
Posts: 1,478
![]() ![]() ![]() ![]() ![]() ![]() ![]() | I'm not sure if it could be faster, but you can 'EXPLAIN' the queries to see which of them are using indexes and which are not. http://dev.mysql.com/doc/refman/5.0/en/explain.html You might consider using REGEXP too. http://dev.mysql.com/doc/refman/5.0/en/regexp.html
__________________ Free Keyword Popularity, CPC, etc... Namejet, Snapnames, GoDaddy Lists Offers valid for 24 hours. Now Available: Rented.com |
| |
| | THREAD STARTER #3 (permalink) |
| NamePros Member Join Date: Mar 2008
Posts: 79
![]() | All fields are VARCHAR or TEXT and none of them uses indexes (FULLTEXT) because I want exactly and all the results from a database (that it could be -from time to time- not very-very big). Using FULLTEXT in a case like that, it could return no results although keys are in the fields. |
| |
| | #4 (permalink) |
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,963
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | IMO a regexp as suggested above could be what you are looking for. Indexing the fields would also improve performance.
__________________ NameNewsletter.com - free lists of available domain names ZoneFiles.net (beta) - ccTLD and gTLD droplists
Last edited by Kath; 03-29-2008 at 01:11 PM.
Reason: typo
|
| |
| | THREAD STARTER #5 (permalink) | ||||
| NamePros Member Join Date: Mar 2008
Posts: 79
![]() |
By this meaning indexes will not work at all if my database sometime stops to be huge. Am I wrong? I don't think so. | ||||
| |
| | #6 (permalink) |
| Senior Member ![]() Join Date: Apr 2006
Posts: 1,478
![]() ![]() ![]() ![]() ![]() ![]() ![]() | You can index the fields, without using FULLTEXT for your searches. If I recall, you can also use a program called Sphinx too that may be what you need. http://www.sphinxsearch.com/ Looking at the features, it may be exactly what you need for performance you desire. http://www.sphinxsearch.com/features.html
__________________ Free Keyword Popularity, CPC, etc... Namejet, Snapnames, GoDaddy Lists Offers valid for 24 hours. Now Available: Rented.com |
| |
| | THREAD STARTER #7 (permalink) | ||||
| NamePros Member Join Date: Mar 2008
Posts: 79
![]() |
Last edited by evdoxos; 03-29-2008 at 04:34 PM.
| ||||
| |