我应该限速还是减少我的数据库查询? [英] Should I rate-limit or reduce my database queries?
问题描述
我正在创建一个PHP脚本,将一些数据从文本文件导入MySQL数据库。这些文本文件相当大,一个平均文件将有10,000行,其中每一个对应于我想要的数据库中的一个新的项目。 (我不会经常导入文件)
I'm creating a PHP script that imports some data from text files into a MySQL database. These text files are pretty large, an average file will have 10,000 lines in it each of which corresponds to a new item I want in my database. (I won't be importing files very often)
我担心从文件中读取一行,然后执行INSERT查询,连续10,000次可能会导致一些问题。有没有更好的方法,我做这个?我应该对所有10,000个值执行一个INSERT查询吗?
I'm worried that reading a line from the file, and then doing a INSERT query, 10,000 times in a row might cause some issues. Is there a better way for me to do this? Should I perform one INSERT query with all 10,000 values? Or would that be just as bad?
也许我可以达到一个中等程度,并一次执行10或100个条目。真的,我的问题是,我不知道什么是好的做法。也许在一排10000个查询是好的,我只是担心没有。
Maybe I can reach a medium, and perform something like 10 or 100 entries at once. Really my problem is that I don't know what is good practice. Maybe 10,000 queries in a row is fine and I'm just worrying for nothing.
任何建议?
推荐答案
yes it is
<?php
$lines = file('file.txt');
$count = count($lines);
$i = 0;
$query = "INSERT INTO table VALUES ";
foreach($lines as $line){
$i++;
if ($count == $i) {
$query .= "('".$line."')";
}
else{
$query .= "('".$line."'),";
}
}
echo $query;
http://sandbox.phpcode.eu/g/5ade4.php
这将会进行一个单一查询,这是多个比一行一查询风格更快!
this will make one single query, which is multiple faster than one-line-one-query style!
这篇关于我应该限速还是减少我的数据库查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!