开发者

MySQL: Get only count of result set

开发者 https://www.devze.com 2022-12-23 22:52 出处:网络
I am using MVC with PHP/MySQL. Suppose I am using 10 functions with different queries for fetching details from the database.

I am using MVC with PHP/MySQL.

Suppose I am using 10 functions with different queries for fetching details from the database.

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 dup开发者_运维问答licate almost whole query and return only the count.

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:

  $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).

If you've already fetched the entire result set it'll probably be faster counting the rows in PHP than issuing another query.

There are 2 functions in MySQL which would return the number of matched rows prior to application of a limit statement:

SQL_CALC_FOUND_ROWS and FOUND_ROWS()

see

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

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.

If you need both data and it's count, why don't you just use count() on the resulting array?

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.


I'd go with the flag idea.

Writing 10 more functions and copy/pasting code does not help readability at all. 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
    • each one building the specific part of the query
  • and one buildFromAndWhere() function to build the parts of the query that are common.
    • and have select() and count() use that one


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消