Which of the following is better for mysql multi-table joint query and separate query?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.