• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Sorting Query Information through SQL or Perl?

When Querying an mySQL database, what is more optimal? to sort using SQL Sort and getting back the results that way, or sorting using regular perl sort, by putting it in a hash. I am just wondering which would be faster and reduce server load. I have read conflicting opinions on this.
3 Solutions
Well first off, using the sort method in the MySQL server is WAY faster, so much faster the two can almost not even be compared.  Anyone who states otherwise must have an agenda or bias because there is no truth to it whatsoever.

Second, using a hash to sort is not very efficient and really an improper use of a hash table.  The very nature of a hash table is order inspecific.
when in doubt, benchmark.

use Benchmark qw(cmpthese);

cmpthese(10000, {
"MySQL sort" => sub {sql_sort();}
"perl sort" => sub {perl_sort();}
sub perl_sort {
sub sql_sort {

don't underestimate perl's sort, it pretty fast and written in C, but i guess indeed Probably the MySQL Server will be _way_ faster, bu hey, you never know ;)
> .. what is more optimal?
I agree with the others that SQL sort is faster, but I also guess that it depends on your SQL query and the available indexes in your database. Ther might be combinations wher perl outperforms SQL.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now