How to create an index on a join table in MySQL

How to create an index on a join table in MySQL

This article introduces how to create an index on a join table that associates two tables in MySQL. I share it for your reference. Let's take a look at the detailed introduction:

Problem Introduction

When creating a database index, you can choose a single-column index or a composite index.

In the following situation, the user table (user) and the department table (dept) are connected through the department user association table (deptuser), as shown in the following figure:


Relationships between tables

The question is, how to create an index in this association table?

There are four options for this table:

  • Create a single column index idx_user for user_uuid
  • Create a single column index idx_dept for user_dept
  • Create a composite index idx_user_dept, that is (user_uuid, dept_uuid)
  • Create a composite index idx_dept_user, that is (dept_uuid, user_uuid)

There are four situations for querying related tables:

-- 1. Check the department to which a person belongs using the AND method EXPLAIN SELECT d.dept_name,u.* FROM org_dept d,org_user u,org_dept_user duser WHERE u.user_uuid=duser.user_uuid AND d.dept_uuid=duser.dept_uuid AND u.user_code="dev1";
-- 2. Use join method to check the department to which personnel belong EXPLAIN SELECT d.dept_name,u.* FROM org_user u LEFT JOIN org_dept_user du ON u.user_uuid=du.user_uuid LEFT JOIN org_dept d ON du.dept_uuid=d.dept_uuid WHERE u.user_code="dev1";
-- 3. Use the AND method to check personnel by department EXPLAIN SELECT d.dept_name,u.* FROM org_dept d,org_user u,org_dept_user du WHERE u.user_uuid=du.user_uuid AND d.dept_uuid=du.dept_uuid AND d.dept_code="D006";
-- 4. Use join method to check the personnel in the department EXPLAIN SELECT d.dept_name,u.* FROM org_dept d LEFT JOIN org_dept_user du ON d.dept_uuid=du.dept_uuid LEFT JOIN org_user u ON u.user_uuid=du.user_uuid WHERE d.dept_code="D006";

Test verification

1. Personnel check department using the and method

1.1 The associated table has no index


1.2 Single index Idx_dept


1.3 Single Index Idx_user


1.4 Composite Index Idx_dept_user


1.5 Composite Index Idx_user_dept


1.6 All built


2. Use join method to check the department to which personnel belong

2.1 The associated table has no index

2.2 Single Index Idx_dept


2.3 Single Index Idx_user

2.4 Composite Index Idx_dept_user


2.5 Composite Index Idx_user_dept


2.6 All are built


3. Departments use the AND method to check personnel

3.1 The associated table has no index

3.2 Single Index Idx_dept


3.3 Single Index Idx_user


3.4 Composite Index Idx_dept_user


3.5 Composite Index Idx_user_dept


3.6 All are built


4. Departments use join to check their personnel

4.1 No index on the associated table


4.2 Single Index Idx_dept


4.3 Single Index Idx_user

4.4 Composite Index Idx_dept_user


4.5 Composite Index Idx_user_dept


4.6 All are built

in conclusion

The following conclusions can be drawn from the actual test results above: It is optimal to create single-column indexes idx_user and idx_dept for user_uuid and dept_uuid respectively for this association table.

The index idx_user is applicable to querying the department of a person through the person ID; the index idx_dept is applicable to querying the persons under the department through the department.

other

Test Data

Test.sql

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • MySQL and Oracle data type correspondence (tabular form)
  • Detailed explanation of creating a data table in MySQL and establishing primary and foreign key relationships
  • Summary of MySQL foreign key constraints and table relationships
  • Python saves dict dictionary type data to Mysql and automatically creates tables and columns
  • MySQL and PHP basics and application topics: creating database tables
  • How to quickly create a test data table with 8 million entries in MySQL
  • MySQL creates three relationship tables in practice

<<:  JavaScript to implement image preloading and lazy loading

>>:  Detailed explanation of the solution to the Baidu https authentication prompt "Please redirect your http site 301 to the https site"

Recommend

JavaScript to implement the back to top button

This article shares the specific code for JavaScr...

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance In the above...

How to monitor Tomcat using LambdaProbe

Introduction: Lambda Probe (formerly known as Tom...

Markup Language - Title

Click here to return to the 123WORDPRESS.COM HTML ...

Some suggestions for Linux system optimization (kernel optimization)

Disable swap If the server is running a database ...

Implementation of communication between Vue and Flask

Install axios and implement communication Here we...

JS implements a simple counter

Use HTML CSS and JavaScript to implement a simple...

The difference between docker run and start

The difference between run and start in docker Do...

About dynamically adding routes based on user permissions in Vue

Display different menu pages according to the use...

foreman ubuntu16 quick installation

Quickstart Guide The Foreman installer is a colle...

Detailed explanation of the use of React list bar and shopping cart components

This article example shares the specific code of ...

MYSQL slow query and log example explanation

1. Introduction By enabling the slow query log, M...