Mysql or through PHP?
I have a design that calls for grouping articles by their date. The date will be a heading and all articles from the latest 10 will be grouped under each heading. The date can be anything and/or highly sporadic, the articles must be in descending order, etc.
I figure i would have to
-
retrieve the last 10 articles and put them in a multi-dimensional array like so:
$allretrieved = array ( array ( id => a, date => b, title => c, body => d, image => e, etc.), array ( id => a, date => b, title => c, body => d, image => e, etc.), etc. ); -
then group the array by using a method I don’t fully understand ( extract into another array? Would someone explain this function to me?)
$date_groups[] = array(); $group = ''; foreach( $date_groups as $key=>$val ) { if($key == 'date'){ $group = $val; } $comment_groups[$group][] = $val; } -
and then sort the groups?
Or should I sort beforehand?
is doing it at the MySql level faster? more efficient?
EDIT: Best Method I have found for this:
Basic PHP MySQL array grouping question
At some point where your dataset isn’t too big, sorting with mysql should be faster if indexes are correctly added since
defaultmysql indexes are b-tree. In your case, bringing the data and looping over it you will have aO(n)thus the sorting will take more as the data gets bigger.I recommend you to take a look to your database schema, use
explainto see how indexes are being used in the query you are running and benchmark. AGAIN: this will depend of your data.now, for a sake of dreaming: Once your data is big enough to make the sorting too slow in mysql then you should switch something where you can sort in parallel using map-reduce or something like that, but given the nature of your post I think you are safe for now.