1

I'm trying to build a query for search page this my first query, and this is very fast the results load within Milli seconds

$query is php variable which is search text

$sql="SELECT * FROM 
tbl_product 
WHERE 
((tbl_product.pd_name LIKE '%$query%') 
OR (tbl_product.pd_art LIKE '%$query%') 
OR (tbl_product.pd_srkw LIKE '%$query%')) 
AND pd_bestsell <> 1  
ORDER BY RAND()";

But when i build a query with search suggestions table it's very slow and take 2-4 seconds for load. our server is very powerful dedicated one so it cannot be a issue with server.

sgtexts= keep suggestion texts

sgproduct =keep products ids on suggestion table

$sql="SELECT * FROM 
tbl_product,tbl_suggetions 
WHERE 
((tbl_product.pd_name LIKE '%$query%') 
OR (tbl_product.pd_art LIKE '%$query%') 
OR (tbl_product.pd_srkw LIKE '%$query%') 
OR (tbl_suggetions.sgtexts LIKE '%$query%' 
AND tbl_product.pd_id=tbl_suggetions.sgproduct))  
AND pd_bestsell <> 1 
GROUP BY pd_id 
ORDER BY RAND()";

can anyone help me for optimize second query please

7
  • 1
    EXPLAIN should give you the answer. dev.mysql.com/doc/refman/5.7/en/explain.html Commented Aug 30, 2016 at 8:38
  • 1
    Is tbl_suggetions.sgtexts indexed? And isn't suggetions a typo? Commented Aug 30, 2016 at 8:38
  • 1
    Add index on your table Commented Aug 30, 2016 at 8:39
  • Thanks mate i index table Commented Aug 30, 2016 at 8:56
  • How big is the table? Commented Aug 30, 2016 at 16:28

1 Answer 1

3

Is there any reason why you order by random? Removing this will make it faster.

Since you are using LIKE operator with %%, this will perform a full table scan and is by nature slower than using LIKE 'example%'.

Also, use a join on your table. Try this

SELECT 
        * 
FROM    tbl_product         tbl_product
JOIN    tbl_suggetions      tbl_suggetions ON tbl_suggetions.sgproduct=tbl_product.pd_id
WHERE 
(
(tbl_product.pd_name    LIKE '%$query%')        OR 
(tbl_product.pd_art     LIKE '%$query%')        OR 
(tbl_product.pd_srkw    LIKE '%$query%')        OR 
(tbl_suggetions.sgtexts LIKE '%$query%')
)  
    AND pd_bestsell <> 1 
    GROUP BY pd_id 
    ORDER BY RAND();

If the data doesnt need to be randomly ordered, remove the ORDER BY RAND()

Sign up to request clarification or add additional context in comments.

11 Comments

You're query has exactly the same efficiency, just has different syntax.
@SunethKalhara , also make sure you only in the SELECT return the columns that you actually need. Now it will return all columns from both tables.
@KayNelson the query is very fast my query took 15.3766 sec but this took 0.0728 sec, but i got a problem my query return 71 results but new one only return 47 results, any idea why is that, Thanks
It most likley has to do with using OR & AND in the WHERE query.
@SunethKalhara I think you can also speed iup your query by using pd_bestsell != 1, this will get all pd_bestsell where its not 1. what your are using now could give back where pd_bestsell=0 for instance, is that a possibility?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.