Jump to content

Getting random pages very slow


gebeer
 Share

Recommended Posts

Hello,

Getting random pages with

$oldPosts = wire('pages')->find('template=post, shown=1, limit=100')->findRandom(16);

works fairly good. About 500ms

But without the limit=100 it takes more than 6 seconds.

There are only 60 pages with template post but it will be thousands when the app is live.

Is there a more efficient way of getting random pages?

Link to comment
Share on other sites

Thank you for the suggestion.

With something like

$oldPosts = wire('pages')->find('template=post, shown=1, limit=16, sort=random')

would PW get the first 16 pages it finds and then sort them randomly or would it go through all post pages and get 16 randomly selected pages?

I'd need the latter.

Link to comment
Share on other sites

Hello,

Getting random pages with

$oldPosts = wire('pages')->find('template=post, shown=1, limit=100')->findRandom(16);

works fairly good. About 500ms

But without the limit=100 it takes more than 6 seconds.

There are only 60 pages with template post but it will be thousands when the app is live.

Is there a more efficient way of getting random pages?

If there are only 60 pages i find it strange that it would take more than 6 seconds to get a couple of random pages. It's also funny that your limit 100 is larger than the actual number of pages, but it is twelve times faster (500ms versus 6 seconds) when you put the limit the selector. I'm not sure what is going on here.

Apart from that, as LostKobrakai said, when getting random pages from a large number of pages the sort=random is preferable. However, when you would get into a really big number of pages even that would probably become slow.

(i've done some simple tests in the past: https://processwire.com/talk/topic/8626-multiple-random-ads/?p=83345 )

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...