mysql query cache

3816 ワード

1.works with Transation
http://www.mysqlperformanceblog.com/2008/01/29/how-mysql-query-cache-works-with-transactions/ 書き記す
The result set can be retrieved from query cache(for statemens both inside and out side of transactions)until there is a statement inside trans modifies the table.
As son as table is modified in tranaction it becommes uncachable by query cache until that transcomitted.
Not only query cache can't be used inside the same transaction which modified data but also in other concurrent transactions which dot eve the changes done yet.
With current approach Innodb can probably do something as simple as marking table"
uncachable"if it has any uncomitted changed which would tare care about all compicated aspects of change visibility in different modes.
 
transaction 1 :
start transaction;
select count(*) from parent;  //  result: 1   QCache_hits : 0
select count(*) from parent;  //  result: 1   QCache_hits : 1

transaction 2 :
start transaction;
select count(*) from parent;  //  result: 1   QCache_hits : 2
insert into parent(name) values('new');
select count(*) from parent;  //  result: 2   QCache_hits : 2

transaction 1 :
select count(*) from parent;  //  result: 1   QCache_hits : 2




transaction 2 :
commit;

transaction 1 :
select count(*) from parent;  //  result: 1   QCache_hits : 2

commit;
select count(*) from parent;  //  result: 2   QCache_hits : 2



select count(*) from parent;  //  result: 2   QCache_hits : 3

transaction 2 :
select count(*) from parent;  //  result: 2   QCache_hits : 4
 
 the experiment rellt perfectly explins  the mechans of mysql cache query with tractions.
 
 
2.White Space and comments
http://www.mysqlperformanceblog.com/2008/03/20/mysql-query-cache-whitespace-and-comments/ 書き記す
You can have whites pace 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.However if you
change whitespace a bit(see the last query has space after compment deleted)it causes query cache miss.
Whitosepace at the start of query does not block query from being cached.Moreover query with 2 spaces in front is consided same as query 3 spaces in front
Commentt at the start of the query does not block query from being cached.However queries with different comment ares are consided different queries(it is not stripped before hashing)-so you shound not put put prothings lint correct。
Comments inside the query also mater.Meaning if you place comments inside the query or in the end.Though this ways the case
 
select count(*) from parent;  //  QCache_hits : 0
select count(*) from parent;  //  QCache_hits : 1
/*comment*/ select count(*) from parent;  //  QCache_hits : 2             //  whitespace and comment at the start of query 
/*newcomment*/ select count(*) from parent;  //  QCache_hits : 3      //   does not block query from
/*comment*/select count(*) from parent;  //  QCache_hits : 4              //   being cached.

select /*comment*/ count(*) from parent;  //  QCache_hits : 4             // comments inside
select /*new comment*/ count(*) from parent;  //  QCache_hits : 5

select /*comment*/count(*) from parent;  //  QCache_hits : 5         // no whitespace between count(*) and comments.
select /*newcomment*/count(*) from parent;  //  QCache_hits : 6

select /*comment*/   count(*) from parent;  //  QCache_hits : 6      // one more whitespace before count(*)