EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Query Cache
Secondary Sidebar
MySQL Tutorial
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MySQL Query Cache

By Aanchal SharmaAanchal Sharma

MySQL Query Cache

Definition of MySQL Query Cache

MySQL Query Cache is a noticeable MySQL feature that is responsible to hustle up the data recovery from a database server. This is attained by keeping the MySQL statements SELECT together with the fetched record collection in memory so that when a client requests matching queries, then it can assist the data quicker without running the commands from the database again. If we compare to data reading from disk, the cached information from the RAM takes a littler access time that diminishes the latency, thus refining I/O(Input/Output) operations. Suppose, if we take an illustration of a WordPress website or any E-commerce portal/website having high read calls along with irregular data changes, then here query cache will boost the presentation of the MySQL database server extremely making it more walkable.

Syntax:

If we want to set up MySQL Query Cache, then we will follow the syntax of it explained as follows:

Except MariaDB has been definitely a built-in absence of query cache but it will be present always while inactive. We can view the availability of query cache on the server with the variable have_query_cache described below:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

SHOW VARIABLES LIKE ‘have_query_cache’;

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

Here, when executed this query if it is set to NO, user cannot allow the query cache except you recreate or reinstall a MariaDB version having cache availability. While the query cache is allowed in MariaDB by default for versions up to 10.1.6 but opening with version 10.1.7 it is disabled and if required we can support it by setting the server variable to query_cache_type to 1.

Though assisted in the versions of MariaDB prior to 10.1.7, the variable query_cache_size will be by default 0KB, which disables the query cache efficiently. But after this version, the cache size defaults to 1MB. If a user wants to set or configure the query cache size too a big enough quantity, it can be done as follows:

SET GLOBAL query_cache_size = 5000000;

Opening from MariaDB version 10.1.7, the query_cache_type is set to ON spontaneously when the server is initiated with setting the query_cache_size to a non-zero or non-default value.

How does Query Cache work in MySQL?

In MySQL, a fresh SELECT query statement is processed when the query cache is allowed so that the query cache is observed to view whether the query performs in the cache or not. Here, if the queries use the identical database server with a similar protocol version and also identical default character set then, those queries are measured as identical. The prepared statements are often deliberated as different compared to non-prepared statements.

If any similar query does not exist in the cache, then the query will be administered usually and stored with its result set within the query cache. If the query exists in the cache, then the results can be dragged from the cache that is much faster than handling it usually. The queries are to be inspected in a case-sensitive way, therefore:

SELECT * FROM z;

This query is different from following:

Select * from z;

In this context the comments are also measured and will make the queries vary:

/* Demo1 */ SELECT * FROM z;

This is different from below one:

/* Demo2 */ Select * from z;

If you want to strip comments before searching as an option, then view the server variable query_cache_strip_comments.

Whenever any changes are done to the records in a table, the entire affected result sets in the query cache are cleared. Thus, it cannot be possible to fetch data from the query cache. If the space enabled for the query cache is drained, then the eldest outcomes will be released from the cache.

If we implement query_cache_type = NO with the query specifying SQL_NO_CACHE i.e. case sensitive, then the server cannot cache the query and cannot retrieve results from the query cache. If we implement query_cache_type = DEMAND i.e. after feature MDEV-6631 request, and the query states SQL_CACHE, then MySQL server will perform the query cache.

Examples

MySQL Query Cache is normally responsible for tuning the MySQL performance and one can view the variables of query cache in the database server using the following commands:

SHOW VARIABLES LIKE %query_cache%;

One can even modify these variables applying SET SESSION or SET GLOBAL query statements shown below:

SET GLOBAL query_cache_size = 18777216;

Query cache is an important feature for query optimization improving performance. You can also display the status of query cache variable working in the server as:

SHOW STATUS LIKE “qcache%”;

The MySQL query cache is defined as a query results cache. It matches the incoming queries which initialize with SEL to a hash table, then if there exists a match it will return from the preceding execution of the query. But you will find few restrictions mentioned below:

  • The query cache escapes parsing and matches byte for byte.
  • Implementing non-deterministic features can result in not caching of the query that comprises temporary tables, RAND(), NOW(), UDFs, and user variables.
  • This query cache was intended for not serving the stale results. If any modifications occur to the underlying table(s) will result in all cache being cancelled for those tables.
  • Suppose if the query cache is applied for InnoDB to esteem MVCC when you have a transaction open, thus, the cache may not signify the data in your projected view.

Here, non-deterministic features denotes the query functions not being cached such as BENCHMARK(), CONVERT_TZ(), CONNECTION_ID(), CURRENT_TIME(), CURDATE(), ENCRYPT(), GET_LOCK(), CURTIME(), FOUND_ROWS(), DATABASE(), etc.

A query will not be cached if:

  • It implements no table or generates a warning.
  • It applies stored functions or local or user-defined variables.
  • Transaction having serialization at isolated level.
  • Having forms like:
  • SELECT with SQL_NO_CACHE
  • SELECT with INTO OUTFILE
  • SELECT with INTO DUMPFILE
  • SELECT FOR UPDATE

Conclusion

In this article, we have discussed MySQL Query Cache which is a feature supported in MySQL which when configured in the server boosts up the SQL queries and enhances the performance of operations. Though it is being deprecated in the MySQL version 5.7.20 and also eliminated in MySQL 8.0 query cache is still a robust tool if a user is using the supported MySQL versions. For the latest versions, one may also use a third-party tool such as ProxySQL for optimizing the MySQL database performance.

Recommended Articles

This is a guide to MySQL Query Cache. Here we discuss the definition of MySQL Query Cache, How does Query Cache works in MySQL along the examples respectively. You may also have a look at the following articles to learn more –

  1. MySQL NOT IN
  2. MySQL Clone Table
  3. MySQL Show Users
  4. MySQL GROUP BY Count
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more