gpt4 book ai didi

php pdo一次插入太多行的有效方法

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:10 25 4
gpt4 key购买 nike

<分区>

我正在以编程方式将行从 Excel 文件导入数据库。 excel 文件有 10 列 * 30000 行。我已将这些数据导入到 php 数组中,然后将其插入到数据库中。

上传文件后,需要 7-8 分钟才能将所有行插入数据库。我知道两种插入行的方法

第一种方法:

生成动态查询,例如,

INSERT INTO table_name (col1, col2,..) VALUES (row1, row1, ....), (row2, row2, ....), (row3, row3, ....), (row4, row4, ....), ...

并运行查询以插入所有行。

第二种方法:

$con->prepare("INSERT INTO table_name (col1, col2, ...) VALUES (:col1, :col2, ...)");
foreach ($rows as $row) { // Loop through all rows and insert them
$result->bindParam(':col1', $val1);
$result->bindParam(':col2', $val2);
$result->bindParam(':col3', $val3);
...
...
$result->execute();
}

第一种方法看起来困惑且效率低下,我正在使用第二种方法,但它每秒仅插入 500-700 行,插入所有行需要 7-8 分钟的总时间。

还有哪些方法比这些方法更高效、更快速?

编辑:不建议将 excel 文件直接导入 mysql。数据在插入数据库之前需要进行处理。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com