简体   繁体   English

mysql性能将所有结果输出到phparray或仅在while循环中使用

[英]mysql performance output all results to phparray or just use in while loop

I have a table which will add about 500,000 rows per year. 我有一张桌子,每年将增加约500,000行。

I have a query to select rows between value1 and value2, which will output between 100 and 4000 rows. 我有一个查询来选择value1和value2之间的行,这将输出100到4000行。 Each row has 5 columns, small data (id fields, and lat long). 每行有5列,小数据(id字段和lat long)。

I previously had this written using mysql, directly in the code where it was needed, and used a while loop to process and print each row returned by the query. 以前,我是使用mysql直接在需要的代码中编写的,并使用while循环来处理和打印查询返回的每一行。

I am in the process of changing from mysql to mysqli (using procedural if that matters) with prepared statements. 我正在使用准备好的语句从mysql更改为mysqli(如果需要的话,使用过程)。 During this "changeover", I'm neatening up the code making functions for the queries as they are repeated a lot, so I am including them in a functions.php file. 在此“转换”期间,由于要重复很多次查询,因此我将整理代码的查询功能,因此将它们包含在functions.php文件中。

Because of this, I am now not processing row by row from the mysql result directly in the code, instead I am putting all the rows from the query into an array, then returning from the function, and looping through one by one in the code where it is needed. 因此,我现在不直接在代码中逐行处理mysql结果,而是将查询中的所有行放入数组中,然后从函数返回,并在代码中逐一循环需要它的地方。

Bit of code for reference: 代码位供参考:

function...
...
        mysqli_stmt_bind_result($stmt,
        $id,
        $heading,
        $journey,
        $lat,
        $lon);

$map_array = array();
$n=0;
           while (mysqli_stmt_fetch($stmt)) {


               //output to array
               $map_array[$n]['id'] = $id;
               $map_array[$n]['heading'] = $heading;
               $map_array[$n]['journey'] = $journey;
               $map_array[$n]['lat'] = $lat;
               $map_array[$n]['lon'] = $lon;
$n=$n+1;
}
return $map_array;
}

And Then I would have to loop through the array and process it in the code. 然后,我将不得不遍历数组并在代码中对其进行处理。

In comparison to my old way: 与我以前的方式相比:

$lasthead ='0';
while ($row=mysql_fetch_array($result)){
$lat = $row['lat'];
$lon = $row['lon'];
$id = $row['id'];
$heading = $row['heading'];
$journey = $row['journey'];

$doprint='1';
if ($journey =='2' && $heading !==''){
    $maxvar = max($heading, $lasthead);
    $minvar = min($heading, $lasthead);
    $headdiff = $maxvar - $minvar;
    if ($headdiff < 1.5)  { $doprint = '0'; }  
}
if ($doprint =='1'){ print "new google.maps.LatLng($lat, $lon),\n";}

$doprint='1';
$lasthead = $heading;
$heading ='';
}

Question. 题。 Is loading all results into an array, then processing the array a bad idea performance wise? 将所有结果加载到数组中,然后处理该数组是否会影响性能? vs a while loop processing the mysql result by row. vs一会儿循环逐行处理mysql结果。

Thanks 谢谢

You don't gain anything by going through the records twice. 您两次浏览记录都不会获得任何收益。 Do all of your work in the mysql fetch loop. 在mysql fetch循环中完成所有工作。

The only times you wouldn't want to do that is if 您唯一不想这样做的时间是

  1. What you are doing in the loop is expensive (in terms of time) and you want to close the statement handle. 在循环中您正在做的事情很昂贵(就时间而言),并且您想关闭语句句柄。

  2. You have to walk through the data multiple times. 您必须多次遍历数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM