QueryCache lock model in MySQL

Source: Internet
Author: User
Some people ask whether the QueryCache (QC) Lock in MySQL is a global lock or a table lock. Here is a brief description. 1. Basic Concept of QC this is a memory structure implemented on the MySQL layer (non-engine layer). The basic rule is to cache query results that meet certain conditions in the memory, if the same query is executed for the second time and the cache is not invalid, you can directly return

Some people ask whether the QueryCache (QC) Lock in MySQL is a global lock or a table lock. Here is a brief description. 1. Basic Concept of QC this is a memory structure implemented on the MySQL layer (non-engine layer). The basic rule is to cache query results that meet certain conditions in the memory, if the same query is executed for the second time and the cache is not invalid, you can directly return

Some people ask whether the locks of QueryCache (QC) in MySQL are "global locks" or "Table locks ". Here is a brief description.

1. Basic QC concepts

This is a memory structure implemented in the MySQL layer (non-engine layer). The basic rule is to cache query results that meet certain conditions in the memory. If the same query is executed for the second time, in addition, if the cache is not invalid, the query results can be directly returned without the need to obtain data from the engine.
-


Several notes:

A) The QC structure is hash, and the key is the original text of the query string. Therefore, to hit QC, the query statement must be identical to the previous one, including the case must be consistent and spaces cannot be increased or decreased.

B) Qc can cache multiple query statements and results in a table.

C) DML or DDL operations on a table will delete the cache related to the table from QC.

2. Lock Model

So we talk about the lock granularity. The entire QC only has one instance in the memory: Query_cache query_cache;

Let's take a look at the code of the failure logic mentioned in c above.

1. void Query_cache: invalidate_table (THD * thd, uchar * key, uint32 key_length)
2 .{
3. DEBUG_SYNC (thd, "wait_in_query_cache_invalidate1 ″);
4.
5. lock ();
6.
7. DEBUG_SYNC (thd, "wait_in_query_cache_invalidate2 ″);
8.
9. if (query_cache_size> 0) 10. invalidate_table_internal (thd, key, key_length );
11.
12. unlock ();
13 .}
Here we can see that lock () has no parameter, and the function uses a global semaphore COND_cache_status_changed to control it.

Therefore, even if the two dml update Tables are different, they will lock each other because they both fail the cache items in QC.

Therefore, it is a "global lock ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.