More efficient way to get total rows?

0

I need to find the total rows for a query, and at the moment I'm doing this:

        $Hotels = new Pod('hotels');
        $Hotels->findRecords('name ASC',1,'location.id = '.$RecordLocations->get_field('id').' AND t.publish = 1');
        $totalHotels = $Hotels->getTotalRows(); 

Is there a more efficient way to do this? The query gets called 50+ times in a single page load and it's causing the page to be very laggy.

asked Jul 6 '10 at 11:18

scottybowl

50

edited Jul 6 '10 at 11:20

add comment
enter at least 15 characters

6 Answers

2

You probably don't need to be generating this sidebar from a query every time you load the page. You could have a static file that displays here and then gets included every time the page renders. This would cut way down on your server time.

I'd say you want to write a post-save helper that gets called if a change is made to your HOTELS pod and writes a static file that contains the HTML code for your sidebar, basically replacing all the ECHO statements with FWRITE.

Your other option would be to bring your whole HOTELS pod into an array, then loop through the array to make the counts. It will still bog down as your site gains popularity, but it might be enough to get you by for a while...

I see with this, you've also got calls to another pod opened as $RecordLocation, making it that much less efficient.

answered Jul 7 '10 at 2:00

chris.pilko

889

add comment
enter at least 15 characters
0

Why are you running this command 50 times in one page, you only need it once .. or is there some special reason you are doing this for ?

After you've done this, you simply do something like this:

<pre>

if( $totalHotels>0 ) { while ( $Hotels->fetchRecord() ) {

    $hotel_id        = $Hotels->get_field('id');
    $hotel_name      = $Hotels->get_field('name');
    $hotel_bio       = $Hotels->get_field('description');

    $hotel_photo     = $Hotels->get_field('photo');
    $hotel_photo     = $hotel_photo[0]['guid'];
?>

<div class="hotel">
    <h3><?php echo $hotel_name; ?></h3>
    <p><?php echo $hotel_bio; ?></p>
    <img src="<?php echo $hotel_photo; ?>" />
</div>

<?php
} // endwhile

} // endif

</pre>and your hotels will be all displayed..

answered Jul 6 '10 at 2:13

tomhermans

36

add comment
enter at least 15 characters
0

I need to get the total number of hotels for each given area and show it to the right of the navigation item (see image below) but doing it the way I've done it seems very laggy

alt text

answered Jul 6 '10 at 4:41

scottybowl

50

add comment
enter at least 15 characters
0

I already cache all my PODs output, but the cache gets cleared fairly regularly (once per day) so a fair number of visitors are going to experience the lag (the cache gets generated on the fly via a page request as there are thousands of combinations of data that can be outputted on the site).

Unfortunately, due to PODs not allowing queries such as location.region.country.id I can't get rid of the extra PODs call for $RecordLocation.

I guess that only leaves me with the array option, will see if that speeds things up.

answered Jul 7 '10 at 9:18

scottybowl

50

add comment
enter at least 15 characters
0

The array method loads the page faster, will go with that fo rnow, cheers

answered Jul 7 '10 at 12:19

scottybowl

50

add comment
enter at least 15 characters
-1

that's pretty lame, but what about putting something like $somevar=$somevar+1; (or $somevar++, if i get it right) in the while cycle?

answered Jul 6 '10 at 5:07

Fike

208

edited Jul 6 '10 at 5:07

add comment
enter at least 15 characters