I am not so int SQL and I have the following doubt about how to optimize a query. I am using MySql
I have this DB schema:
And this is t
JOIN
s -- particularly on primary keys -- are not necessarily expensive. It looks like your joins are following the data model.
I wouldn't start optimizing the query without understanding its performance characteristics. How long does it take to run? How many records are being sorted to get the most recent?
Your WHERE
clause appears to be limiting the data considerably. You can also set up an index to help with the WHERE
clause clause -- however, because the fields come from different tables, it can be tricky to use indexes or all of them.
You have a complicated data model that is a bit difficult to follow. It seems possible that you are getting a Cartesian product due to multiple n-m relationships. If so, that can have a big impact on performance, and pre-aggregating the data along each dimension is the way to go.
However, I wouldn't start optimizing the query without understanding how the current one behaves.