A
A
Alexander Pepel2018-11-14 11:45:34
MySQL
Alexander Pepel, 2018-11-14 11:45:34

How to optimize a query with 3 selects?

There is a task: Select records from a table where the creator is a certain user, but the user specified in the record may not be its creator. There is a second table with post histories. That is, you need to select all records from T2 with a minimum date (to find the first creator) and then select records from the first table by id.
I did like this, but it takes about 8 seconds. Can this be optimized?

select *
from t1
where id in ( 
                   select sh.order_id
                   from t2 sh
                   where updated_at = (
                                                     select min(updated_at)
                                                     from t2 zs
                                                     where sh.order_id = zs.order_id
                                                    )
                   and user_id = 21192
)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question