Home >Backend Development >PHP Tutorial >What are the pros and cons of doing data correlation in an application versus using join queries directly?
What are the advantages and disadvantages of performing data correlation in an application (that is, executing multiple queries and correlating the results) and using correlation queries directly?
In what scenarios do these two methods perform better?
What are the advantages and disadvantages of performing data correlation in an application (that is, executing multiple queries and correlating the results) and using correlation queries directly?
In what scenarios do these two methods perform better?
Generally speaking, use direct correlation query, because this is not something you can do without. If you want to reduce related queries and improve efficiency, you have to do a lot of extra work, which is simply various caches.
The simplest example: Order.StuatsId is associated with OrderStatus.Id
In order to display the order status
Method 1: Associated query
Method 2: OrderStatus is cached, and a Helper with the method getStatusTitleById is provided for the front desk to use, thereby avoiding association.
So projects with low concurrency pressure don’t need to pay too much attention to this, as it will have almost no impact, while projects with high concurrency pressure will specifically deal with this as a long-term task.
Conclusion: Generally speaking, the performance loss caused by using associated queries is imperceptible to visitors, but too many associations must be avoided (for example, only data from 2 tables is needed, but the actual query SQL associates 5 tables )
From the perspective of efficiency alone, the associated query efficiency is definitely higher
From the perspective of code readability, it should be more readable separately. And it can be reused.
The specific choice depends on the size of your project.