subscribe

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.

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

Web mentions

Comments

  • Basi

    Basi

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

    Topbit

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

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

    Evert

    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
  • Shantanu Oak

    Shantanu Oak

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

    Evert

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

    Jillian Sands

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

    Evert

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