MySQL establishes efficient index example analysis

MySQL establishes efficient index example analysis

This article uses examples to describe how to create efficient indexes in MySQL. Share with you for your reference, the details are as follows:

How to create an ideal index?

  • Query frequency
  • Discrimination
  • Index length
  • Overwrite Fields

Discrimination

Assuming there are 1 million users, the genders are basically 500,000 males and 500,000 females, so the differentiation is low.

Short length

The index length directly affects the size of the index file, the speed of addition, deletion and modification, and indirectly affects the query speed (occupies more memory).

High discrimination, short length

Question: What if we make the discrimination high and the length small?

Answer: You can create an index by cutting off the values ​​in the column from left to right.

(1) The shorter the cut, the higher the repetition, the lower the differentiation, and the worse the indexing effect. (2) The longer the cut, the lower the repetition, the higher the differentiation, and the better the indexing effect, but the greater the impact - the addition and deletion are slow, and the query speed is affected.

Therefore, we need to strike a balance between discrimination and length. Conventional method: Cut different lengths and test their discrimination.

Suppose we have a table: a vocabulary table for English Level 4, which contains 13,324 records. How do we add an index to the name field?

這里寫圖片描述

How to calculate discrimination?

Extract the first non-repeating number of a word:

select count(distinct left(name,1)) from dict

Total quantity:

select count(*) from dict

Distinction: unique number/total number, the SQL statement is as follows:

select (select count(distinct left(name,1)) from dict) / (select count(*) from dict) as rate;

Then follow these steps to find out the discrimination corresponding to other lengths. Looking at this chart, we can see that when the length is 11, the repetition rate is only 1%. We can consider creating an 11-bit index.

這里寫圖片描述

alter table dict add index name name(11);

The left prefix is ​​difficult to distinguish

Tips for creating indexes for columns whose left prefix is ​​not easily distinguished

Such as url column

http://www.baidu.com
http://www.web-bc.cn

The first 11 characters of the column are the same and difficult to distinguish. You can use the following two methods to solve this problem.

(1) Store the column contents in reverse order and create an index

moc.udiab.www//:ptth
nc.cb-bew.www//://ptth

This way the left prefix has great discrimination

(2) Pseudo-hash index effect

Store both url and url_hash columns

#Create table create table t10 (
id int primary key,
url char(60) not null default ''
);
#Insert into t10 values
(1,'http://www.baidu.com'),
(2,'http://www.sina.com'),
(3,'http://www.sohu.com.cn'),
(4,'http://www.onlinedown.net'),
(5,'http://www.gov.cn');
#Modify the table structure and add the urlcrc column alter table t10 add urlcrc int unsigned not null;

When storing, insert the crc32 code corresponding to the url into the database, and then create an index according to the urlcrc field. When searching, we convert the corresponding url into crc32 in the business layer for search, and then we can use the index.

Because the result of crc is a 32-bit int unsigned number, there will be duplication when the data exceeds 4 billion, but it is worth it. (The index length is int4 bytes)

Multi-column indexes

Considerations for multi-column indexes - column query frequency, column differentiation, and be sure to combine them with actual business scenarios

Taking ecshop as an example, cat_id and brand_id in the goods table are multi-column indexes. From the perspective of differentiation, brand_id has higher differentiation. However, from the actual business of the mall, customers generally choose large categories -> small categories -> brands first, and finally choose to create two indexes:

(1) index(cat_id,brand_id)
(2) index(cat_id,shop_price)

You can even add (3) index (cat_id, brand_id, shop_price), 3 redundant indexes

But the first two columns in (3) are the same as the first two columns in (1), so we can remove (1) and create two indexes.

index(cat_id,price) and index(cat_id,brand_id,shop_price);

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:
  • Analysis of MySQL duplicate index and redundant index examples
  • MySQL index coverage example analysis
  • Example analysis of the impact of MySQL index on sorting
  • In-depth explanation of the impact of NULL on indexes in MySQL
  • Detailed explanation of MySQL index principles and optimization
  • Understanding MySQL deadlock routines through unique index S lock and X lock
  • mysql add index mysql how to create index
  • MySQL index type summary and usage tips and precautions
  • How to view, create and delete indexes in MySQL
  • MySQL Create Index method, syntax structure and examples
  • Example analysis of the page splitting principle of MySQL clustered index

<<:  How to clean up the disk space occupied by Docker

>>:  WeChat applet calculator example

Recommend

Ubuntu 20.04 Best Configuration Guide (Newbie Essential)

1. System Configuration 1. Turn off sudo password...

SMS verification code login function based on antd pro (process analysis)

Table of contents summary Overall process front e...

Solve the Linux Tensorflow2.0 installation problem

conda update conda pip install tf-nightly-gpu-2.0...

Introducing ECharts into the Vue project

Table of contents 1. Installation 2. Introduction...

How to install Windows Server 2008 R2 on Dell R720 server

Note: All pictures in this article are collected ...

Advantages and disadvantages of Table layout and why it is not recommended

Disadvantages of Tables 1. Table takes up more byt...

How to block and prohibit web crawlers in Nginx server

Every website usually encounters many non-search ...

Nginx memory pool source code analysis

Table of contents Memory Pool Overview 1. nginx d...

How to compile the Linux kernel

1. Download the required kernel version 2. Upload...

Sharing the structure and expression principles of simple web page layout

Introduction to structure and performance HTML st...

Implementation steps for building a local web server on Centos8

1 Overview System centos8, use httpd to build a l...

Things to note when migrating MySQL to 8.0 (summary)

Password Mode PDO::__construct(): The server requ...

Web page layout should consider IE6 compatibility issues

The figure below shows the browser viewing rate i...