Home Database Mysql Tutorial What is the principle of mysql database query caching

What is the principle of mysql database query caching

Oct 26, 2020 am 11:58 AM
mysql Query cache

The principle of mysql database query caching is: 1. Cache the result set and SQL statement of the SELECT operation, the key is sql, and the value is the query result set; 2. If a new SELECT statement arrives, use this sql as the key. Query in the cache, and if there is a match, return the cached result set.

What is the principle of mysql database query caching

Mysql database query caching principle is:

Overview

Query Cache (QC for short) stores SELECT statements and the data results they generate. I have nothing to do, so I would like to summarize this topic and make a memo!

Super detailed mysql database query cache summary, worth collecting

What is the principle of mysql database query caching

Working principle

  • Cache the result set and SQL statement of the SELECT operation, the key is sql, and the value is the query result set;

  • If a new SELECT statement arrives, use this sql as the key to query in the cache , if it matches, the cached result set will be returned;

  • Matching criteria: Whether it is exactly the same as the cached SQL statement. Letters in SQL are case-sensitive and spaces in between are simply understood as A key-value structure is stored. The key is sql and the value is the sql query result. Java's String equals() is used when matching, for example:

  • select age from user and select AGE from user will not match because the case is different;

  • select age from use and select age from user will not match because the spaces are different;

  • The spaces on both sides of sql can be ignored. It can be considered that the key is trimmed and then compared with equals.

View mysql setting parameters

Execution

show variables like '%query_cache%';

What is the principle of mysql database query caching

You can see the relevant parameters:

  • query_cache_type: 0-Do not enable query cache; 1-Enable, 2-Enable, the default value is 0;

  • query_cache_size: Set the total size of the cache area. The minimum value allowed to set query_cache_size is 40K. The default is 1M. The recommended setting is: 64M/128M;

  • query_cache_limit: Limits the maximum size of a single query record set that can be cached in the cache area. The default setting is 1M

  • When query_cache_type is 1, as long as the query cache requirements are met, the client's query statements and records All sets can be cached. If SQL_NO_CACHE is added to SQL, it will not be cached;

  • query_cache_type is 2, as long as the parameter: SQL_CACHE is added to SQL and meets the requirements of query caching, the customer The query statements and record sets on the client can be cached.

View cache usage

show status like '%Qcache%%';

What is the principle of mysql database query caching

##You can see the relevant parameters:

  • Qcache_hits: the number of cache hits;

  • Qcache_inserts: the number of insertions in the cache, each cache adds 1. Note that this is not the cache quantity;

Enable query cache

Set the option query_cache_type = 1 and set query_cache_size = 67108864;

Note: The value of query_cache_size can be set within 100MB. In MySQL, the query cache is controlled by a global lock, and every time the memory block of the query cache is updated, it needs to be locked.

Turn off query cache

Set option query_cache_type = 0, and set query_cache_size = 0.

Applicable scenarios

Used for scenarios where the same statement is submitted frequently and the table data does not change very frequently, such as some static pages or a certain block in the page Information that changes infrequently.

Since the query cache needs to cache the latest data results, any changes to the table data (insert, update, delete or other operations that may produce data changes) will cause the query cache to be refreshed. Therefore, for a scenario where the update frequency is very low and the read-only query frequency is very high, it is more advantageous to turn on the query cache.

Not applicable scenarios

The query cache strictly requires that the two SQL requests must be exactly the same, including SQL statements, connected databases, protocol versions, character sets and other factors will all affect . The following are several scenarios where query caching is not applicable:

  • Subquery;

  • SQL called in procedures, functions, triggers, and events , or refer to these results;

  • When the query involves some special functions, such as: BENCHMARK(), CURDATE(), CURRENT_TIME(), CURRENT_TIMESTAMP(), NOW(), SLEEP(), CONNECTION_ID(), CURRENT_DATE(), CURRENT_USER(), PASSWORD(), RAND(), UUID(), ENCRYPT(), LAST_INSERT_ID(), etc.;

  • The query involves mysql, information_schema or performance_schema.

  • Queries similar to SELECT...LOCK IN SHARE MODE, SELECT...FOR UPDATE, SELECT..INTO OUTFILE/DUMPFILE, SELECT * FROM... WHERE autoincrement_col IS NULL;

  • The SELECT execution plan uses temporary tables;

  • Queries that do not reference any table, such as SELECT 1 2;

  • The query generated warnings;

  • The SQL_NO_CACHE keyword exists in the SELECT statement;

  • involves partition tables.

More related free learning recommendations: mysql tutorial(Video)

The above is the detailed content of What is the principle of mysql database query caching. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles