如何在 PHP 中使用 MySQL Found_Rows()?

发布于 2024-09-13 09:29:43 字数 421 浏览 6 评论 0原文

由于性能问题,我尝试避免执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蓝礼 2024-09-20 09:29:43

SQL_CALC_FOUND_ROWS 仅当您使用 LIMIT 子句时才有用,但仍想知道在没有 LIMIT 的情况下会找到多少行。

想想这是如何工作的:

SELECT SQL_CALC_FOUND_ROWS * FROM Users;

您强制数据库检索/解析表中的所有数据,然后将其丢弃。即使您不打算检索任何行,数据库服务器仍会开始从磁盘提取实际数据(假设您需要该数据)。

用人类的话来说,你买了超级杂货店的全部东西,但扔掉了除了收银台边的一包口香糖之外的所有东西。

然而,这样做:

SELECT count(*) FROM users;

让数据库引擎知道,虽然您想知道有多少行,但您不能不关心实际数据。在大多数智能 DBMS 上,引擎可以从表的元数据中检索此计数,或者简单地运行表的主键索引,而无需接触磁盘上的行数据。

SQL_CALC_FOUND_ROWS is only useful if you're using a LIMIT clause, but still want to know how many rows would've been found without the LIMIT.

Think of how this works:

SELECT SQL_CALC_FOUND_ROWS * FROM Users;

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:

SELECT count(*) FROM users;

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.

给妤﹃绝世温柔 2024-09-20 09:29:43

它的两个查询:

$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users';
mysql_query($query);
$query = 'SELECT FOUND_ROWS()';
mysql_query($query);

PHP 每个 mysql_query 调用只能发出一个查询

Its two queries:

$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users';
mysql_query($query);
$query = 'SELECT FOUND_ROWS()';
mysql_query($query);

PHP can only issue a single query per mysql_query call

放低过去 2024-09-20 09:29:43

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

雨轻弹 2024-09-20 09:29:43

只有这段代码适用于我,所以我想与您分享。

$Result=mysqli_query($i_link,"SELECT SQL_CALC_FOUND_ROWS  id From users LIMIT 10"); 
$NORResult=mysqli_query($i_link,"Select FOUND_ROWS()"); 
$NORRow=mysqli_fetch_array($NORResult); 
$NOR=$NORRow["FOUND_ROWS()"]; 
echo $NOR;

Only this code works for me so i want to share it for you.

$Result=mysqli_query($i_link,"SELECT SQL_CALC_FOUND_ROWS  id From users LIMIT 10"); 
$NORResult=mysqli_query($i_link,"Select FOUND_ROWS()"); 
$NORRow=mysqli_fetch_array($NORResult); 
$NOR=$NORRow["FOUND_ROWS()"]; 
echo $NOR;
请止步禁区 2024-09-20 09:29:43

使用“union”和空列:

$sql="(select sql_calc_found_rows tb.*, tb1.title
      from orders tb
           left join goods tb1 on tb.goods_id=tb1.id
      where {$where}
      order by created desc
      limit {$offset}, {$page_size})
      union
      (select found_rows(), '', '', '', '', '', '', '', '', '')
     ";
$rs=$db->query($sql)->result_array();
$total=array_pop($rs);
$total=$total['id'];

Use 'union' and empty columns:

$sql="(select sql_calc_found_rows tb.*, tb1.title
      from orders tb
           left join goods tb1 on tb.goods_id=tb1.id
      where {$where}
      order by created desc
      limit {$offset}, {$page_size})
      union
      (select found_rows(), '', '', '', '', '', '', '', '', '')
     ";
$rs=$db->query($sql)->result_array();
$total=array_pop($rs);
$total=$total['id'];
国际总奸 2024-09-20 09:29:43

这是一个简单的方法&对我有用:

$query = "
SELECT SQL_CALC_FOUND_ROWS * 
FROM tb1
LIMIT 5";

$result = mysqli_query($link, $query);

$query = "SELECT FOUND_ROWS() AS count";
$result2 = mysqli_query($link, $query);

$row = mysqli_fetch_array($result2);

echo $row['count'];

This is an easy way & works for me :

$query = "
SELECT SQL_CALC_FOUND_ROWS * 
FROM tb1
LIMIT 5";

$result = mysqli_query($link, $query);

$query = "SELECT FOUND_ROWS() AS count";
$result2 = mysqli_query($link, $query);

$row = mysqli_fetch_array($result2);

echo $row['count'];
半边脸i 2024-09-20 09:29:43

您真的认为从表中选择所有行比计算它们更快吗?
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文