mysqli_store_result() vs. mysqli_use_result()

自古美人都是妖i 提交于 2019-11-27 19:39:42

mysqli::store_result() will fetch the whole resultset from the MySQL server while mysqli::use_result() will fetch the rows one by one.

This is also mentioned in the mysqli::use_result docs you linked to:

The mysqli_use_result() function does not transfer the entire result set from the database and hence cannot be used functions such as mysqli_data_seek() to move to a particular row within the set. To use this functionality, the result set must be stored using mysqli_store_result(). One should not use mysqli_use_result() if a lot of processing on the client side is performed, since this will tie up the server and prevent other threads from updating any tables from which the data is being fetched.

You can usually always use mysqli::store_result() unless you have a good reason for not reading all rows from the server at once.

use_result returns an unbuffered result.

store_result returns a buffered result.

Christopher

I think this elaboration might help people getting here from languages that support retrieving either via arrays or enumerators. Because it is just that difference.

For .NET, it would be the same difference as GetDirectories (store_results) vs EnumerateDirectories (use_results) (and the File/DbResult/CSV parsing/XML Parsing equivalents).

The enumerator variant uses less (PHP) client memory and might fetch the next row asynchronously in the background while the current row is processing, making it both more memory and processing time effective on the client side. At the cost of taking up (SQL) Server resources for longer (as the result set has not been fully transmitted).

In turn the get/array way will retrieve the data in one blocking operation, taking up more client side memory for freeing DB memory earlier.

Personally I would default for the non-Enumerator Way, unless you actually got (client) memory issues.

In .NET you have to use the Enumerator way, if you run into the 2/3 GiB limit for x32 processes. Particular EnumerateLines for files is important here. With DB results it should not happen to begin with. A 2 GiB DB result is either way under-filtered (do more filtering in Query) or contains a lot of Blobs (which you should load piecemeal or even let the browser retrieve via a http handler if possible).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!