当前位置:网站首页>MySQL-07
MySQL-07
2022-06-26 05:58:00 【Mr.Rop】
7、 Indexes
MySQL The official definition of index is : Indexes (index) Help MySQL Data structure for efficient data acquisition . Extract the sentence trunk , We can get the essence of index : An index is a data structure .
7.1、 Classification of indexes
In a table , The primary key index can only have one , There can be more than one unique index
- primary key (PRIMARY KEY)
- Unique identification , Do not repeat , There can only be one column as the primary key
- unique index (UNIQUE KEY)
- Avoid duplicate columns , The unique index can be repeated , Multiple columns can identify bits unique index
- General index (KEY/INDEX)
- default ,index,key Keyword to set
- Full-text index (FULLText)
- Under a specific database engine ,MyISAM
- Quickly locate data
Basic grammar
-- Use of index
-- 1、 Add indexes to fields when creating tables
-- 2、 After creation , Add index
-- Show all information
SHOW INDEX FROM student
-- Add an index
ALTER TABLE school.student ADD FULLTEXT INDEX `StudentName` (`StudentName`);
-- analysis sql Execution status
EXPLAIN SELECT * FROM student; -- General index , Non full text index
EXPLAIN SELECT * FROM student WHERE MATCH(StudentName) AGAINST(' Zhang ')
7.2、 Test index
CREATE TABLE `app_user` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) DEFAULT '' COMMENT ' The user nickname ',
`eamil` VARCHAR(50) NOT NULL COMMENT ' User mailbox ',
`phone` VARCHAR(20) DEFAULT '' COMMENT ' cell-phone number ',
`gender` TINYINT(4) UNSIGNED DEFAULT '0' COMMENT ' Gender (0: male ;1: Woman )',
`password` VARCHAR(100) NOT NULL DEFAULT '' COMMENT ' password ',
`age` TINYINT(4) DEFAULT NULL COMMENT ' Age ',
`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
-- Insert 100 Ten thousand data
DELIMITER $$ -- Before you write a function, you have to write , sign
CREATE FUNCTION mock_data()
RETURNS INT
BEGIN
DECLARE num INT DEFAULT 1000000;
DECLARE i INT DEFAULT 0;
WHILE i<num DO
-- Insert statement
INSERT INTO app_user(`name`,`eamil`,`phone`,`gender`,`password`,`age`)
VALUES(CONCAT(' user ',i),'[email protected]'
,CONCAT('18',FLOOR(RAND()*(999999999-1000000000)+100000000))
,FLOOR(RAND()*2),UUID(),FLOOR(RAND()*100));
SET i =i+1;
END WHILE;
RETURN i;
END;
SELECT mock_data();
-- test
SELECT * FROM app_user WHERE `name` =' user 9999';
SELECT * FROM app_user WHERE `name` =' user 9999';
EXPLAIN SELECT * FROM app_user WHERE `name` =' user 9999';
SELECT * FROM student;
-- id_ Table name _ Field name
CREATE INDEX id_app_user_name ON app_user (`name`);
SELECT * FROM app_user WHERE `name` =' user 9999';
EXPLAIN SELECT * FROM app_user WHERE `name` =' user 9999';
There is an index :
No index :
Index in a small amount of data , Not very useful . But when it comes to big data , The difference is obvious
7.3、 Indexing principles
- More indexes is not better
- Don't index process change data
- A table with a small amount of data does not need to be indexed
- The index is usually added to the fields commonly used for query
The data structure of the index
Hash Index of type
Btree:INOODB The default data structure of
边栏推荐
- SQL query time period content
- Posting - don't get lost in the ocean of Technology
- Navicat如何将当前连接信息复用另一台电脑
- numpy.tile()
- Func < T, tresult > Commission - learning record
- Household accounting procedures (the second edition includes a cycle)
- 状态模式,身随心变
- Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
- Overloading and overriding
- ES6的搭配环境
猜你喜欢
随机推荐
新的征程
Cython入门
Spark source code analysis (I): RDD collection data - partition data allocation
小程序第三方微信授权登录的实现
Consul服务注册与发现
Soft power and hard power in program development
A love that never leaves
Func < T, tresult > Commission - learning record
Posting - don't get lost in the ocean of Technology
numpy.random.choice
SSH keygen specifies the path
劣币驱逐良币的思考
售前分析
Bubble sort
【C語言】深度剖析數據在內存中的存儲
How Navicat reuses the current connection information to another computer
Life is so fragile
Class and object learning
C generic speed
Day3 - variables and operators