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

avue-crud implementation example of multi-level complex dynamic header

Table of contents Preface Background data splicin...

Vue+Element UI realizes the encapsulation of drop-down menu

This article example shares the specific code of ...

JavaScript style object and CurrentStyle object case study

1. Style object The style object represents a sin...

MySQL max_allowed_packet setting

max_allowed_packet is a parameter in MySQL that i...

SVG button example code based on CSS animation

The specific code is as follows: <a href="...

Detailed usage of kubernetes object Volume

Overview Volume is the abstraction and virtualiza...

Detailed explanation of the general steps for SQL statement optimization

Preface This article mainly shares with you the g...

The principle and application of MySQL connection query

Overview One of the most powerful features of MyS...

MySQL 5.7 zip archive version installation tutorial

This article shares the installation tutorial of ...

jQuery achieves the shutter effect (using li positioning)

This article shares the specific code of jQuery t...

Detailed steps to build the TypeScript environment and deploy it to VSCode

Table of contents TypeScript environment construc...

How to use CSS custom variables in Vue

Table of contents The CSS custom variable functio...

How to recover accidentally deleted messages files in Linux

If there are files that are being used by a proce...

Difference and implementation of JavaScript anti-shake and throttling

Table of contents 1. Anti-shake 2. Throttling 3. ...

Summary of 4 methods of div+css layout to achieve 2-end alignment of css

The div+css layout to achieve 2-end alignment is ...