MySQL multiple single-table queries and multiple table joins query

Tip: not recommended more than three tables of multi-table joint inquiry
on the amount of data applications, the development of efficient multi-table joint inquiry, but multi-table joins query large volumes of data in the table, and there is no index of the time, if Cartesian product, the amount of data that will be very large, sql efficiency will be very low

Multiple single-table queries to merge the benefits in service layer:
1, higher cache efficiency, many applications can easily cache single-table queries corresponding result object. If the association is a table changed, then you can not use the query cache, while after the split, if a table is rarely changed, then the query based on the table you can re-use the query cache result.
2, multi-table joint list information page pagination, only need to show a part of the data, if it is a multi-table joint inquiry should check out all of the data link re-execute limit, single-table query if it is repeated, the first single table screening, performed first and then with the rest of the table to limit association, will greatly reduce the amount of data
3, if no separate read and write the database (master from backup), at the time of high concurrency, since the write exclusive lock table is added, the multi- table size joint inquiry into single-table queries after the lock becomes smaller, reducing the lock of the competition
4, after the large amount of data, the method will generally sub-library sub-table to ease the pressure on the database, the use of single-table queries over multiple tables joint inquiry easier sub-libraries, does not require extensive modifications sql statement, more scalable middleware sub-library sub-table of general support for cross-database join bad
5, the query itself efficiency may also be improved. Query id set time, using the IN () instead of the relational query, allowing a query MySQL ID order, it may be more efficient than random association.
6, when business growth, as the bottom of the database, most likely to encounter a problem, stand-alone databases is very expensive computing resources, databases simultaneously read and write to the service, need to consume CPU, in order to allow the database becomes higher throughput,
and business subtle and do not care about the hundreds of milliseconds to delay the gap, the business will put more computing service layer to do, after all, a good level of computing resources to expand the database is difficult ah, this is a heavy traffic light DB's architecture
7, the query can reduce redundant records, make related inquiries at the application layer, means that for a record application only needs to query once, and do related query in the database, you may need to repeat the access part of the data.
Furthermore, by doing this, to achieve the hash associated in the application, rather than using a nested loop associated with MySQL. Hash efficiency associated with some scenes is much higher.

Merge multiple single-table queries the service layer disadvantages:
1, require multiple database connections
2, the code is more complex

Summary:
personally think it is better to do multiple single-table queries, more scalable, of course, when the amount of data, more convenient and direct the development of the joint inquiry

Guess you like

Origin www.cnblogs.com/youmingDDD/p/11921187.html