R
R
Roman L.2016-05-09 11:07:14
SQL
Roman L., 2016-05-09 11:07:14

Can this query be optimized?

There is a request:

(SELECT id,category_id FROM contents where  category_id=1 ORDER BY published DESC limit 4)  
UNION
(SELECT id,category_id FROM contents where  category_id=2 ORDER BY published DESC limit 4)      
UNION
(SELECT id,category_id FROM contents where category_id=3 ORDER BY published DESC limit 4)   
UNION
(SELECT id,category_id FROM contents where category_id=4 ORDER BY published DESC limit 4)  
order by  category_id

Is there an easier way to write it down?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey S., 2016-05-09
@Winsik

You are using order by, so it will iterate over the entire table. To speed up the work, you can make a function in which, with one request, and select 4 records from each category

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question