Forum
Randomising content - using RAND adding a lot to load time
Hi,
I'm using Perch Content Custom with a sort order of RAND to randomise the URL on a CTA button. Both the list of URLs and the CTA button itself has its own Region.
<a href="<?php perch_content_custom('CTA Single URL',array(
'template' => 'CTAs-urls.html',
'sort' => 'urls',
'count' => 1,
'sort-order' => 'RAND'
));
?>">
<?php perch_content('CTA Panel Single'); ?>
</a>
Using RAND is casing the page load to go from 167ms (when using ASC) to 1.28 seconds, which isn't acceptable to the client who is very hot on his SEO and page speed.
I couldn't find an alternative way of randomising only part of a template which is why I split it out into two.
This maybe a shot in the dark but is there a better way of achieving this?
Randomising is pretty inefficient due to MySQL limitations.
There could be a better way. How many items are in the region?
There are 7 items (URLs) in that region
Ok, I would do something like:
Thanks Drew you're a star!
Out of interest is there a reason you asked how many items were in that region, could there be too many for this approach?
Yes, you wouldn't want to pull hundreds or thousands of items into PHP memory, that would perform worse than the MySQL query.
Hi Drew,
I've just noticed this sometimes returns nothing, I've checked to make sure there isn't a blank item in the region it could be using and there isn't. Any thoughts?
Many thanks.
If you do the following, what do you get?
I get
Which looks correct
Does your code include the
-1
as per above? I did forget that and edit it in a few seconds after I originally posted the code.That was it, I must have got to it before you edited it. Thank you!