1
As I could improve this query, it returns me 30 records from a table that contains 43thousand records.
NOTE: I use index in the core column (much improved).
((SELECT id,situation,info FROM request WHERE core=1 ORDER BY RAND() LIMIT 9)
UNION ALL
(SELECT id,situation,info FROM request WHERE core=3 ORDER BY RAND() LIMIT 7)
UNION ALL
(SELECT id,situation,info FROM request WHERE core=5 ORDER BY RAND() LIMIT 5)
UNION ALL
(SELECT id,situation,info FROM request WHERE core=6 ORDER BY RAND() LIMIT 2)
UNION ALL
(SELECT id,situation,info FROM request WHERE core=2 ORDER BY RAND() LIMIT 3)
UNION ALL
(SELECT id,situation,info FROM request WHERE core=4 ORDER BY RAND() LIMIT 4));
have you thought about using the
...where core in (3,2,5...) ... Limit 30
?– Bulfaitelo
Yes, but each core has the limit specific.
– Carlos H. Araújo
What is the execution time of this query ?
– Bulfaitelo
this query that you assembled was not to return only 30 results, because returned the total of 43k ?
– Bulfaitelo
I think first is to remove this commando from the order by
– Felipe Aragão
My fault! copy/Paste, Result: (30 total, 2,340 sec)
– Carlos H. Araújo
Because you sort randomly ?
– Bulfaitelo
Felipe, it needs to be random.
– Carlos H. Araújo
I need to bring in random records.
– Carlos H. Araújo
I understand, but randomizing order by takes a lot of query performance.
– Felipe Aragão
Exactly!!! Still missing some 20,000 more records to enter...
– Carlos H. Araújo
I will answer with the only thing that can think to improve your code, test and ne speaks if improves the result.
– Bulfaitelo
What can help is to divide this query, instead of one, turns into 6 and loads them asynchronously, so the user reads the first ones while loading the others. Won’t improve performance, but improves user experience
– Costamilam