dongle7882 2012-03-19 16:04
浏览 25
已采纳

mysql查询速度的差异

Suppose i have 1kk records in my database.

Now i need to select some data, and also i need to know how many fields did i select, so my question is:

Is it better to run one query to count data like this:

SELECT COUNT("id") from table where something = 'something'

And after that run one more querio for selection like this:

SELECT 'some_field' from table where something = 'something';

Or Maybe it's better to just select data and then just count it with php like:

count($rows);

Or maybe there is even better ways to do it, for example do it all in one query?

  • 写回答

3条回答 默认 最新

  • duanpang5583 2012-03-19 16:09
    关注

    Reading between the lines, I think what your are probably after is SQL_CALC_FOUND_ROWS. This allows you to select part of a result set (using a LIMIT clause), and still calculate the total number of matching rows in a single operation. You still use two queries, but the actual search operation in the data only happens once:

    // First get the results you want...
    $result = mysql_query("
      SELECT SQL_CALC_FOUND_ROWS
      FROM `table`
      WHERE `something` = 'something'
      LIMIT 0, 10
    ");
    
    // ...now get the total number of results
    $numRows = mysql_query("
      SELECT FOUND_ROWS()
    ");
    $numRows = mysql_fetch_row($numRows);
    $numRows = $numRows[0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?