一、生成思路
利用mysql內存表插入速度快的特色,先利用函數和存儲過程在內存表中生成數據,而後再從內存表插入普通表中php
二、建立內存表及普通表
[sql] view plain copymysql
- CREATE TABLE `vote_record_memory` (
- `id` INT (11) NOT NULL AUTO_INCREMENT,
- `user_id` VARCHAR (20) NOT NULL,
- `vote_id` INT (11) NOT NULL,
- `group_id` INT (11) NOT NULL,
- `create_time` datetime NOT NULL,
- PRIMARY KEY (`id`),
- KEY `index_id` (`user_id`) USING HASH
- ) ENGINE = MEMORY AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8
[sql] view plain copylinux
- CREATE TABLE `vote_record` (
- `id` INT (11) NOT NULL AUTO_INCREMENT,
- `user_id` VARCHAR (20) NOT NULL,
- `vote_id` INT (11) NOT NULL,
- `group_id` INT (11) NOT NULL,
- `create_time` datetime NOT NULL,
- PRIMARY KEY (`id`),
- KEY `index_user_id` (`user_id`) USING HASH
- ) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8
三、建立函數及存儲過程
[sql] view plain copysql
- CREATE FUNCTION `rand_string`(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
[sql] view plain copy函數
- CREATE PROCEDURE `add_vote_memory`(IN n int)
- BEGIN
- DECLARE i INT DEFAULT 1;
- WHILE (i <= n ) DO
- INSERT into vote_record_memory (user_id,vote_id,group_id,create_time ) VALUEs (rand_string(20),FLOOR(RAND() * 1000),FLOOR(RAND() * 100) ,now() );
- set i=i+1;
- END WHILE;
- END
四、調用存儲過程
[sql] view plain copy性能
- CALL add_vote_memory(1000000)
根據電腦性能不能所花時間不同,大概時間在小時級別,若是報錯內存滿了,只在修改max_heap_table_size 個參數便可,win7修改位置以下,linux,修改my.cnf文件,修改後要重啓mysql,重啓後內存表數據會丟失spa
五、插入普通表中
[sql] view plain copy.net
- INSERT into vote_record SELECT * from vote_record_memory
六、結果
更多php技術交流,可加Q羣:884743303,裏面各路大神爲您保駕護航,blog