MYSQL database basics - Join operation principle

MYSQL database basics - Join operation principle

Join uses the Nested-Loop Join algorithm. There are three types of Nested-Loop Join.

select * from t1 join t2 on t1.a = t2.a;
-- a 100 data items, b 1000 data items

Simple Nested-Loop Join

The entire table t1 will be traversed. T1 is used as the driving table. Each data in t1 will be queried in the entire table in t2. This process will be compared 100*1000 times.

Every time a full table query is performed in t2, the full table scan is not guaranteed to be in memory, the Buffer Pool will be eliminated, and it may be on disk.

Block Nested-Loop Join (MySQL driver link does not use index)

It will traverse the entire t1 table, load the t1 data into join_buffer, and then traverse the entire t2 table to match each piece of data in t2 with the data cached in t1 in join_buffer.

t1 full table scan = 100 times

t2 full table scan = 1000 times

Number of queries = 1100

Comparisons in join_buffer = 100 * 1000 times

The number of comparisons is the same as that of Simple Nested-Loop Join, but the comparison process is much faster than Simple Nested-Loop Join and has better performance.

join_buffer has a size. If the data found in t1 is larger than the size of join_buffer, part of the data in t1 will be loaded first. After comparing t2, join_buffer will be cleared and the remaining data in t1 will be loaded. If the loading is incomplete, the operation will be repeated.

The number of full table scans for t1 remains the same as the number in join_buffer 1, but the number of scans for t2 is multiplied by the number of segments.

Assume that the number of data rows in the driving table is N, which needs to be divided into K segments to complete the algorithm process, and the number of data rows in the driven table is M.

K = λ * N

Scan the driven table times = M * λ * N

λ is related to the size of join_buffer. When the join_buffer size is large enough, the time for large table driver and small table driver is the same.

When segmentation is required, the fewer the segmentation times, the fewer times the driven table is scanned, so a small table driver should be used.

Index Nested-Loop Join (MySQL driver link uses index)

Let’s take the above SQL as an example, if field a is indexed.

The entire t1 table will be scanned, and each data in the t1 table will be indexed in the t2 table. After the ID is found, the table will be queried again (if the connection field is the primary key of the t2 table, the table retrieval operation will be omitted).

t1 scans the entire table = 100 times

t2 index queries = log1000 times

t2 table query = log1000 times

Assume that the number of data rows in the driving table is N, and the number of data rows in the driven table is M.

Total number of queries = N + N * 2logM

As can be seen from the above, the larger the data in the driving table, the more queries there will be, so a small table should be used as the driving table.

The article refers to "MySQL Practical 45 Lectures--Lecture 34"

Summarize

This is the end of this article about the basics of MYSQL database Join operation principle. For more relevant MYSQL Join principle content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of seven MySQL JOIN types
  • MySQL join buffer principle
  • Specific usage instructions for mysql-joins
  • Mysql join query syntax and examples
  • Summary of various common join table query examples in MySQL
  • Specific use of MySQL's seven JOINs

<<:  HTML pop-up div is very useful to realize mobile centering

>>:  Detailed explanation of the commonly used functions copy_from_user open read write in Linux driver development

Recommend

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...

Swiper.js plugin makes it super easy to implement carousel images

Swiper is a sliding special effects plug-in built...

Detailed explanation of MySQL index principles and optimization

Preface This article was written by a big shot fr...

Detailed explanation of Javascript basics

Table of contents variable Data Types Extension P...

Detailed explanation of Apache SkyWalking alarm configuration guide

Apache SkyWalking Apache SkyWalking is an applica...

Kill a bunch of MySQL databases with just a shell script like this (recommended)

I was woken up by a phone call early in the morni...

Implementation of MySQL GRANT user authorization

Authorization is to grant certain permissions to ...

An example of using Lvs+Nginx cluster to build a high-concurrency architecture

Table of contents 1. Lvs Introduction 2. Lvs load...

Detailed steps for using AES.js in Vue

Use of AES encryption Data transmission encryptio...

How to implement rounded corners with CSS3 using JS

I found an example when I was looking for a way t...

An article to master MySQL index query optimization skills

Preface This article summarizes some common MySQL...

Detailed tutorial on how to install mysql8.0 using Linux yum command

1. Do a good job of cleaning before installation ...

Solution to 404 error when downloading apk file from IIS server

Recently, when using IIS as a server, the apk fil...

The hottest trends in web design UI in 2013 The most popular UI designs

Time flies, and in just six days, 2013 will becom...