I've altered the code to format it better and to just have one regexp comparison (using s?) rather than 2 separate matches. It will still run slow because of the other points I mentioned but ...
Code:
select A.*,
B.*,
C.GGCategoryID,C.CategoryName,C.CategorySlugName,
XX.EcoStatus
from tbl_products A,
tbl_members B,
tbl_product_categories C ,
tbl_eco_ratings XX
WHERE A.MerchantMemberID=B.MemberID
and B.MemberType='Merchant'
and A.GGCategoryID=C.GGCategoryID
and XX.EcoStatusValue=A.EcoStatusValue
and A.Status='Active'
and A.isDeleted = 'No'
and B.MerchantType = 'Online'
and C.Status='Active'
and C.isDeleted='No'
and ( A.ProductName REGEXP '[[:<:]]#SEARCHTERM#s?[[:>:]]'
OR A.ProductDescription REGEXP '[[:<:]]#SEARCHTERM#s?[[:>:]]'
OR B.MemberNickName REGEXP '[[:<:]]#SEARCHTERM#s?[[:>:]]'
OR B.ScreenName REGEXP '[[:<:]]#SEARCHTERM#s?[[:>:]]'
OR A.ProductType REGEXP '[[:<:]]#SEARCHTERM#s?[[:>:]]'
OR C.CategoryName REGEXP '[[:<:]]#SEARCHTERM#s?[[:>:]]' )
Group By A.`ProductName`
Order By ProductID DESC
LIMIT 0,10
As well as my original points I'd also point out:
- Using tbl_ to start each table name is a poor naming convention.
- If a search string matches a category name then the user will always get everything from that category returned even if his search matches a product exactly. This will confuse most users as they'd never understand why they were given these items.
- I can see what the code is doing now so please disregard the idea of full text searches I mentioned before.
- You may have problems with matching upper and lower case depending on how your server is set up (easy to test though).
- Also if the user searches for 2 words with a space in ie "Seagate drives" then it won't match against "Seagate hard drives".
I think you should split the searches into separate searches: 1st to match against product names, 2nd product desc etc. If at any stage you have 10 records returned then you can stop searching further and just return the 10 matches. This should make the results a better match to what the user wants and it should be a lot quicker.
You could also group the results showing the matches on product but then also saying that there are 2 matches on memberNickName and perhaps 1 on screenName and so giving the user the option of choosing these matches ie if the user searched for QNAP
Code:
Products:
QNAP TS-409
QNAP TS-209
QNAP TS-209 Pro
Manufactures:
QNAP Store