When you join multiple tables to query data, write multiple SQL statements for separate query, or write an SQL statement for joint query?
When you join multiple tables to query data, write multiple SQL statements for separate query, or write an SQL statement for joint query?
Reply content:
When you join multiple tables to query data, write multiple SQL statements for separate query, or write an SQL statement for joint query?
Separate strongly recommended
Complex queries take a long time for a single query. Separate queries take a short time but the total query time is long. separate queries mean that each database lock takes a short time and can be quickly switched between multiple requests. The throughput of the entire system will be large, and complex queries will easily become serious queues. of course, this statement is only effective for several basic mysql engines. For commercial engines, such as snapshot and isolation, these effects can be ignored. however, in my opinion, the most important thing about code is not efficiency, not design, but understanding. Short statements are obviously easier to understand than complex nested statements, especially for others to understand, therefore, we recommend that you do not use complex queries or functions that cannot be reflected in query statements, such as foreign key constraints and stored procedures.
1. There is no cache. If there is a cache, it is recommended that all queries be separated;
2. If no cache is available and the Combined Query is not complex, the Combined Query is performed;
3. There is no cache, and the combined query is very complicated. We recommend that you separate complicated queries.
If the amount of simple data is small, you can perform joint queries. If the amount of complex data is large, it is best to perform separate queries.
The number and scenario of tables must be viewed.
It depends on the scenario to decide which one is better.
If the data volume is not found through a large connection, a convenient and quick solution is provided.
However, if there is more data and it is a table-Level Lock, the system will easily collapse.