Optimizing a query returning a lot of records, a way to avoid hundreds of join. Is it a smart solution?

后端 未结 3 1389
攒了一身酷
攒了一身酷 2021-01-29 04:08

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

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-29 05:14

    JOINs -- 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.

提交回复
热议问题