Detailed explanation of basic operation of Mysql index

Please pay attention to my new home: www.taoyongpan.xin/2017/12/05/sql001/

Website in the eyes of college students

I'm also a junior, and I definitely don't mean to belittle.

  When we knew that we were admitted to the School of Computer Science, we all started our university with curiosity and anxiety; the curiosity is: how are these websites and APPs that we use in our daily life made? Will I do this in the future? What I am worried about is: wco I usually play games and watch videos, can I learn those things? When we first came into contact with the code, we had a spontaneous sense of pretense, and we couldn't help but post a comment, telling the people around me that I am also the kind of person who uses code on TV; after a year of Ups and downs, most of us are already small developers. We can make various websites and APPs with our own hands, and our hearts are full of ambition. But what is the efficiency of our website? It is obviously reduced, how should we improve our query efficiency? Cache or index, that is the question.
  Using caching is a method. Now there are Redis and Memcached which are popular, but when dealing with some dynamic data, our caching method will definitely have problems. I will focus on explaining it in another blog; this article is mainly about database index is used;

Randomly generate one million data

  We use database indexes to deal with big data problems, but we don't have data, which is really stumbling for a bunch of heroes; but big brother, when we have to pay attention, how much data cannot be automatically generated by the computer, he is ours Silly brother, then let's teach this silly little brother, first generate a million data:

Create a simple user table

CREATE TABLE `t_user_memory` (  
    `id` INT (11) NOT NULL AUTO_INCREMENT,  
    `username` VARCHAR(50) NOT NULL DDEFAULT '',  
    `password` VARCHAR(50) NOT NULL DDEFAULT '',  
    PRIMARY KEY (`id`),  
) ENGINE = MEMORY AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8 ;

 Do the following in sequence:

1. A function that randomly generates a string of length n:

CREATE FUNCTION `rand_str`(n INT) RETURNS varchar(255) CHARSET latin1  
BEGIN   
DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';   
DECLARE return_str varchar(255) DEFAULT '' ;  
DECLARE i INT DEFAULT 0;   
WHILE i < n DO   
SET return_str = concat(return_str,substring(chars_str , FLOOR(1 + RAND()*62 ),1));   
SET i = i +1;   
END WHILE;   
RETURN return_str;   
END  

 2. A function to add n pieces of data to the database:

CREATE  PROCEDURE `add_user`(IN n int)  
BEGIN    
  DECLARE i INT DEFAULT 1;  
    WHILE (i <= n ) DO  
      INSERT into t_user_memory (username,password) VALUES (rand_str(20),rand_str(20));  
            set i=i+1;  
    END WHILE;  
END  

 3. Call the insert function and insert one million data:

CALL add_user(1000000)

 Simple violence, after adding one million data, we can change the size of the number of inserts according to our own needs;

Create database index

normal index

This is the most basic index type, and it has no restrictions like uniqueness;

1. Create

CREATE INDEX indexId ON t_user_memory(id)

 2. Index deletion

DROP INDEX indexId ON  t_user_memory

 

unique index

This kind of index is basically the same as the previous "normal index", but there is one difference: all values ​​of the indexed column can only appear once, that is, they must be unique.

1. Create

CREATE UNIQUE INDEX indexId ON t_user_memory(id);

 2. Delete is the same as ordinary index

primary key index

It is a special unique index that does not allow nulls. Generally, the primary key index, which is our primary key, is created at the same time as the table is built.

Single Column Index & Composite Index

single column index

Still using the above example, when we searched for a user with username = “Taoyongpan”, we investigated 10 items, but the passwords for each item were different. The data is placed on an intermediate result set, and then the passwords are compared and excluded one by one. It is the same principle that we add an index to the password;

composite index

When we put username and password into an index, we will directly find the piece of data we need. When the amount of data is very large, it will greatly improve our search speed. This is the combined index;

1. Create

ALTER TABLE t_user_memory ADD INDEX indexUser (username,password)

 2. The delete operation is the same as above

Pros and Cons of Database Indexing

profit

1. The indexed column can ensure the uniqueness of the row and generate a unique rowId
2. The establishment of the index can effectively shorten the retrieval time of the data
3. The establishment of the index can speed up the connection between the table and the table
4. It is used for sorting or grouping Adding indexes to the fields can speed up grouping and sorting order

cons

1、创建索引和维护索引需要时间成本,这个成本随着数据量的增加而加大
2、创建索引和维护索引需要空间成本,每一条索引都要占据数据库的物理存储空间,数据量越大,占用空间也越大(数据表占据的是数据库的数据空间)
3、虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。

下篇预告

下一篇我们一起来学习一下,索引的原理是怎么的,为什么可以提高查询效率呢,等问题,我们下一篇再见。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326468490&siteId=291194637