MySQL快速插入一億測(cè)試數(shù)據(jù)
CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `c_user_id` varchar(36) NOT NULL DEFAULT ’’ COMMENT ’用戶Id’, `c_name` varchar(22) NOT NULL DEFAULT ’’ COMMENT ’用戶名’, `c_province_id` int(11) NOT NULL COMMENT ’省份Id’, `c_city_id` int(11) NOT NULL COMMENT ’城市Id’, `create_time` datetime NOT NULL COMMENT ’創(chuàng)建時(shí)間’, PRIMARY KEY (`id`), KEY `idx_user_id` (`c_user_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;1.2 創(chuàng)建臨時(shí)表
CREATE TABLE `tmp_table` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
python -c 'for i in range(1, 1+100000000): print(i)' > base.txt
找到對(duì)應(yīng)的數(shù)據(jù)庫(kù)
Type ’help;’ or ’h’ for help. Type ’c’ to clear the current input statement. mysql> use test;Database changedmysql> show tables;+----------------+| Tables_in_test |+----------------+| student|| t_user || tmp_table |+----------------+3 rows in set (0.00 sec)
執(zhí)行導(dǎo)入命令
mysql> load data infile ’E:/base.txt’ replace into table tmp_table;ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statementmysql>
導(dǎo)入數(shù)據(jù)時(shí)有可能會(huì)報(bào)錯(cuò),原因是mysql默認(rèn)沒(méi)有開(kāi)secure_file_priv( 這個(gè)參數(shù)用來(lái)限制數(shù)據(jù)導(dǎo)入和導(dǎo)出操作的效果,例如執(zhí)行LOAD DATA、SELECT … INTO OUTFILE語(yǔ)句和LOAD_FILE()函數(shù)。這些操作需要用戶具有FILE權(quán)限。 )
解決辦法:在mysql的配置文件中(my.ini 或者 my.conf)中添加 secure_file_priv = 文件所在的路徑 , 然后重啟mysql 解決。添加自己文件放置的路徑即可。
可以用 show variables like ’%secure%’; 先看一下配置:
mysql> show variables like ’%secure%’;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| require_secure_transport | OFF || secure_auth | ON || secure_file_priv | NULL |+--------------------------+-------+3 rows in set, 1 warning (0.00 sec)
說(shuō)明:
secure_file_prive=null 限制mysqld 不允許導(dǎo)入導(dǎo)出secure_file_priv=/var/lib/mysql-files/ 限制mysqld的導(dǎo)入導(dǎo)出只能發(fā)生在/var/lib/mysql-files/目錄下secure_file_priv=’ ’ 不對(duì)mysqld的導(dǎo)入導(dǎo)出做限制
注意:配置要添加到 [mysqld] 節(jié)點(diǎn)下,至于路徑加不加引號(hào),你可以試試:
重啟MySQL,先查看配置:
mysql> use test;Database changedmysql> show variables like ’%secure%’;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| require_secure_transport | OFF || secure_auth | ON || secure_file_priv | E: |+--------------------------+-------+3 rows in set, 1 warning (0.00 sec)
再重新導(dǎo)入:
mysql> load data infile ’E:/base.txt’ replace into table tmp_table;Query OK, 100000000 rows affected (3 min 53.42 sec)Records: 100000000 Deleted: 0 Skipped: 0 Warnings: 0 mysql>
億級(jí)數(shù)據(jù),233.42s,看一下別人的數(shù)據(jù),差不多就是這個(gè)。
一億數(shù)據(jù)需要:快半個(gè)小時(shí)了。。。(或許直接在命令行下運(yùn)行更快點(diǎn)...)
更新創(chuàng)建時(shí)間字段讓插入的數(shù)據(jù)的創(chuàng)建時(shí)間更加隨機(jī):
mysql> UPDATE t_user SET create_time=date_add(create_time, interval FLOOR(1 + (RAND() * 7)) year);Query OK, 100000000 rows affected (7 min 24.17 sec)Rows matched: 100000000 Changed: 100000000 Warnings: 0 mysql> UPDATE t_user SET create_time=date_add(create_time, interval FLOOR(1 + (RAND() * 7)) year);Query OK, 100000000 rows affected (8 min 2.49 sec)Rows matched: 100000000 Changed: 100000000 Warnings: 0
到此,一億數(shù)據(jù)插入結(jié)束。
4、參考MySQL如何快速的創(chuàng)建千萬(wàn)級(jí)測(cè)試數(shù)據(jù)
The MySQL server is running with the --secure-file-priv option
到此這篇關(guān)于MySQL快速插入一億測(cè)試數(shù)據(jù)的文章就介紹到這了,更多相關(guān)MySQL 插入一億數(shù)據(jù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 在redhat 9 上安裝oracle 9.2.0.4 時(shí),DBCA 出錯(cuò)的解決辦法2. mysql數(shù)據(jù)庫(kù)中最常用的時(shí)間轉(zhuǎn)換函數(shù)的用法4. mysql like語(yǔ)句問(wèn)題5. 快速刪除ORACLE重復(fù)記錄6. Access數(shù)據(jù)庫(kù)安全的幾個(gè)問(wèn)題7. 導(dǎo)出錯(cuò)誤編碼的mysql數(shù)據(jù)庫(kù)8. oracle觸發(fā)器介紹9. 數(shù)據(jù)庫(kù)Oracle9i的企業(yè)管理器簡(jiǎn)介10. MySQL基本調(diào)度策略淺析
