Home > Database > Mysql Tutorial > mysql利用存储过程批量插入数据_MySQL

mysql利用存储过程批量插入数据_MySQL

WBOY
Release: 2016-06-01 13:04:14
Original
1114 people have browsed it

最近需要测试一下mysql单表数据达到1000W条以上时增删改查的性能。由于没有现成的数据,因此自己构造,本文只是实例,以及简单的介绍。

首先当然是建表:

CREATE TABLE `fortest` (
  `ID` INT(30) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `IP` VARCHAR(32) NOT NULL,
  `OID` VARCHAR(15) DEFAULT NULL)
Copy after login

其次,构建存储过程:

DELIMITER $$

USE `插入表所在的数据库名字`$$

DROP PROCEDURE IF EXISTS `autoinsert`$$

CREATE DEFINER=`root`@`192.168.137.10` PROCEDURE `autoinsert`(IN IP_NUM INT, IN OID_NUM INT)
BEGIN
  DECLARE iIP INT DEFAULT 0 ;
  DECLARE iOID INT DEFAULT 0 ;   
   WHILE(iIP < IP_NUM)
    DO
      SET iOID = 0;
      WHILE(iOID<OID_NUM)
      DO
        SET @mySql=CONCAT("INSERT INTO fortest (IP, OID) VALUES(CONCAT((ROUND(RAND() * 255) + 1),&#39;.&#39;,(ROUND(RAND() * 255) + 1),&#39;.&#39;,(ROUND(RAND() * 255) + 1),&#39;.&#39;,(ROUND(RAND() * 255) + 1)),ROUND(RAND()*100)+1);");            
        PREPARE stmt FROM @mySql;
        EXECUTE stmt;  
        DEALLOCATE PREPARE stmt;
        SET iIP = iIP+1;
      END WHILE;
        SET iPC = iPC+1;
    END WHILE;

    END$$

DELIMITER ;
Copy after login

上述存储过程指定了两个输入参数:IP_NUM OID_NUM,两个参数分别指定了有多少台机器,以及每台机器有多少OID。

之后调用存储过程就可以了:

call autoinsert 1000 50
Copy after login

意思是,有100台机器,每个机器有50个参数。

这样,我们就构建了50000条数据,如果按上述存储过程,想达到1000W的数据,还是要花点时间的。可以采用如下方法,进一步提高速度:

首先创建具有同样表结构的表:

CREATE TABLE fortest_2 LIKE fortest;
Copy after login

然后根据fortest表插入5W条数据

INSERT INTOfortest_2(IP,OID) SELECT IP,ROUND(RAND() * 100) + 1) FROM fortest;
Copy after login

上述一条语句执行速度特别快,瞬间就插入了5W条数据。可以写个脚本执行:

#!/bin/bash

i=1;
MAX_INSERT_ROW_COUNT=$1;
j=0;
while [ $i -le $MAX_INSERT_ROW_COUNT ]
do
    time mysql -h192.168.137.1 -uroot -p123456 fortest -e "INSERT INTOfortest_2(IP,OID) SELECT IP,ROUND(RAND() * 100) + 1) FROM fortest;"
    echo "INSERT $i "    
    i=$(($i+1))
#    sleep 0.05
done

exit 0
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template