I am fetching some comments from the database in DESC from 0,15 – 15,15 – 30,15 and so on and i want to show them in reverse so i use array_reverse() function.
Whats fastest? Using array_reverse or change the SQL query to reverse it
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using
ORDER BY field DESCis generally going to be faster and easier to read (and slightly lighter on the memory consumption of the script), but if you’ve already written this it’s not so significant that I’d go back to re-do it.There are some cases, however, where you’ll actually want to stick with a post-query array reversal. For instance where you’re ordering by an index:
It may be much faster to use either
ASCorDESC, so you’d just want to run the faster order and then reverse it afterwards: see MySQL ORDER BY DESC is fast but ASC is very slowAlso, in cases where the mysql query becomes much more complex as a result of ordering, it may be faster to reverse the array. For instance, if you want only the last three results from a query that returns five results but you want them ordered desc, you’d have to run a sub-query, like:
In this scenario, it may be a lot easier (and perhaps faster) to just run:
and the reverse the results once they are returned.