PHPはloadコマンドを利用して百(千)万数データ時間対比を生成する

5844 ワード

千万のデータを挿入して、テストやmysqlのパフォーマンスの学習に使用します.シナリオは一般的にinsertロット挿入、loadがあります.他の言語でサポートされる必要のないストレージ・プロシージャもあります.PHPとloadを利用する方法について説明します.
データベースmysql_testテーブルusers,MyISAMエンジン:
CREATE TABLE `users` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '  ID',
    `uuid` CHAR(32) NOT NULL,
    `name` VARCHAR(32) NOT NULL COMMENT '   ',
    `email` VARCHAR(32) NOT NULL COMMENT '  ',
    `pwd` VARCHAR(64) NOT NULL COMMENT '  ',
    `sex` ENUM('M','F','S') NOT NULL COMMENT '  ',
    `status` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '\'   \', \'  \', \'  \'',
    `created_at` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '    ',
    `remark` TEXT NULL COMMENT '  ',
    PRIMARY KEY (`id`)
)
COMMENT='   '
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

(id以外の)インデックスを含まない:=====start=====1 run 100000 lines,using 0.568033 s 2 run 100000 lines,using 0.569032 s 3 run 100000 lines,using 0.571033 s 4 run 100000 lines,using 0.585034 s 5 run 100000 lines,using 0.596034 s 6 run 100000 lines,using 0.586033 s 7 run 100000 lines,using 0.590034 s 8 run 100000 lines, using 0.593034 s 9 run 100000 lines, using 0.600034 s 10 run 100000 lines, using 0.597034 s total time 5.862335 s =====end=====
索引:
ALTER TABLE `users` ADD UNIQUE INDEX `uuid` (`uuid`);
ALTER TABLE `users` ADD INDEX `name_email_pwd` (`name`, `email`, `pwd`);

=====start===== 1 run 99998 lines, using 1.937110 s 2 run 99997 lines, using 2.644152 s 3 run 99995 lines, using 2.787159 s 4 run 99988 lines, using 2.875165 s 5 run 99984 lines, using 3.320189 s 6 run 99967 lines, using 3.676211 s 7 run 99969 lines, using 4.046231 s 8 run 99964 lines, using 4.304246 s 9 run 99965 lines,using 4.524259 s 10 run 99951 lines,using 4.865279 s total time 34.987001 s====end=====uuidのインデックスを削除し、実行時間は20 sになります.エンジンをInnoDBに交換すると、速度が遅くなり、特に複数のインデックスが含まれている場合は、速度がちょっと耐えられません.インデックスが少ないほど挿入が速くなります.MyISAMエンジンはInnoDBより速い.
PHPコード:
host};port={$this->post};dbname={$this->dbname};charset=UTF8;",
            $this->user,
            $this->pass,
            array(\PDO::ATTR_PERSISTENT => true, \PDO::MYSQL_ATTR_LOCAL_INFILE => true)
        ) or die('mysql connect fail');
        $db->query("SET NAMES utf8;");
        return $db;
    }

    private function loadFile($file)
    {
        $db = $this->pdo();
        $sql = 'LOAD DATA LOCAL INFILE "' . $file . '" IGNORE INTO TABLE users fields terminated by "," enclosed by "\"" lines terminated by "
"(`uuid`,`name`,`email`,`pwd`,`sex`,`status`,`created_at`,`remark`)'; return $db->exec($sql); } public function insertFromFile() { //You maybe need to set 'memory_limit' for your php. //ini_set('memory_limit', '256M'); set_time_limit(0); echo '=====start=====' . PHP_EOL; $start = microtime(true); for ($i = 1; $i <= $this->times; $i++) { $s1 = microtime(true); $file = $this->path . $i; $flag = true; if (!file_exists($file)) { $flag = $this->generateFile($file); } if (false !== $flag) { $count = $this->loadFile($file); $e1 = microtime(true); printf($i . ' run ' . $count . ' lines, using %f s' . PHP_EOL, $e1 - $s1); } } $end = microtime(true); printf('total time %f s' . PHP_EOL, $end - $start); echo '=====end=====' . PHP_EOL; for ($i = 1; $i <= $this->times; $i++) { //You can remove the temp file. //@unlink($this->path . $i); } } } $generator = new pdoTenMillion(); $generator->insertFromFile();

参照先:http://blog.csdn.net/zhengfeng2100/article/details/53487736 http://www.cnblogs.com/fanwencong/p/5765136.html $timeを100に変更し、1000万件のデータを挿入します.実は1000万本はidプライマリキーインデックスしか含まれていません.load挿入には80秒ぐらいかかります.4分もかかりません.