1. 程式人生 > >mysql的儲存引擎innodb、myisam對插入影響和索引對插入的影響

mysql的儲存引擎innodb、myisam對插入影響和索引對插入的影響

前言

一直好奇mysql的儲存引擎innodb和myisam對插入影響和索引對插入的影響。
這次我就來做個測試,以下測試供大家參考。

drop table userinfo;
CREATE TABLE userinfo
(
    uuid int(11) auto_increment NOT NULL,
    name varchar(64) NOT NULL DEFAULT '',
    email varchar(64) NOT NULL DEFAULT '',
    password varchar(64) NOT NULL DEFAULT '',
    dob date
DEFAULT NULL, address varchar(255) NOT NULL DEFAULT '', city varchar(64) NOT NULL DEFAULT '', state_id tinyint unsigned NOT NULL DEFAULT '0', zip varchar(8) NOT NULL DEFAULT '', country_id smallint unsigned NOT NULL DEFAULT '0', gender enum('M','F') NOT NULL DEFAULT 'M', account_type varchar
(32) NOT NULL DEFAULT '', verified tinyint NOT NULL DEFAULT '0', allow_mall tinyint unsigned NOT NULL DEFAULT '0', parrent_account int unsigned NOT NULL DEFAULT '0', closest_airport varchar(3) NOT NULL DEFAULT '', PRIMARY KEY(uuid), UNIQUE KEY email (email), KEY country_id (country_id), KEY
state_id (state_id), KEY state_id_2 (state_id,city,address) )ENGINE=InnoDB;
set global log_bin_trust_function_creators = 1;

DROP FUNCTION IF EXISTS rand_string;
DELIMITER $$
CREATE FUNCTION rand_string(n INT)
RETURNS VARCHAR(255)
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 $$
DELIMITER ;
  CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_userinfo_uuid`(IN item INTEGER)
     BEGIN
     DECLARE counter INT;
     SET counter = item;
     WHILE counter >= 1 DO
     insert into userinfo (uuid,name,email,password,dob,address,city,state_id,zip,country_id,gender,account_type,verified,allow_mall,parrent_account,closest_airport)
         values(uuid(),rand_string(64), rand_string(64), rand_string(64), '2010-10-10', rand_string(255), rand_string(64), ceil(rand() * 100), rand_string(8),
          ceil(rand() * 100), 'M', rand_string(32), 0, 0, 0, rand_string(3));
     SET counter = counter - 1;
     END WHILE;
     END$$

     DELIMITER ;

為了節省時間,10000條資料測試。

innodb下面的資料資料插入(去掉所有的索引,沒有uuid列)

這裡寫圖片描述

myisam下面的資料資料插入(去掉所有的索引,沒有uuid列)

這裡寫圖片描述

總之這個差距還是有的,如果資料量在足夠大的話,差距會更明顯。
如果insert大量的資料,不妨先將儲存引擎先設為myisam,匯入資料後改為innodb

主鍵索引下面的資料資料插入(同為innodb,主鍵索引和無主鍵索引的比較,可以參考上面測試innodb那個)

這裡寫圖片描述

這個和第一個測試的差距還是比較小的,但是資料量不是很大。
好了就玩到此,如若哪天有興趣,在跑個多點的資料。