MySQL JOIN Abuse? How bad can it get?

后端 未结 5 1071
星月不相逢
星月不相逢 2021-01-31 12:36

I\'ve been reading a lot about Relational Databases using many JOIN statements on every SELECT. However, I\'ve been wondering if there\'s any performance problem on the long run

5条回答
  •  孤城傲影
    2021-01-31 13:30

    As others have said - joins aren't a thing to avoid at all. In fact, in most models it is rare not to have a few joins in every single query that the application runs.

    Even in the biggest queries they aren't usually a performance problems - and often fix performance problems that would occur if you have redundant and repeating data all over the place.

    However, be aware that under the cover the database just joins two tables at a time. So, joins necessitate multiple steps by the database that are invisible to the developer. When it does these joins it has to make a few decisions about how to go about it:

    • walk through all the values on the left table, then match them one at a time to the values on the right?
    • Do just the opposite?
    • Sort the keys from both tables and walk through them at the same time?
    • Build hashes of keys on both sides?
    • Apply filtering criteria before or after a given join?
    • etc

    So, if your joins are complex ultimately the efficiency will be driven by the sophistication of your optimizer/planner and the currency and detail of your statistics. MySQL isn't a strong contender here - so I'd generally keep my model and sql a little simpler than if I was using something else. But a few joins per query should almost always be fine.

提交回复
热议问题