Sis srl

Ciarpame {.com}

Useful stuffs for the masses. Useless things for few.

June 28th, 2008

true mysql “natural order by” trick

by Giovanni Savastano

when you develop some kind of web app (or sites containing tabular data too) and you need to order “naturally” mysql results, problems starts to come out! googling a lot I’ve found a simple trick that I’m here to share.

  1. go to: Drupal ViewVC site
  2. point to / contributions / modules / natsort / natsort.install.mysql.
  3. download the latest version of the script
  4. login to your ssh enviroment
  5. type: mysql – u [username] -p [databasename] < natsort.install.mysql
  6. insert your corresponding password
  7. look at your db, now you have 3 new stored procedures, 2 stored functions and 2 new tables:
    1. Stored Procedures:
      • natsort_benchmark
      • natsort_finalize
      • natsort_initialize
    • Stored Functions:
      • natsort_canon
      • natsort_canon_save
    • Tables:
      • natsort_lookup
      • natsort_lookup_pending
  8. you can delete the natsort_initialize() stored procedure. it is used only by drupal, so we don’t need this
  9. go to Drupal ViewVC site
  10. Point to / contributions / modules / natsort / README.txt
  11. follow the simple pass to start using this good workaround.

Don’t forget to call the natsort_finalize() procedure at the end of any query you do. This will speed-up all you next queries.

Tags: · , ,
Categories: Programming · Tricks

2 Comments

  • 1 Jun 29, 2008 at 12:10
    Daniele Veratti

    Good job, man! The only problem i can see about this trick is that you can’t easily apply it to a huge, already written web site (you know what website I mean). You have to rewrite all your SELECT queries in order to use it. Think about it for the next web site I’m going to develop (if any) ;-)

    PS: Sìì Gruoss!

  • 2 Jun 29, 2008 at 13:22
    Giovanni

    you can, instead of rewrite all of your queries, modify all views applying natsort to made some rapid improvement to your web app.

Leave a Comment