为什么 mysql 结果集的循环很慢? (每个周期 1.4 毫秒)
$res 包含大约 488k 行,整个循环需要 61 秒!每个周期超过 1.25 毫秒!是什么占用了这么长的时间?
while($row = $res->fetch_assoc())
{
$clist[$row['upload_id']][$row['dialcode_id']][$row['carrier_id']]['std'] = $row['cost_std'];
$clist[$row['upload_id']][$row['dialcode_id']][$row['carrier_id']]['ecn'] = $row['cost_ecn'];
$clist[$row['upload_id']][$row['dialcode_id']][$row['carrier_id']]['wnd'] = $row['cost_wnd'];
$dialcode_destination[$row['upload_id']][$row['carrier_id']][$row['dialcode_id']]['other_destination'] = $row['destination_id'];
$dialcode_destination[$row['upload_id']][$row['carrier_id']][$row['dialcode_id']]['carrier_destination'] = $row['carrier_destination_id'];
}
现在,结果集为 10 行,数组更小,性能提高了 30 倍(0.041 毫秒),虽然不是最快的,但更好。
while($row = $res->fetch_assoc())
{
$customer[$row['id']]['name'] = $row['name'];
$customer[$row['id']]['code'] = $row['customer'];
}
The $res contains around 488k rows the whole loop takes 61s! that's over 1.25ms per cycle! What is taking all that time?
while($row = $res->fetch_assoc())
{
$clist[$row['upload_id']][$row['dialcode_id']][$row['carrier_id']]['std'] = $row['cost_std'];
$clist[$row['upload_id']][$row['dialcode_id']][$row['carrier_id']]['ecn'] = $row['cost_ecn'];
$clist[$row['upload_id']][$row['dialcode_id']][$row['carrier_id']]['wnd'] = $row['cost_wnd'];
$dialcode_destination[$row['upload_id']][$row['carrier_id']][$row['dialcode_id']]['other_destination'] = $row['destination_id'];
$dialcode_destination[$row['upload_id']][$row['carrier_id']][$row['dialcode_id']]['carrier_destination'] = $row['carrier_destination_id'];
}
Now resultset of 10 rows, smaller arrays and performance 30 times higher (0.041ms) not the fastest still but better.
while($row = $res->fetch_assoc())
{
$customer[$row['id']]['name'] = $row['name'];
$customer[$row['id']]['code'] = $row['customer'];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大数组需要更多时间来分配内存和处理。
这就是为什么我们总是要求数据库完成所有工作并返回 10 行作为最终结果。
Big arrays take more time to allocate memory and handle.
that's why we always ask a database to do all the job and return 10 rows as a final result.
488k是很多行,意味着很多数据。数组中的项目越多,需要分配的内存就越多,查找元素所需的时间就越长。
当您执行相同的代码近 50 万次时,优化数组访问是值得的:
这样您只需执行一次数组查找,而不是重复执行,并且可能会显着减少运行时间。尽管如此,将这么多东西放在一个数组中并不会很快。
从长远来看,最好的选择是将数据留在数据库中,只在需要时抓取数据(或者让数据库为您做繁重的工作,如果您想求和/平均/其他) 。
488k is a lot of rows, which means a lot of data. The more items you stick in an array, the more memory has to be allocated and the longer it takes to lookup elements.
As you're executing the same code almost half a million times, it'd be worth optimising the array accesses:
That way you're only doing the array lookups once instead of repeatedly, and will probably significantly reduce the runtime. Sticking that many things in an array is not going to be fast regardless, though.
In the long run, your best bet is to leave the data in the database and only grab bits as and when you need them (or have the database do the heavy lifting for you, if you're trying to sum/average/whatever).
我怀疑一直花费的时间是对 4 维数组的持续访问,其中一些(或全部?)维度是从字符串字段中键入的,这些值本身必须从
$row
...我建议您认真重新考虑:
I suspect that what's taking all the time is the continual access to 4-dimensional arrays, where some (or all?) of the dimensions are being keyed from string fields, those values themselves having to be extracted from
$row
...I would suggest you seriously reconsider:
这个怎么样:
What about this: