Reducing server load

14 posts by 2 authors in: Forums > CMS Builder
Last Post: December 19, 2013   (RSS)

Reducug server load

By Dave - December 8, 2013

Hi Jerry, 

From a quick glance at the code it looks like you're loading $accountsRecords twice, if you are you can remove one call and only do it once at the top of the page.

Next, if you need more speed or optimization than the built in CMSB function provide, the next step is usually to write custom MySQL code.  

Here's some code that loads uploads directly from the CMS 'uploads' table for files that are associated with the 'accounts' table and the the field 'larger_banners":

$uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'large_banners' ORDER BY RAND()");
showme($uploadRecords);
exit;

Try the above code, and then looping over the images with a foreach loop.  Note that the direct MySQL won't have all the same fields as what getRecords() returns, so sometimes you need to add some code.

Let me know how that works for you.

Dave Edis - Senior Developer
interactivetools.com

By gkornbluth - December 9, 2013 - edited: December 9, 2013

Thanks Dave,

Using the call below once at the top of he page certainly sped things up.

I hope I've implemented your suggestion correctly.

I moved the orderBy to the geRecords call to randomize the order that the records are pulled (since there's only one large banner in each record)

There are 2 banner rotators on the page, which was the reason for the second randomized getRecords call.

Is there any way to randomize the records again in the second iteration of the banner rotator slideshow code without a second getRecords call (which would slow down the page load again)?

Thanks,

Jerry Kornbluth

<?php  // load records from 'accounts'
  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
   'orderBy' => 'RAND()',
    $uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'large_banners' "),
  ));
   ?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - December 12, 2013

HI Jerry,

Is there any way to randomize the records again in the second iteration of the banner rotator slideshow code without a second getRecords call (which would slow down the page load again)?

Yes, is it possible to just display the first record from a randomized $uploadRecords, and then just the second one? eg: $uploadRecords[0] and $uploadRecords[1]?

Let me know if that would work.

Dave Edis - Senior Developer
interactivetools.com

Hi Dave,

Thanks for checking in on this.

Unless I'm not understanding you, I'm not looking to display only the first or the second record, but to randomize the same group of retrieved records twice.

Only one banner image can be uploaded per record and right now both banner rotators on the page begin with showing the same banner. 

I'd like each rotator to start with different records (banner images) even though they are all pulled from the same pool of records.

Hope that makes more sense.

Thanks

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - December 12, 2013

Hi Jerry, 

So a two banners from two different accounts? I might not be following correctly.

In any case, if the new code is quicker, you could try just including it twice, or if it returns a list of randomized banners, you could use the first 2 (since they would be different and randomized).  Although if you wanted to ensure the banners were from different user accounts (not two banners from the same user) you'd need to do a more complicated MySQL query that involved a GROUP BY option.  Basically any time you start needing to optimize the next step is open to go to straight MySQL code, because that gets everything out of the way that might hinder performance.

Let me know if any of those options will work (and include the code you're using if you need more help so I can take a look).

Dave Edis - Senior Developer
interactivetools.com

The banners are all from different users however, your original MySQL code (below) did a perfect job of pulling all of the uploads into (an array?).

I'm using that (array) to populate 2 banner rotators that appear on the same page, one at the top and one at the bottom.

Right now, because they are pulled from the same (array),  they appear in the identical order on both rotators.

What I'd like to do is to randomize the (array) before it's used for the second rotator so that the 2 rotators show banners in different orders.

I'd rather not do another MySQL query, because even with the optimized code, it's churning through a lot of records and there is a noticeable load time increase with a second query.

Thanks,

Jerry Kornbluth

<?php  // load records from 'accounts'
  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
   'orderBy' => 'RAND()',
    $uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'large_banners' "),
  ));
   ?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - December 13, 2013

Hi Jerry, 

Ok, well you can randomize an array with PHP's shuffle function, like this: <?php shuffle($yourArrayHere); ?>  Note that shuffle returns true or false so you can't call it in an foreach block, you need to call it on the line before that.

Also, for this code: 

<?php  // load records from 'accounts'
  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
   'orderBy' => 'RAND()',
    $uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'large_banners' "),
  ));
?>

I think you want either this:

<?php  // load records from 'accounts'
  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
   'orderBy' => 'RAND()',
  ));
?>

Or this:

<?php
   $uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'large_banners' ");
?>

Or maybe have them one after the other, but you don't need to put them together because they don't affect each other.  Just load the one you need for the variable you are using.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

Thank you again Dave,

It now works perfectly, thanks to your guidance.

Here’s the final code, in context with the rotator code I'm using for anyone else needing the same.

<?php // load uploads from accounts
   $uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'large_banners' ");
?>

<script>
        
        window.addEvent('domready', function() {
    // modified from http://joesong.com/2009/11/external-links-in-new-window-passive-and-with-mootools/
    // to avoid using the 'target' attribute, which is not part of xhtml 1.0 strict
    var currentDomain = window.location.host;
    $(document.body).addEvent('click', function(evt) {
    var target = $(evt.target);
    if (target.get('tag') !== 'a') {
    target = target.getParent();
    }
     
    if (target && target.get('tag') === 'a' && target.get('href').test('http') && !target.get('href').test(currentDomain)) {
    window.open(target.get('href'), '_blank');
    return false;
    }
    });
    });    
        window.addEvent('domready', function(){
        var data = { <?php $blank = "'" ?><?php $output = ''; 
 ?>

<?php shuffle ($uploadRecords) ?>
<?php foreach ($uploadRecords as $upload): ?>
 <?php $output .= "'". "/cmsAdmin/uploads/".$upload['urlPath']. "'". ": { caption: " ."'4'".  ",". "href:" . "'" . $upload['info3'] . "'" . " }" . ","; ?>

<?php endforeach ?>
<?php $output = rtrim($output,","); // remove trailing comma 
print $output; 
?>
};
new Slideshow('top', data, { captions: false, controller: false, thumbnails: false, overlap: false, delay: <?php echo $common_informationRecord['top_rotator_duration']?>, duration: <?php echo $common_informationRecord['cross_fade_duration']?>, height: 100, hu: '', width: 668 });
            
});
    </script>

<script>
        window.addEvent('domready', function(){
            var data2 = { <?php $blank = "'" ?><?php $output = '';
 ?>

<?php shuffle ($uploadRecords) ?>
<?php foreach ($uploadRecords as $upload): ?>
 <?php $output .= "'". "/cmsAdmin/uploads/".$upload['urlPath']. "'". ": { caption: " ."'4'".  ",". "href:" . "'" . $upload['info3'] . "'" . " }" . ","; ?>

<?php endforeach ?>
<?php $output = rtrim($output,","); // remove trailing comma
print $output;
?>
};
new Slideshow('bottom', data2, { captions: false, controller: false, thumbnails: false, overlap: false, delay: <?php echo $common_informationRecord['bottom_rotator_duration']?>, duration: <?php echo $common_informationRecord['cross_fade_duration']?>, height: 100, hu: '', width: 668 });
            
});
    </script>

Top rotator

 <div align="center" id="top" class="slideshow"></div>

Bottom Rotator

 <div align="center" id="bottom" class="slideshow"></div>

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - December 13, 2013

Great! Nice job, and thanks for posting back to let us know you got it working.

All the best!

Dave Edis - Senior Developer
interactivetools.com