2014-10-20 53 views
0

我有一张名为discounts的表格。将数据插入到MySQL/MariaDB中的最快和最简单的方法是什么?

CREATE TABLE discounts (
id INT NOT NULL AUTO_INCREMENT, 
title VARCHAR(255) NOT NULL, 
expired_date DATE NOT NULL, 
amount DECIMAL(10,2) NULL, 
PRIMARY KEY (id) 
); 

我需要从后端插入超过一百万。如果我通过插入查询运行,需要很长时间才能在我的MariaDB服务器中执行。

有没有最简单的方法来执行从文本文件或SQL文件的导入数据的查询?

+1

你应该真的看看文档:http://dev.mysql.com/doc/refman/5.0/en/loading-tables.html – Esse 2014-10-20 12:31:41

+0

谢谢。我得到了结果 – Phoenix 2014-10-20 12:36:09

回答

1

首先打开记事本,在其中输入以下内容。

/* give tab inbetween the columns */ 
Spring Break 2014 20140101 20 
Back to School 20140901 25 
Summer 2014 20140825 10 

并将文件保存在C盘中作为.sql或.txt文件格式。

打开MariaDB的/ MySQL和粘贴以下代码

use test; /* here test is the database name */ 
/* i save the file path in c:/ drive */ 
LOAD DATA LOCAL INFILE '/sample.sql' /* here sample if the file name */ 
INTO TABLE discounts COLUMNS TERMINATED BY '\t' /* \t it will truncate the tab */ 
(title, expired_date, amount); 

执行查询。

它将执行无限数量的记录。

+0

如果您要在MySQL/MariaDB服务器上运行上述查询,则必须提及** LOAD DATA INFILE'/var/lib/mysql/sample.sql'.**代码。 – Phoenix 2014-10-21 04:28:10

相关问题