How can I optimize this SQL query with ORDER BY RAND ()
source link: https://www.codesd.com/item/how-can-i-optimize-this-sql-query-with-order-by-rand.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How can I optimize this SQL query with ORDER BY RAND ()
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.
Related Articles
How can I optimize this SQL query with nested SELECT?
How can I optimize this SQL query with a large IN clause?
How can I optimize this SQL query?
How can I make this sql query with jpa?
How can I optimize this mysql query with nested joins?
How can I optimize this SQL outer join query?
How can I optimize the MySQL query with multiple joins?
How can I optimize the SQL query?
How can I accelerate this SQL query on MySQL 4.1?
How can I rewrite this LINQ query with reflection
How can we improve this SQL query?
How can I optimize this MySQL query?
How can I make this SQL query faster?
How can I optimize this Linq query to find blog articles with most views in the last 24 hours
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK