Ali standard does not recommend multi-table join, but how to write this SQL?

Preface

Let's first look at the description of the Ali development manual

image

The manual says [Mandatory], but Fei Chao believes that many of the code in the project does not meet this requirement. But the key question is how to write this SQL without joining!

High-performance MySQL

I believe everyone has read the book High Performance MySQL, and 分解大的查询it is mentioned in this section.

Decompose the associated query, that is, perform a single-table query for each table to be associated, and then associate the results in the application. The following query:

SELECT * FROM tag
    JOIN tag_post ON tag_post.tag_id=tag.id
    JOIN post ON tag_post.post_id=post.id
WHERE tag.tag = 'mysql';

Can be broken down into the following queries instead:

SELECT * FROM tag WHERE tag = 'mysql';
SELECT * FROM tag_post WHERE tag_id = 1234;
SELECT * FROM post WHERE post.id in (123,456,567,9098,8904);

However, this scheme also has obvious problems, that is, the infollowing parameters may be too many, which shows that the versatility of this scheme is actually very limited.

Know almost

Let's take a look at the answer of Li Chenxi, the big brother of Zhihu database. (Original address https://www.zhihu.com/question/56236190/answer/153450286)

When construction of the table, put the columns in a table, such as a start there student(id, name), class(id, description), student_class(student_id, class_id)three tables, this is in line with the database paradigm (the first paradigm, the second paradigm, the third paradigm, BC paradigm, etc.), no Any redundancy, but it does not meet the "programming specifications" immediately, then we can replace it with a large table student_class_full(student_id, class_id, name, description), so that the name and description may be stored in multiple copies, but since no join is required, the query performance can be improved A lot. Any standard is a kind of compromise under a specific situation, and it may not be established if it is out of this environment.

The specific approach and pros and cons of the solution Fei Chao believes that it is very clear.

Tell your story

So, does your company have many multi-table join situations? Which solution is used to solve it, or just treat it as if it is not solved! Welcome to leave a message to tell Fei Chao.



image



Guess you like

Origin blog.51cto.com/15082403/2591235