MySQL Query Cache WhiteSpace and comments

March 20, 2008
Author
Peter Zaitsev
Share this Post:

Commenting on my previous post on MySQL Query Cache Gerry pokes me as I’m all wrong and both comments and whitespace are fixed in MySQL 5.0. This was not what I remember seeing in production so I decided to do some tests on the matter:

I did the test two ways. First using command line client and second using little PHP script which just does the same query. I did this as command line client is known to optimize queries sometimes by skipping “unnecessary” comments.

So here is the command line run:

Looking at these results you could judge as all problems are indeed fixed. You can have whitespace in the start and you can have leading comment and it all works. However the comment which is inside the query works interesting way – the queries with different comments are both treated as same query if only comment is different. However if you change whitespace a bit (see the last query has space after comment deleted) it causes query cache miss.

If we look at process list output we can see the comments are actually skipped:

This tells us we should not use MySQL Command Line Client for any tests involving comments

Repeating queries from PHP instead we can learn the following about MySQL 5.0 Query Cache:

Whitespace at the start of query does not block query from being cached

      1. . Moreover query with 2 spaces in front is considered same as query with 3 spaces in front

 

Comment at the start of the query does not block query from being cached.

      1. However queries with different comments are considered different queries (it is not stripped before hashing) – so you should not put things like current time in such a comment.

 

Comments inside the query also matter.

      1. Meaning if you place comments inside the query or in the end. Though this was always the case

 

So in the nutshell you should be more free now in regards of some SELECT queries not cached because of whitespace or comments in front of them. Though you still need to have queries exactly the same including comments to make them cached by query cache.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved