darksystem — 2010-03-15T13:07:21-04:00 — #1
Can anyone tell me if this is a good usage of "LIKE" query
select p.productid, p.prodname, p.prodcode, c.catname from products p, categories c
where p.productid like "%acne%" or p.prodname like "%acne%" or p.prodcode like "%acne%" or c.catname like "%acne%" ORDER BY RAND();
using "LIKE" condition in multiple fields to search on something is kinda
messy to me. Are there any other better idea on how to optimize that query?
Any help is highly appreciated.
r937 — 2010-03-15T13:20:29-04:00 — #2
the messiness can be mitigated, but only somewhat, like this...
WHERE CONCAT(p.prodname,p.prodcode,c.catname) LIKE like '%acne%'
but as for optimization, no, using LIKE with a leading wildcard is going to remain slow
darksystem — 2010-03-15T13:27:19-04:00 — #3
I see. May I know what will be the best recommended query for the sql code that I provided above? The idea is to search(a keyword(s)) on products(product_id, product_name, product_code) and categories(categories_name) table
Btw, I really appreciate your help.
r937 — 2010-03-15T13:36:12-04:00 — #4
what you posted is about as good as it gets
darksystem — 2010-03-15T13:45:46-04:00 — #5
i was wondering with your comment on this one
"leading wildcard is going to remain slow"
is there anything i could to make it faster?
risoknop — 2010-03-15T14:26:22-04:00 — #6
I'm not expert but maybe something like Lucene or some other search engine would be faster than MySQL in this case?
That would require some benchmarks.
r937 — 2010-03-15T14:40:11-04:00 — #7
not without removing the leading wildcard, no
darksystem — 2010-03-15T14:50:41-04:00 — #8
so it should be like something like this(below) to make it faster
the leading wildcard is now removed.
will that make a great difference in terms of performance/optimization?
vali — 2010-03-15T14:57:51-04:00 — #9
LIKE "%foo%" will always be slow in MySQL
LIKE "foo%" is faster, but your not getting all the results you need...
If you really need the "%foo%" foo functionality, you need a good full text search engine.
Try SOLR for that, it's free and works well with millions of records, on cheap web servers.
r937 — 2010-03-15T14:59:19-04:00 — #10
but it will also make a huge difference in the results that you get