Frank Vèssia Posted January 13, 2015 Share Posted January 13, 2015 I'm wondering what's the best practice (in terms of performances) for obtaining total values. For example in my site I show to my users some stats based on their activities, like uploaded photos, total friends etc...I usually get this totals using a simple $pages->find("...limit=2")->getTotal() Could be more effective instead having a specific field just for the total, increasing its value on each action ? Of course with this solution I have to update this value on each user action...so I don't know if it's worth it... Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 13, 2015 Share Posted January 13, 2015 getTotal() is the standard api way to do it. I'm not firm with the database related issues, but if you're really concerned with speed issues, you could use a small autoload module, which counts the pages on pagesave of relevant pages and saves it to a field. This is only a few lines long and you don't have to worry about all the different things users can do on the frontend. 1 Link to comment Share on other sites More sharing options...
Jan Romero Posted January 13, 2015 Share Posted January 13, 2015 If you find() a limited number of pages anyway, it probably won’t get any more performant than getTotal(), at least without venturing into hacky territory or cache-like approaches like LostKobrakai’s suggestion. In this case, the PageArray returned from find() already has the total count ready for you, because it uses MySQL’s SQL_CALC_FOUND_ROWS by default! In fact, you can influence this by passing an options array to find() that recognizes the following keys: /*@param array $options - findOne: boolean - apply optimizations for finding a single page and include pages with 'hidden' status (default: false) - getTotal: boolean - whether to set returning PageArray's "total" property (default: true except when findOne=true) - loadPages: boolean - whether to populate the returned PageArray with found pages (default: true). The only reason why you'd want to change this to false would be if you only needed the count details from the PageArray: getTotal(), getStart(), getLimit, etc. This is intended as an optimization for Pages::count(). - caller: string - optional name of calling function, for debugging purposes, i.e. pages.count*/ As you can see, if you do a find(), you get the “numTotal” property for free, and if you just want the number of pages without fetching the actual page objects, $pages->count() will do an SQL COUNT with little overhead. If you want to squeeze performance out of your site, this is probably one of the less worthwhile places to start. edit: by the way, you can see the numTotal property of your PageArrays by var_dump()ing them. It’s one of the first properties it lists. 4 Link to comment Share on other sites More sharing options...
Frank Vèssia Posted January 13, 2015 Author Share Posted January 13, 2015 Thanks guys Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now