How can MySQL use stored procedures to insert tens of millions of data to improve efficiency?

--MySQL test task: use stored procedures, insert tens of millions of data into the table, and optimize the speed according to the index
-- 1. Use index query
-- 2. Do not use index search
-- 3. Compare the difference in query speed between the two

-- 1. Create an index test table
DROP TABLE IF EXISTS index_test;
CREATE TABLE index_test(
id BIGINT(20) PRIMARY KEY NOT NULL AUTO_INCREMENT,
USER VARCHAR(16) DEFAULT NULL,
psd varchar(64) default null
/*psd mediumint DEFAULT 0 store random data*/
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*The MYISAM storage engine does not generate engine transactions, and the data insertion speed is extremely fast. For the convenience of quickly inserting tens of millions of test data, after we insert the data, change the storage type to InnoDB*/


-- 2. Create a stored procedure and insert data
DELIMITER $$
USE `xscj`$$
DROP PROCEDURE IF EXISTS `insert_data`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_data`(IN num INT)
BEGIN
DECLARE n INT DEFAULT 1;/*Define a variable to store the current execution times*/
WHILE n <= num
DO
  INSERT INTO index_test(USER,psd) VALUES(concat('user',n),password(n));/*concat() string connection, PASSWORD() encryption function*/
  /*INSERT INTO index_test(USER,psd) VALUES(CONCAT('用户',n),ret_pwd());*/
set n=n+1;
end while;
END$$
DELIMITER ;


-- 3. Create a function to randomly generate a password and return it to the pwd field
DELIMITER $$

CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    FUNCTION `xscj`.`ret_pwd`()
    RETURNS int(5)
    /*LANGUAGE SQL
    | [NOT] DETERMINISTIC
    | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
    | SQL SECURITY { DEFINER | INVOKER }
    | COMMENT 'string'*/
    BEGIN
DECLARE r int default 0;
SET r = floor(10+rand()*1000);
RETURN r;
    END$$

DELIMITER ;

-- 4. Call the stored procedure and insert 1 million pieces of data (total time: 6 min 35 sec)
CALL insert_data(10000000);

-- 5. This step can be ignored. Modify engine (execution time: 2 min 49 sec transmission time: 1.078 sec total time: 2 min 51 sec)
ALTER TABLE `index_test` ENGINE=INNODB;

-- 6. Query through the primary key index without using ordinary indexes (total time-consuming: 0.022 sec)
SELECT * FROM index_test WHERE id='1950000';

-- 7.不通过索引查询(总耗时:7.058 sec)
SELECT * FROM index_test WHERE USER='用户1950000'

-- 8.为USER字段创建普通索引,并通过该索引进行查询
CREATE INDEX index_user ON index_test (USER ASC);-- (总耗时:1 min 20 sec)
SELECT * FROM index_test WHERE USER='用户1950000';-- (总耗时:0.078 sec)

-- 9.使用user、pwd联合查询,由于user添加了索引,pwd未添加索引,故进行筛选查询是仍旧采用全表扫描,因此时间略有提升(总耗时:7.086 sec)
SELECT * FROM index_test WHERE USER='用户1950000' OR psd='*B810355CF0690506E5295AA66741D44E6AF4E61D';

-- 10.对user、pwd字段创建聚合索引后,再查询
CREATE INDEX index_userpwd ON index_test (USER,psd ASC);-- (总耗时:2 min 25 sec)
SELECT * FROM index_test WHERE USER='用户1950000' OR psd='*B810355CF0690506E5295AA66741D44E6AF4E61D';-- (总耗时:13.014 sec)


-- 查看表结构
DESC index_test;
-- 查询表数据
SELECT * FROM index_test;
-- 删除表数据
DELETE FROM index_test;
-- 删除索引
DROP INDEX index_user ON `index_test`;
DROP INDEX index_userpwd ON `index_test`;

Guess you like

Origin blog.csdn.net/youcheng_ge/article/details/77728189