I know about mysql_num_rows ( resource $result )
but I have read somewhere that is it not 100% accurate. I have seen other alternatives that actually gets each row and run a counter but that sounds pretty inefficient. I am interested in seeing what others do to get an accurate and efficient count.
我知道mysql_num_rows(资源$结果),但我读过某个地方,它不是100%准确。我已经看到其他替代方案实际上获得每一行并运行一个计数器,但听起来效率很低。我有兴趣看到其他人做了什么来获得准确有效的计数。
3 个解决方案
#1
use mysql_num_rows()
when you've done a SELECT
or SHOW
query and mysql_affected_rows()
in case of INSERT, UPDATE, REPLACE or DELETE
queries.
both are accurate enough!!
在执行SELECT或SHOW查询时使用mysql_num_rows(),在INSERT,UPDATE,REPLACE或DELETE查询时使用mysql_affected_rows()。两者都足够准确!!
#2
mysql_num_rows()
is accurate, as long as you are not using mysql_unbuffered_query()
.
mysql_num_rows()是准确的,只要你没有使用mysql_unbuffered_query()。
If you are using mysql_query()
, mysql_num_rows()
is accurate.
如果您使用的是mysql_query(),则mysql_num_rows()是准确的。
If you are using mysql_unbuffered_query()
, mysql_num_rows()
will return the wrong result until all rows are retrieved.
如果您使用的是mysql_unbuffered_query(),则mysql_num_rows()将返回错误的结果,直到检索到所有行。
#3
It would help to know exactly what you're doing with the row count, but I'll assume it's for the very standard application of paging result sets.
这将有助于准确知道您正在对行计数做什么,但我会假设它是非常标准的分页结果集应用程序。
Issue two queries. The first is a COUNT query with all of the WHERE criteria of the second. The second is used to fetch the rows. It's either this or loading 1,000 rows into memory when you're only going to display 10 of them.
发出两个查询。第一个是COUNT查询,其中包含第二个的所有WHERE条件。第二个用于获取行。当你只打算显示其中的10行时,它就是这个或者在内存中加载1000行。
However, if you need to guarantee accuracy, you'll have to wrap the two queries in a read consistent transaction. Otherwise a query that happens to run between the count and data queries could invalidate the paging info.
但是,如果您需要保证准确性,则必须将两个查询包装在读取一致的事务中。否则,在计数和数据查询之间运行的查询可能会使分页信息无效。
#1
use mysql_num_rows()
when you've done a SELECT
or SHOW
query and mysql_affected_rows()
in case of INSERT, UPDATE, REPLACE or DELETE
queries.
both are accurate enough!!
在执行SELECT或SHOW查询时使用mysql_num_rows(),在INSERT,UPDATE,REPLACE或DELETE查询时使用mysql_affected_rows()。两者都足够准确!!
#2
mysql_num_rows()
is accurate, as long as you are not using mysql_unbuffered_query()
.
mysql_num_rows()是准确的,只要你没有使用mysql_unbuffered_query()。
If you are using mysql_query()
, mysql_num_rows()
is accurate.
如果您使用的是mysql_query(),则mysql_num_rows()是准确的。
If you are using mysql_unbuffered_query()
, mysql_num_rows()
will return the wrong result until all rows are retrieved.
如果您使用的是mysql_unbuffered_query(),则mysql_num_rows()将返回错误的结果,直到检索到所有行。
#3
It would help to know exactly what you're doing with the row count, but I'll assume it's for the very standard application of paging result sets.
这将有助于准确知道您正在对行计数做什么,但我会假设它是非常标准的分页结果集应用程序。
Issue two queries. The first is a COUNT query with all of the WHERE criteria of the second. The second is used to fetch the rows. It's either this or loading 1,000 rows into memory when you're only going to display 10 of them.
发出两个查询。第一个是COUNT查询,其中包含第二个的所有WHERE条件。第二个用于获取行。当你只打算显示其中的10行时,它就是这个或者在内存中加载1000行。
However, if you need to guarantee accuracy, you'll have to wrap the two queries in a read consistent transaction. Otherwise a query that happens to run between the count and data queries could invalidate the paging info.
但是,如果您需要保证准确性,则必须将两个查询包装在读取一致的事务中。否则,在计数和数据查询之间运行的查询可能会使分页信息无效。