I’m trying to optimize this query because the DB it runs against is huge and the host says this query is causing a serious load on the server. I’ve read some of the other answers about how to replace ORDER BY RAND() but I don’t know enough about SQL to adapt those answers to this particular query. Can anyone help? TIA
SELECT COUNT( p.prod_id ) AS no_prod, s.*
FROM product p, seller s
WHERE s.admin_status = '1'
AND s.pay_status = '1'
AND s.sub_type != ''
AND p.seller_id = s.seller_id
GROUP BY s.seller_id
HAVING COUNT( p.prod_id )>5
ORDER BY RAND()
LIMIT 0, 4
You may be better off adding a non-unique index which includes the fields seller.admin_status, seller.pay_status, seller.sub_type. You’ll get the biggest bang for your buck by indexing fields referenced in your WHERE clause.