国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MySQL快速插入一億測(cè)試數(shù)據(jù)

瀏覽:97日期:2023-10-01 14:02:01
目錄1、建表1.1 建立測(cè)試表 t_user1.2 創(chuàng)建臨時(shí)表2、生成數(shù)據(jù)2.1 用 python生成 【一億】 記錄的數(shù)據(jù)文件(這個(gè)確實(shí)稍微花點(diǎn)時(shí)間)2.2 將生成的文件導(dǎo)入到臨時(shí)表tmp_table中3、以臨時(shí)表為基礎(chǔ)數(shù)據(jù),插入數(shù)據(jù)到t_user中4、參考1、建表1.1 建立測(cè)試表 t_user

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;

MySQL快速插入一億測(cè)試數(shù)據(jù)

2、生成數(shù)據(jù)2.1 用 python生成 【一億】 記錄的數(shù)據(jù)文件(這個(gè)確實(shí)稍微花點(diǎn)時(shí)間)

python -c 'for i in range(1, 1+100000000): print(i)' > base.txt

MySQL快速插入一億測(cè)試數(shù)據(jù)

MySQL快速插入一億測(cè)試數(shù)據(jù)

MySQL快速插入一億測(cè)試數(shù)據(jù)

2.2 將生成的文件導(dǎo)入到臨時(shí)表tmp_table中

找到對(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快速插入一億測(cè)試數(shù)據(jù)

重啟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è)。

MySQL快速插入一億測(cè)試數(shù)據(jù)

3、以臨時(shí)表為基礎(chǔ)數(shù)據(jù),插入數(shù)據(jù)到t_user中

一億數(shù)據(jù)需要:快半個(gè)小時(shí)了。。。(或許直接在命令行下運(yùn)行更快點(diǎn)...)

MySQL快速插入一億測(cè)試數(shù)據(jù)

更新創(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)!

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 花莲县| 会理县| 东乡族自治县| 手游| 堆龙德庆县| 迁西县| 怀宁县| 汉源县| 汉寿县| 武汉市| 乐昌市| 那坡县| 昭平县| 黄大仙区| 永顺县| 吕梁市| 景洪市| 栖霞市| 桑日县| 清水河县| 青神县| 额敏县| 东辽县| 芜湖市| 茶陵县| 怀化市| 江孜县| 夏河县| 柳州市| 竹溪县| 神池县| 英吉沙县| 江口县| 交城县| 敦煌市| 历史| 哈尔滨市| 巴里| 西安市| 阜南县| 富顺县|