Quick MySQL tip: add comments

MySQL allows comments to be added with the standard /* */ syntax. If your application is getting more complex, it can become more difficult to figure out where certain queries originate from.

By simply adding a comment in front of your query, it will be very easy to spot the origin of some of your queries.

  1. /* recent blogposts */ SELECT id, title, time FROM blogposts ORDER BY time DESC limit 10

The comment will show up as the first thing in SHOW [FULL] PROCESSLIST, MySQL administrator and your log/slow-log files.


7 Responses to Quick MySQL tip: add comments

  1. 709 Basi 2008-09-09 9:53 pm

    Yes, I've been using this trick too and it's very useful.

    I added this comments as an extra parameter into my PHP db class, it automatically adds the comment as in your example.

    Regards,

    Basi.

  2. 710 Topbit 2008-09-11 5:43 pm

    If the first six characters aren't 'SELECT', it will not be put into the query cache.

    "SELECT /* ... */ " will work fine though.

  3. 711 Evert 2008-09-11 6:10 pm

    Thanks Topbit, thats a really important thing to know..

    Luckily this is not the case for MySQL 5.0 upwards

    From the manual:
    "Before MySQL 5.0, a query that began with a leading comment could be cached, but could not be fetched from the cache. This problem is fixed in MySQL 5.0."

    http://dev.mysql.com/doc/refman/5.0/en/query-cache-how.html

  4. 708 Shantanu Oak 2008-09-23 10:01 am

    Another simple way is to use different case. for e.g. SeLeCt or selecT
    Slow-query log as well as processlist shows the query as it is and helps me to debug.

  5. 707 Evert 2008-09-30 4:24 pm

    meh, I fail to see to benefit over using comments (which are more or less self-documenting)

  6. 706 Jillian Sands 2008-09-30 5:57 pm

    Interesting suggestion to use different case. I can see new uses for that tip, which used to be a disadvantage, but this turns it around to something helpful.

  7. 705 Evert 2008-09-30 6:24 pm

    meh, I fail to see to benefit over using comments (which are more or less self-documenting)



About

My name is Evert, and I've been writing semi-regularly on this blog since 2006.

I'm currently available for contract work.

more info.

Subscribe

Dropbox

Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.