whats quicker - doing a count or adding a new attribute to user

By Mark

Hi

I've come across lots of situations where I have to do a count of the number of objects with certain metadata. For example, to find out the number of ratings a user has, i do this:

                $NameValuePairs[] = array('name' => 'rated_guid', 'value' => $user->guid);
                $NameValuePairs[] = array('name' => 'pushed', 'value' => 1);
               
                $options = array();
                $options = array (
                    'type'=> 'object',
                    'subtype' => 'rating',
                    'metadata_name_value_pairs' => $NameValuePairs
                );
                $ratings = elgg_get_entities_from_metadata($options);

               $num_ratings = sizeof($ratings);   

Is it more efficient to introduce a new attribute on the user, like $user->num_ratings = $num_ratings rather than doing a count? Or is the performance roughly the same? There are plenty other occassions when I have to do a count on objects so would like to know the correct approach and apply it across the project...

Thanks

 

 

Performance and Scalability

Performance and Scalability

If you've got a need for speed, this group is for you.