Jump to content


Photo

how to count items without getting the results.


  • Please log in to reply
4 replies to this topic

#1 joshuag

joshuag

    Sr. Member

  • Members
  • PipPipPipPip
  • 112 posts
  • 72

  • LocationCalgary AB, & Mérida, Yucatan

Posted 29 June 2012 - 02:05 PM

Hi,

I am wondering, is there a way to count items in the DB without returning the results? I have a site that is going to have 10,00 pages and I want to count them all.

Using $pages->find(selector)->count; returns the results and I guess this is the same as count($pages->find()) just counting how many items are in the returned wire array. Not what I want. I want to count items without the overhead of getting all the data.

Thanks,

#2 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 905

  • LocationVihti, Finland

Posted 29 June 2012 - 02:13 PM

Actually I think there is very little overhead of doing that, since PW doesn't load the data until it is used (or if you have autojoin set on your fields). And if you don't have to loop all those pages it will be breeze.

You can also do this:

$pages->find(selector, limit=2)->getTotal()

But I guess it does pretty much the same under the hood (not sure though) - removes the limit and counts it.

#3 joshuag

joshuag

    Sr. Member

  • Members
  • PipPipPipPip
  • 112 posts
  • 72

  • LocationCalgary AB, & Mérida, Yucatan

Posted 29 June 2012 - 02:29 PM

Awesome. Ok, I didn't know that. I thought PW got everything when I asked for it. This is great news. Thanks.

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 29 June 2012 - 02:30 PM

We also have an API function for this:

$total = $pages->count("selector");

It basically works the same as @apeisa's example and is fast/low overhead.

#5 joshuag

joshuag

    Sr. Member

  • Members
  • PipPipPipPip
  • 112 posts
  • 72

  • LocationCalgary AB, & Mérida, Yucatan

Posted 29 June 2012 - 04:19 PM

ahh, great, thanks Ryan... strange that I didn't know about that one.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users