Sorting Query Information through SQL or Perl?

Posted on 2006-04-25
Last Modified: 2010-05-18
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.
Question by:sganjam
    LVL 5

    Accepted Solution

    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.
    LVL 4

    Assisted Solution

    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 ;)
    LVL 51

    Assisted Solution

    > .. 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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now