如何在 PHP 中使用 MySQL Found_Rows()?
由于性能问题,我尝试避免执行 Count() 。 (即 SELECT COUNT() FROM Users)
如果我在 phpMyAdmin 中运行以下命令,那就可以了:
SELECT SQL_CALC_FOUND_ROWS * FROM Users;
SELECT FOUND_ROWS();
它将返回行数。即用户数。
但是,如果我在 PHP 中运行,我就不能这样做:
$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users;
SELECT FOUND_ROWS(); ';
mysql_query($query);
似乎 PHP 不喜欢传入两个查询。那么,我该怎么做呢?
I try to avoid doing Count() because of performance issue. (i.e. SELECT COUNT() FROM Users)
If I run the followings in phpMyAdmin, it is ok:
SELECT SQL_CALC_FOUND_ROWS * FROM Users;
SELECT FOUND_ROWS();
It will return # of rows. i.e. # of Users.
However, if I run in in PHP, I cannot do this:
$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users;
SELECT FOUND_ROWS(); ';
mysql_query($query);
It seems like PHP doesn't like to have two queries passing in. So, how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
SQL_CALC_FOUND_ROWS
仅当您使用LIMIT
子句时才有用,但仍想知道在没有LIMIT
的情况下会找到多少行。想想这是如何工作的:
您强制数据库检索/解析表中的所有数据,然后将其丢弃。即使您不打算检索任何行,数据库服务器仍会开始从磁盘提取实际数据(假设您需要该数据)。
用人类的话来说,你买了超级杂货店的全部东西,但扔掉了除了收银台边的一包口香糖之外的所有东西。
然而,这样做:
让数据库引擎知道,虽然您想知道有多少行,但您不能不关心实际数据。在大多数智能 DBMS 上,引擎可以从表的元数据中检索此计数,或者简单地运行表的主键索引,而无需接触磁盘上的行数据。
SQL_CALC_FOUND_ROWS
is only useful if you're using aLIMIT
clause, but still want to know how many rows would've been found without theLIMIT
.Think of how this works:
You're forcing the database to retrieve/parse ALL the data in the table, and then you throw it away. Even if you aren't going to retrieve any of the rows, the DB server will still start pulling actual data from the disk on the assumption that you will want that data.
In human terms, you bought the entire contents of the super grocery store, but threw away everything except the pack of gum from the stand by the cashier.
Whereas, doing:
lets the DB engine know that while you want to know how many rows there are, you couldn't care less about the actual data. On most any intelligent DBMS, the engine can retrieve this count from the table's metadata, or a simple run through the table's primary key index, without ever touching the on-disk row data.
它的两个查询:
PHP 每个 mysql_query 调用只能发出一个查询
Its two queries:
PHP can only issue a single query per mysql_query call
SQL_CALC_FOUND_ROWS 的性能比 COUNT() 更好,这是一个常见的误解。请参阅 Percona 人员的比较: http:// /www.mysqlperformanceblog.com/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/
回答您的问题:每个 mysql_query 调用只允许一个查询,如手册中所述: < code>mysql_query() 发送唯一查询(不支持多个查询)
使用
ext/mysqli
作为 MySQL 扩展时支持多个查询:http://www.php.net/manual/en/mysqli.multi-query.php
It's a common misconception, that SQL_CALC_FOUND_ROWS performs better than COUNT(). See this comparison from Percona guys: http://www.mysqlperformanceblog.com/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/
To answer you question: Only one query is allowed per one mysql_query call, as described in manual:
mysql_query() sends a unique query (multiple queries are not supported)
Multiple queries are supported when using
ext/mysqli
as your MySQL extension:http://www.php.net/manual/en/mysqli.multi-query.php
只有这段代码适用于我,所以我想与您分享。
Only this code works for me so i want to share it for you.
使用“union”和空列:
Use 'union' and empty columns:
这是一个简单的方法&对我有用:
This is an easy way & works for me :
您真的认为从表中选择所有行比计算它们更快吗?
Myisam 在表的元数据中存储大量记录,因此 SELECT COUNT(*) FROM table 不必访问数据。
Do you really think that selecting ALL rows from tables is faster than counting them?
Myisam stores a number of records in table's metadata, so SELECT COUNT(*) FROM table don't have to access data.