You are here: Re: [PHP] Re: Pagination Optimization « PHP « IT news, forums, messages
Re: [PHP] Re: Pagination Optimization

Posted by Bruno B B Magalhγes on 01/08/05 16:43

Thanks for your help, in fact helped a lot... Now my function only
performs 2 queries, here it is:

=======================================
function fetch_paginated($query='',$page=1,$itens=20)
{
$this->query($query);
$total_rows = $this->num_rows();

if($total_rows > $itens)
{
$this->total_pages = ceil($total_rows/$itens);
$this->pages_before = ceil($page - 1);
$this->pages_after = ceil($this->total_pages - $page);

$this->query($query.' LIMIT '.(($page*$itens)-$itens).','.$itens);

while($this->fetch_array())
{
$results[] = $this->row;
}
}
elseif($total_rows > 0)
{
while($this->fetch_array())
{
$results[] = $this->row;
}

$this->total_pages = '1';
$this->pages_before = '0';
$this->pages_after = '0';
}
else
{
return null;
}

return $results;
}
=======================================


Regards,
Bruno B B MagalhΓ£es

On Jan 7, 2005, at 10:09 PM, M. Sokolewicz wrote:
> first of all, you're running 4 queries here. 4 queries is a lot!
> Especially when you don't need more than 2 ;)
> the problem here is that your queries are pretty "unknown" to this
> function. Although it does a nice result for that unknowing, there's a
> few minor things that make it faster.
>
> First of all, would be using less queries.
> What I usually do is issue a query like this:
> "SELECT count(some_unique_col) WHERE
> (that_where_clause_youre_using_in_the_select_query)"
> then, we do some math.
>
> $pages_before = $page-1;
> $rows_before = $pages_before*$itens;
>
> $rows_after = $total_number_of_rows-($page*$itens);
> $pages_after = ceil($rows_after/20);
>
> Then do the actual selecting of the rows using the limit.
>
> The thing that makes it slow in your example is the fact that 4 times
> you're selecting ALL data from the relevant rows, and buffer it. You
> buffer it, but don't use any of it, except for the number of rows.
> Mysql does a far quicker job at this than PHP would, so use mysql. :)
> Then, you're using 3 queries to determine the rows around the page;
> even though, with a bit of simple math, you can calculate it. And
> trust me on this, simple math is faster ;)
>
> anyway, hope that helped.

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация