如何将这25个INSERT查询加入到一个?

时间:2015-03-21 14:03:31

标签: php mysql transactions sql-insert sql-optimization

我有一个基于网络的系统,可以读取一张excel文件(每张上传的图纸是6行-5列),如下所示:

enter image description here

下面是我正在使用的代码(为了演示目的,我为$ entry设置了静态值,但其实际代码将是例如$entry1=$objTpl->getActiveSheet()->getCell('A2')->getValue();     

$con=mysqli_connect("localhost","root","","view");

if(isset($_POST['submit']))
{
$entry1 = 1;
$entry2 = 2;
$entry3 = 3;
$entry4 = 4;
$entry5 = 5;
$entry6 = 1;
$entry7 = 2;
$entry8 = 0;
$entry9 = 4;
$entry10 = 0;
$entry11 = 0;
$entry12 = 2;
$entry13 = 3;
$entry14 = 4;
$entry15 = 0;
$entry16 = 1;
$entry17 = 2;
$entry18 = 0;
$entry19 = 0;
$entry20 = 5;
$entry21 = 1;
$entry22 = 0;
$entry23 = 3;
$entry24 = 0;
$entry25 = 5;

if($entry1 != 0 || !empty($entry1))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry1','$entry1')");
}
if($entry2 != 0 || !empty($entry2))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry2','$entry2')");
}
if($entry3 != 0 || !empty($entry3))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry3','$entry3')");
}
if($entry4 != 0 || !empty($entry4))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry4','$entry4')");
}
if($entry5 != 0 || !empty($entry5))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry5','$entry5')");
}
if($entry6 != 0 || !empty($entry6))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry6','$entry6')");
}
if($entry7 != 0 || !empty($entry7))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry7','$entry7')");
}
if($entry8 != 0 || !empty($entry8))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry8','$entry8')");
}
if($entry9 != 0 || !empty($entry9))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry9','$entry9')");
}
if($entry10 != 0 || !empty($entry10))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry10','$entry10')");
}
if($entry11 != 0 || !empty($entry11))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry11','$entry11')");
}
if($entry12 != 0 || !empty($entry12))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry12','$entry12')");
}
if($entry13 != 0 || !empty($entry13))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry13','$entry13')");
}
if($entry14 != 0 || !empty($entry14))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry14','$entry14')");
}
if($entry15 != 0 || !empty($entry15))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry15','$entry15')");
}
if($entry16 != 0 || !empty($entry16))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry16','$entry16')");
}
if($entry17 != 0 || !empty($entry17))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry17','$entry17')");
}
if($entry18 != 0 || !empty($entry18))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry18','$entry18')");
}
if($entry19 != 0 || !empty($entry19))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry19','$entry19')");
}
if($entry20 != 0 || !empty($entry20))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry20','$entry20')");
}
if($entry21 != 0 || !empty($entry21))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry21','$entry21')");
}
if($entry22 != 0 || !empty($entry22))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry22','$entry22')");
}
if($entry23 != 0 || !empty($entry23))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry23','$entry23')");
}
if($entry24 != 0 || !empty($entry24))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry24','$entry24')");
}
if($entry25 != 0 || !empty($entry25))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry25','$entry25')");
}
}
?>

我有几个问题:

  1. 是否可以将所有这些陈述合并为一个?
  2. 在执行一个大查询而不是多个查询时,是否有任何重大的性能变化?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用单个SQL语句执行此操作。它会像这样工作。您可以根据需要将多组(val,val,val)串在一起。 (整个查询的字符串长度有限制,但在MySQL中它非常大。)

INSERT INTO viewer(base_id,name,value) VALUES
      ('1','entry1','$entry1'),
      ('1','entry2','$entry2'),
      ('1','entry3','$entry3')

如果您要将内容插入到InnoDB表中,那么当您的连接处于自动提交模式时,可以通过这样做获得相当大的性能提升。这是因为您将插入中的所有行一起提交,并且commit会产生开销。

修改 在php中,如果你有一个数组$ values,其中每个元素都包含一个像(1,'val','val')这样的文本字符串,你可以使用这段代码创建你的查询字符串。

$sqlstart = 'INSERT INTO viewer(base_id,name,value) VALUES ';
$sql = sqlstart . implode(',' , $values);

这会将逗号放在之间,但不会放在$ values数组的元素之后。 implode() is made for this kind of purpose

因此,您的代码可能如下所示:

$values = array();  /* make an empty array */
if ($entry1 != 0 || !empty($entry1)) {
   $values[] = "('1','entry1','$entry1')";  /* append to array */
}
if ($entry2 != 0 || !empty($entry2)) {
   $values[] = "('1','entry2','$entry2')";
}
/* etc etc for the rest of your $entry values */
$sqlstart = 'INSERT INTO viewer(base_id,name,value) VALUES ';
$sql = sqlstart . implode(',' , $values);

(请注意SQL插入漏洞利用)。