Welcome Guest, Not a member yet? Register   Sign In
Select joins and pagination question. How to count all results?
#1

[eluser]hyperfire[/eluser]
Hi there!

I have this doubt about counting the search results for ages, maybe its just better ask Smile

Every time I have a page on CI, that uses pagination, as I use a lot of joins and parameters on my queries, I need to request the same query to the db twice, the first one to get the full result count, and the second with the limit and offset params so CI navigation works.

How do you guys do it in order to reduce the requests to db?

Here's a sample code:

Code:
// Query for all results (pagination total records)
        $this->mdl_grouped_areas->events_fetch_from_cat = true;
        $this->mdl_grouped_areas->events_activeornot= 1;                             // Get only active items
        $this->mdl_grouped_areas->get_agenda();                            // execute()
        $content['total_records'] = $this->mdl_grouped_areas->events_num_records;
    
        // Query paged
        $this->mdl_grouped_areas->events_fetch_from_cat = true;
        $this->mdl_grouped_areas->events_activeornot= 1;                             // Get only active items
        $this->mdl_grouped_areas->events_pag_amount = $limit;
        $this->mdl_grouped_areas->events_pag_offset = $offset;
        $this->mdl_grouped_areas->events_sortby = 'events.dt_post DESC';
        $content['pageJuice'] = $this->mdl_grouped_areas->get_agenda(); // execute()

        // Pagination
        $this->config_pag['base_url']         = base_url() . '/' . $this->area_c . '/featured/';
        $this->config_pag['total_rows']     = $content['total_records'];
        $this->config_pag['uri_segment']    = 3;
        $this->pagination->initialize($this->config_pag);

In the example above, after execute the code for the first time, without offset and limit params, the model gets a variable with the full result count for that search.
On the second query, results are fetched with limits then pagination is initialized before calling the views.
Is there some clever way to get the same results? Am I doing something wrong?

Thanks!!!
#2

[eluser]Jondolar[/eluser]
I do the same thing. I query the database twice. I have very complex queries with sub-queries and I limit the number of returned records based on the page. The only other way I have seen this is where you get the total records on the first page and then save that to a cookie or session or add it to the pagination URLS as a variable. On subsequent pages, use the stored total record count. I didn't like that way so I do two queries.
#3

[eluser]hyperfire[/eluser]
The session or cookie approach is somehow different. Thanks for sharing it.
Anyone else?
#4

[eluser]hyperfire[/eluser]
UP! This REALLY matters to me sorry. Wanna know what ppl are using to count results. Come on devs! Share your experiences! CI still open source as far as I know. Smile
#5

[eluser]wiredesignz[/eluser]
Do some research on the MYSQL operator "SQL_CALC_FOUND_ROWS" and its partner function "FOUND_ROWS()".
Quote:...SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again...
#6

[eluser]hyperfire[/eluser]
I have never used those tags, but I know that they should be used in conjunction to fetch results using the found_rows().
Anyway, EVERYONE says that it makes your query run really slower than running the same query twice, so that's why I have never gone into it.
If someone have successfully used the SQL_CALC_FOUND_ROWS to calc rows on a portal and have experiences on benchmarking it against double queries, I would be glad to know about it.
Thanks wired!
#7

[eluser]hyperfire[/eluser]
There's also the possibility to run SQL_CALC_FOUND_ROWS, store it in session/cookie and then avoid the subsequent double queries and SQL_CALC_FOUND_ROWS. Perhaps its how it should be used.
Anyway, I can do the same "keep total count in session" using the double query method, so the questions are:

Witch methods are available to store the total result count?
Using SQL_CALC_FOUND_ROWS is faster than running the same query twice to store the results?

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB