简体   繁体   English

MySQL:仅获取结果集计数

[英]MySQL: Get only count of result set

I am using MVC with PHP/MySQL. 我在PHP / MySQL中使用MVC。

Suppose I am using 10 functions with different queries for fetching details from the database. 假设我正在使用具有不同查询的10个函数从数据库中获取详细信息。
But at other times I may want to get only the count of the result that will be returned by the query. 但是在其他时候,我可能只想获取查询返回的结果计数。
What is the standard way to handle such situation. 处理这种情况的标准方法是什么?
Should I write 10 more functions which duplicate almost whole query and return only the count. 我是否应该再编写10个函数,这些函数几乎重复整个查询并仅返回计数。
Or 要么
Should I always return the count also with the result set 我是否应该始终将计数与结果集一起返回
Or 要么
I can pass a flag to indicate that the function should return count only, and then based on the flag I will dynamically generate the (select part of) query. 我可以传递一个标志来指示该函数应仅返回计数,然后基于该标志,我将动态生成查询的(选择部分)。
Or 要么
Is there a better way? 有没有更好的办法?

Now that mysql supports sub-queries, you can get counts for any query using: 现在,mysql支持子查询,您可以使用以下命令获取任何查询的计数:

  $count_query="SELECT COUNT(*) FROM ($query)";

How hard was that? 那有多难?

However this approach always means that you are running two queries instead of just the one (I'm not sure if MySQL would necessarily be able to use a cached result set for the count - try it out and see). 但是,这种方法始终意味着您正在运行两个查询,而不是仅运行一个查询(我不确定MySQL是否一定能够使用缓存的结果集进行计数-试用一下,看看)。

If you've already fetched the entire result set it'll probably be faster counting the rows in PHP than issuing another query. 如果您已经获取了整个结果集,则对PHP中的行进行计数可能比发出另一个查询更快。

There are 2 functions in MySQL which would return the number of matched rows prior to application of a limit statement: MySQL中有2个函数,它们将在应用limit语句之前返回匹配的行数:

SQL_CALC_FOUND_ROWS and FOUND_ROWS() SQL_CALC_FOUND_ROWS和FOUND_ROWS()

see 看到

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

C. C。

If you want only number of rows matched certain criteria, you shouldn't use a count of the result, but another query that select only count(*) instead. 如果仅希望满足特定条件的行数,则不应使用结果计数,而应使用仅选择count(*)的另一个查询。

If you need both data and it's count, why don't you just use count() on the resulting array? 如果既需要数据又需要计数,为什么不只在结果数组上使用count()?

another way is to use some class that can return both data and it;s count, but not different classes for the each 10 queries but one single database access class. 另一种方法是使用一些class ,可以返回数据和它; S计数,而不是不同类的各10个查询,但一个单一的数据库访问类。

I'd go with the flag idea. 我会用旗帜的主意。

Writing 10 more functions and copy/pasting code does not help readability at all. 再编写10个功能和复制/粘贴代码根本无法提高可读性。 If you always also return the count, that means that whenever you're only interested in the count, the database still has to generate and transmit the full result set which might be grossly inefficient. 如果您还总是返回计数,则意味着无论何时您仅对计数感兴趣,数据库仍必须生成并传输可能效率极低的完整结果集。

With the flag, you'd have something like 带有标志,您将拥有类似

function getData($countOnly=false) {
    // ...generate FROM and WHERE clause
    if ($countOnly) {
        $query = 'SELECT COUNT(*) '.$query;
    } else {
        $query = 'SELECT field1, field2, ...'.$query.' ORDER BY ...';
    }
    ...
}

I would generally try to have as much code as possible shared between methods. 我通常会尝试在方法之间共享尽可能多的代码。 A possibility would be to : 一种可能是:

  • have one select() and one count() functions 有一个select()和一个count()函数
    • each one building the specific part of the query 每一个都建立查询的特定部分
  • and one buildFromAndWhere() function to build the parts of the query that are common. 和一个buildFromAndWhere()函数来构建查询的常见部分。
    • and have select() and count() use that one 并让select()count()使用那个


Written in pseudo-code, it could look a bit like this : 用伪代码编写,看起来可能像这样:

function select($params) {
    return "select * "
            . from()
            . where($params)
            . "limit 0, 10";
}

function count() {
    return "count(*) as nbr "
            . from()
            . where();
}

function from() {
    return "from table1 inner join table1 on ... ";
}

function where($params) {
    // Use $params to build the where clause
    return "where X=Y and Z=blah";
}

This way, you have as much common code as possible in the from() and where() functions -- considering the hard part of the queries is often there, it's for the best. 这样,您就可以在from()where()函数中使用尽可能多的通用代码-考虑到查询中经常遇到的困难部分是最好的。


I prefer having two separate functions to select and count ; 我更喜欢选择和计数两个独立的函数; I think it make code easier to read and understand. 我认为这使代码更易于阅读和理解。

I don't like the ideas of one method returning two distinct data (list of results and the total count) ; 我不喜欢一种方法返回两个不同数据(结果列表和总计数)的想法; and i don't really like the idea of passing a flag either : looking at the function's call, you'll never know what that parameter means. 我也不喜欢传递标志的想法:查看函数的调用,您将永远不知道该参数的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM