Analysis of MySQL duplicate index and redundant index examples

Analysis of MySQL duplicate index and redundant index examples

This article uses examples to describe MySQL duplicate indexes and redundant indexes. Share with you for your reference, the details are as follows:

Duplicate index : refers to multiple indexes created on one column or several columns in the same order.

Redundant indexes : The columns covered by the two indexes overlap

Redundant indexes use index coverage in some special scenarios, so they are faster.

Scenario

For example, the article and tag table

+——+——-+——+
| id | artid | tag |
+——+——-+——+
| 1 | 1 | PHP |
| 2 | 1 | Linux |
| 3 | 2 | MySQl |
| 4 | 2 | Oracle |
+——+——-+——+

In actual use, there are two types of queries

  • artid - query article - tag
  • tag—query article’s—artid

SQL statement:

select tag from t11 where artid=2;
select artid from t11 where tag='PHP';

We can create redundant indexes to achieve index coverage, which will increase query efficiency.

1. Create an article tag table

There are two indexes in this table, one is at and the other is ta. Both indexes use the artid and tag fields.

CREATE TABLE `t16` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `artid` int(10) unsigned NOT NULL DEFAULT '0',
 `tag` char(20) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`),
 KEY `at` (`artid`,`tag`),
 KEY `ta` (`tag`,`artid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

2. Test two SQL statements

select artid from t11 where tag='PHP';

The Extra in the query analysis of this statement contains Using index, which means that index coverage is used here. After using index coverage, there is no need to return rows to query data, so the query efficiency is relatively high.

這里寫圖片描述

select tag from t11 where artid = 1;

The Extra in the query analysis of this statement contains Using index, which means that index coverage is used here. After using index coverage, there is no need to return rows to query data, so the query efficiency is relatively high.

列表內容

For more information about index coverage, please refer to the previous article: Index Coverage

Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL query redundant indexes and unused index operations
  • Detailed explanation of redundant and duplicate indexes in MySQL
  • The difference between redundant and duplicate indexes in MySQL

<<:  Graphic tutorial on installing Mac system in virtual machine under win10

>>:  WeChat applet implements calculator function

Recommend

In-depth explanation of the locking mechanism in MySQL

Preface In order to ensure the consistency and in...

Vue+js realizes video fade-in and fade-out effect

Vue+js realizes the fade in and fade out of the v...

How to change $ to # in Linux

In this system, the # sign represents the root us...

Summary of various methods of implementing article dividing line styles with CSS

This article summarizes various ways to implement...

Detailed analysis of the difference between Ref and Reactive in Vue3.0

Table of contents Ref and Reactive Ref Reactive T...

A Deep Understanding of Angle Brackets in Bash (For Beginners)

Preface Bash has many important built-in commands...

Steps to encapsulate the carousel component in vue3.0

Table of contents 1: Encapsulation idea 2. Packag...

VMware virtual machine three connection methods example analysis

NAT In this way, the virtual machine's networ...

How to use CocosCreator object pool

Table of contents Preface: Specific operations St...

Some CSS questions you may be asked during an interview

This article is just to commemorate those CSS que...

Vue implements page caching function

This article example shares the specific code of ...

MySQL optimization: how to write high-quality SQL statements

Preface There are a lot of information and method...

MySQL multi-instance configuration solution

1.1 What is MySQL multi-instance? Simply put, MyS...