LOAD DATA INFILE
方法。这需要39秒。我希望将时间减少到2秒以内。MySQL表仅包含3列ID,TiME,状态。代码如下:
$servername = "localhost";
$username = "root";
$password = "signion";
$db = "In_band";
$conn = mysqli_connect($servername, $username, $password,$db);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$x = "(ID,TIME";
for ($num=1;$num<=1024;$num++){
$x = $x.",@status".$num;
}
$x = $x.")";
$sql = "CREATE TABLE IF NOT EXISTS table_7(ID INT,TIME TEXT,status TEXT)";
mysqli_query($conn, $sql);
$sql = "LOAD DATA INFILE '/var/lib/files/data86400_1024.csv' REPLACE INTO TABLE table_7 FILEDS TERMINATED BY ',' ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 1 LINES ".$x." set status=@status1024";
$query = mysqli_query($conn, $sql);
mysqli_close($conn);
完整的查询如下:
LOAD DATA INFILE '/var/lib/files/data86400_1024.csv' REPLACE INTO TABLE table_7 FILEDS TERMINATED BY ',' ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 1 LINES (ID,TIME,@status1,@status2,@status3,.......@status1024) set status=@status1024";
在上面的代码中,我将csv文件的最后一列(status1024
)导入到mysql表status
列中。
我也使用了fgetcsv
方法。但是花了将近1分钟。
如何在不到2秒的时间内优化将特定列导入mysql表?