如果一个 SQL 语句看起来很乱,那么我将其分解并使用更多资源。对于需要压缩每一滴性能的大型网络应用程序来说,这是一个糟糕的解决方案。
但是,大多数情况下,对于要求不高的情况,一个漂亮的视觉逻辑过程就可以了。
我会选择我的信息,并使用高级语言的内部循环将结果放入插入语句中。我没有受到足够的惩罚来制作更多的插入物。在许多共享主机上,您可以在一段时间内进行多少次查询是有限制的,因此您可能需要构建更少但更大的插入语句。
为此,我可能会使用 PHP 来处理我的数据和 SQL,我相信以下内容可以适应您想要使用的任何语言。
<!php
$placeid=001
$sql1="SELECT column,othercolumn,maybeasterisk FROM table1 WHERE PlaceID=$placeid";
$sql2="INSERT into table2 (column,othercolumn,thirdcolumn,whymore,so-on) VALUES ('$data1a','$data1b','$data1c','$data1d','$data1e')";
$sql3="INSERT into table2 (column,othercolumn,thirdcolumn,whymore,so-on) VALUES $bigstring";
$counter=0;
$result1=mysql_query($sql1);
while ($resultrow1=mysql_fetch_array($result1)) {
$data1a=$resultrow1['1'];
$data1b=$resultrow1['2'];
$data1c=$resultrow1['3'];
$data1d=$resultrow1['whymore'];
$data1e=$resultrow1['so-on'];
if ($abillioninsertsareokay='true') {
mysql_query($sql2);
}
else {
//a billion inserts isn't okay
//build an array that we can use in a separate loop
for ($i=0, $i<=5, $i++) {
$sql3arraydata['$counter']['$i']
$counter++
}
}
$counter=0;
if ($abillioninsertsareokay!='true') {
foreach $sql3arraydata['$counter'] {
if ($counter>0) {
$bigstring.=', ';
}
$bigstring.="('$sql3arraydata['$counter'][1]','$sql3arraydata['$counter'][2]','$sql3arraydata['$counter'][3]','$sql3arraydata['$counter'][4]','$sql3arraydata['$counter'][5]')";
}
mysql_query($sql3); //one huge insert statement
}
?>
It may be the wrong way to look at things, but sometimes you just have to crank out mediocre code that works to get the job done. Yes, that would be great if you had all the time in the world to make it super awesome, but if this is the kind of question you are asking-- probably not a mission critical system for a mass amount of people.