Built-in query caching for all PHP MySQL extensions/APIs

U
Ulf WendelSenior Software Engineer at Oracle
MySQL native driver for PHP: The query cache plugin
mysqlnd_qc:  The query cache plugin  Ulf Wendel, Andrey Hristov MySQL Connectors Team Sun Microsystems
Table of Contents ,[object Object]
Basic Usage ,[object Object],[object Object]
Cache Architecture
Storage handler ,[object Object],[object Object]
Configuration
Key Features ,[object Object]
supports PDO_MYSQL
supports ext/mysql ,[object Object],[object Object]
custom: user callbacks ,[object Object],[object Object]
custom: user callbacks
Possibly asked questions ,[object Object]
Only buffered queries can be cached ,[object Object]
mysqli_real_query() + mysqli_store_result() ,[object Object],[object Object]
(this is the default) ,[object Object],[object Object]
Uncachable queries ,[object Object]
mysqli_query() + MYSQLI_ASYNC
mysqli_stmt_*() ,[object Object],[object Object],[object Object],[object Object]
Benchmarks? ,[object Object]
Chart compares handler – no more
f Basic Usage $mysqli = new mysqli($host, $user, $passwd, $db, $port, $socket);; // Cached: SQL hint used $res = $mysqli->query("/*qc=on*/" . "SELECT id, label FROM test"); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free(); SQL hint /*qc=on*/ to control caching // Uncached: no SQL hint $res = $mysqli->query("SELECT id, label FROM test"); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free();
Installation ,[object Object]
./configure --help | grep mysql
./configure --help | grep qc
make clean && make
Compared to MySQL Server Cache ,[object Object]
Shorter distance to the app (round trip times)
Scale by client
No server overload
Less sophisticated invalidation (stale data!)
Compared to Memcache ,[object Object]
Often used for storing cached data
We offer a Memcache storage handler ,[object Object],[object Object]
caching of raw data instead of processed data
Table of Contents ,[object Object]
Basic Usage ,[object Object],[object Object]
Cache Architecture
Storage handler ,[object Object],[object Object]
Configuration
How PHP connects to MySQL  PHP MySQL Server Library: implements MySQL Client-Server Protocol PHP API for PHP applications MySQL native driver for PHP / MySQL Client Library
Inside PHP (on the C level!) PHP Extensions Zend Engine PDO ext/mysql ext/mysqli SAPI PDO_MYSQL PDO_XYZ MySQL Client Library (libmysql)  or MySQL native driver for PHP (default as of PHP 5.3)
PHP 5.3.2: mysqlnd plugin interface <?php  /* Any PHP MySQL application */  ?> ext/*mysql* ext/*mysql* Plugin MySQL native driver for PHP C plugins operate transparently: no user API changes
“Object-orientation” in mysqlnd <?php  mysqli_query($link, … ); ?> Inside mysqlnd: “objects” of data and function pointer mysqlnd_connection connection_methods host user password ... connection_methods query() store_result() use_result() free_result() ...
Plugins as “Proxies” or “Decorators” <?php  mysqli_query($link, … ); ?> Plugins replace or extend internal mysqlnd objects connection_methods query() store_result() use_result() free_result() ... Plugin query() store_result() use_result() free_result() ... mysqlnd_connection connection_methods host user password ...
Plugins can change everything ,[object Object]
metadata access
utility calls ,[object Object],[object Object]
… ,[object Object],[object Object]
C level support for Plugins ,[object Object]
management of function tables ,[object Object],[object Object]
via result set object
via statistics hash
Query cache: proxy-style plugin Cache MySQL <?php  /* Any PHP MySQL application */  ?> ext/*mysql* ext/*mysql* mysqlnd mysqlnd Cache miss: record wire data, cache hit: replay Query Cache Plugin
Query Cache: Miss mysqlnd orig. mysqlnd_conn object mysqlnd_qc object query()  Should cache? Yes! Is cached? No! Activate data recorder query() Send query to MySQL Deactivate recorder Cache wire data Decode data  Fetch reply
Query Cache: Hit mysqlnd mysqlnd_qc object query()  Should cache? Yes! Is cached? Yes! Fetch data from cache Decode data
Storage handler responsibilities ,[object Object]
Location: distance to cache
Replacement strategy
Slam defense strategy ,[object Object],[object Object]
Parse SQL-Hints ,[object Object],[object Object]
Cache storage handler (I) ,[object Object]
1 of 62

Recommended

MySQL 5.7 Fabric: Introduction to High Availability and Sharding by
MySQL 5.7 Fabric: Introduction to High Availability and Sharding MySQL 5.7 Fabric: Introduction to High Availability and Sharding
MySQL 5.7 Fabric: Introduction to High Availability and Sharding Ulf Wendel
52.5K views65 slides
Vote NO for MySQL by
Vote NO for MySQLVote NO for MySQL
Vote NO for MySQLUlf Wendel
3.2K views61 slides
MySQL 5.7 clustering: The developer perspective by
MySQL 5.7 clustering: The developer perspectiveMySQL 5.7 clustering: The developer perspective
MySQL 5.7 clustering: The developer perspectiveUlf Wendel
5.8K views85 slides
Data massage: How databases have been scaled from one to one million nodes by
Data massage: How databases have been scaled from one to one million nodesData massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesUlf Wendel
24.3K views307 slides
NoSQL in MySQL by
NoSQL in MySQLNoSQL in MySQL
NoSQL in MySQLUlf Wendel
23.2K views87 slides
The mysqlnd replication and load balancing plugin by
The mysqlnd replication and load balancing pluginThe mysqlnd replication and load balancing plugin
The mysqlnd replication and load balancing pluginUlf Wendel
3K views50 slides

More Related Content

What's hot

MySQL Group Replication by
MySQL Group ReplicationMySQL Group Replication
MySQL Group ReplicationUlf Wendel
33.1K views92 slides
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy by
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyThe PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyUlf Wendel
3K views75 slides
Intro to PECL/mysqlnd_ms (4/7/2011) by
Intro to PECL/mysqlnd_ms (4/7/2011)Intro to PECL/mysqlnd_ms (4/7/2011)
Intro to PECL/mysqlnd_ms (4/7/2011)Chris Barber
7K views25 slides
MySQL 5.6 Global Transaction Identifier - Use case: Failover by
MySQL 5.6 Global Transaction Identifier - Use case: FailoverMySQL 5.6 Global Transaction Identifier - Use case: Failover
MySQL 5.6 Global Transaction Identifier - Use case: FailoverUlf Wendel
2.2K views16 slides
DIY: A distributed database cluster, or: MySQL Cluster by
DIY: A distributed database cluster, or: MySQL ClusterDIY: A distributed database cluster, or: MySQL Cluster
DIY: A distributed database cluster, or: MySQL ClusterUlf Wendel
10.6K views81 slides
PHP mysqlnd connection multiplexing plugin by
PHP mysqlnd connection multiplexing pluginPHP mysqlnd connection multiplexing plugin
PHP mysqlnd connection multiplexing pluginUlf Wendel
11.2K views20 slides

What's hot(20)

MySQL Group Replication by Ulf Wendel
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
Ulf Wendel33.1K views
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy by Ulf Wendel
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyThe PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
Ulf Wendel3K views
Intro to PECL/mysqlnd_ms (4/7/2011) by Chris Barber
Intro to PECL/mysqlnd_ms (4/7/2011)Intro to PECL/mysqlnd_ms (4/7/2011)
Intro to PECL/mysqlnd_ms (4/7/2011)
Chris Barber7K views
MySQL 5.6 Global Transaction Identifier - Use case: Failover by Ulf Wendel
MySQL 5.6 Global Transaction Identifier - Use case: FailoverMySQL 5.6 Global Transaction Identifier - Use case: Failover
MySQL 5.6 Global Transaction Identifier - Use case: Failover
Ulf Wendel2.2K views
DIY: A distributed database cluster, or: MySQL Cluster by Ulf Wendel
DIY: A distributed database cluster, or: MySQL ClusterDIY: A distributed database cluster, or: MySQL Cluster
DIY: A distributed database cluster, or: MySQL Cluster
Ulf Wendel10.6K views
PHP mysqlnd connection multiplexing plugin by Ulf Wendel
PHP mysqlnd connection multiplexing pluginPHP mysqlnd connection multiplexing plugin
PHP mysqlnd connection multiplexing plugin
Ulf Wendel11.2K views
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency by Ulf Wendel
MySQL 5.6 Global Transaction IDs - Use case: (session) consistencyMySQL 5.6 Global Transaction IDs - Use case: (session) consistency
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
Ulf Wendel2.8K views
PoC: Using a Group Communication System to improve MySQL Replication HA by Ulf Wendel
PoC: Using a Group Communication System to improve MySQL Replication HAPoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HA
Ulf Wendel6.1K views
Highly Available MySQL/PHP Applications with mysqlnd by Jervin Real
Highly Available MySQL/PHP Applications with mysqlndHighly Available MySQL/PHP Applications with mysqlnd
Highly Available MySQL/PHP Applications with mysqlnd
Jervin Real4.9K views
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4 by Ulf Wendel
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
Ulf Wendel8.5K views
Introduction to Galera by Henrik Ingo
Introduction to GaleraIntroduction to Galera
Introduction to Galera
Henrik Ingo9.6K views
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013 by Serge Frezefond
 MySQL PHP native driver  : Advanced Functions / PHP forum Paris 2013   MySQL PHP native driver  : Advanced Functions / PHP forum Paris 2013
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
Serge Frezefond16.4K views
High-Availability using MySQL Fabric by Mats Kindahl
High-Availability using MySQL FabricHigh-Availability using MySQL Fabric
High-Availability using MySQL Fabric
Mats Kindahl4.8K views
Mysql high availability and scalability by yin gong
Mysql high availability and scalabilityMysql high availability and scalability
Mysql high availability and scalability
yin gong1.8K views
MySQL Multi Master Replication by Moshe Kaplan
MySQL Multi Master ReplicationMySQL Multi Master Replication
MySQL Multi Master Replication
Moshe Kaplan19.9K views
MySQL Group Replication - an Overview by Matt Lord
MySQL Group Replication - an OverviewMySQL Group Replication - an Overview
MySQL Group Replication - an Overview
Matt Lord7.7K views
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication by Kenny Gryp
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationPercona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp18.4K views
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11 by Kenny Gryp
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
Kenny Gryp660 views

Similar to Built-in query caching for all PHP MySQL extensions/APIs

Mysqlnd, an unknown powerful PHP extension by
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionjulien pauli
6.8K views60 slides
mysqlnd query cache plugin: user-defined storage handler by
mysqlnd query cache plugin: user-defined storage handlermysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlerUlf Wendel
1.2K views48 slides
Mysqlnd query cache plugin statistics and tuning by
Mysqlnd query cache plugin statistics and tuningMysqlnd query cache plugin statistics and tuning
Mysqlnd query cache plugin statistics and tuningUlf Wendel
1.6K views66 slides
Presentation of OrientDB v2.2 - Webinar by
Presentation of OrientDB v2.2 - WebinarPresentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarOrient Technologies
2.9K views49 slides
Mysqlnd Query Cache by
Mysqlnd Query CacheMysqlnd Query Cache
Mysqlnd Query CacheAnis Berejeb
502 views42 slides
Barcelona mysqlnd qc by
Barcelona mysqlnd qcBarcelona mysqlnd qc
Barcelona mysqlnd qcAnis Berejeb
2.8K views42 slides

Similar to Built-in query caching for all PHP MySQL extensions/APIs(20)

Mysqlnd, an unknown powerful PHP extension by julien pauli
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
julien pauli6.8K views
mysqlnd query cache plugin: user-defined storage handler by Ulf Wendel
mysqlnd query cache plugin: user-defined storage handlermysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handler
Ulf Wendel1.2K views
Mysqlnd query cache plugin statistics and tuning by Ulf Wendel
Mysqlnd query cache plugin statistics and tuningMysqlnd query cache plugin statistics and tuning
Mysqlnd query cache plugin statistics and tuning
Ulf Wendel1.6K views
Barcelona mysqlnd qc by Anis Berejeb
Barcelona mysqlnd qcBarcelona mysqlnd qc
Barcelona mysqlnd qc
Anis Berejeb2.8K views
Caching and tuning fun for high scalability by Wim Godden
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
Wim Godden2.4K views
Caching and tuning fun for high scalability by Wim Godden
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
Wim Godden1.5K views
DjangoCon 2010 Scaling Disqus by zeeg
DjangoCon 2010 Scaling DisqusDjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling Disqus
zeeg32.6K views
6 tips for improving ruby performance by Engine Yard
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
Engine Yard6.6K views
Using Apache as an Application Server by Phil Windley
Using Apache as an Application ServerUsing Apache as an Application Server
Using Apache as an Application Server
Phil Windley6.5K views
Advanced Ops Manager Topics by MongoDB
Advanced Ops Manager TopicsAdvanced Ops Manager Topics
Advanced Ops Manager Topics
MongoDB2.3K views
Symfony2 - from the trenches by Lukas Smith
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
Lukas Smith3.3K views
When dynamic becomes static by Wim Godden
When dynamic becomes staticWhen dynamic becomes static
When dynamic becomes static
Wim Godden638 views
Osiąganie mądrej architektury z Symfony2 by 3camp
Osiąganie mądrej architektury z Symfony2 Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2
3camp692 views
Instrumenting plugins for Performance Schema by Mark Leith
Instrumenting plugins for Performance SchemaInstrumenting plugins for Performance Schema
Instrumenting plugins for Performance Schema
Mark Leith19.7K views
Caching and tuning fun for high scalability by Wim Godden
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
Wim Godden2.9K views

More from Ulf Wendel

HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL by
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQLHTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQLUlf Wendel
41.9K views89 slides
PHPopstar der PHP Unconference 2011 by
PHPopstar der PHP Unconference 2011PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011Ulf Wendel
2K views44 slides
Award-winning technology: Oxid loves the query cache by
Award-winning technology: Oxid loves the query cacheAward-winning technology: Oxid loves the query cache
Award-winning technology: Oxid loves the query cacheUlf Wendel
3.2K views92 slides
The power of mysqlnd plugins by
The power of mysqlnd pluginsThe power of mysqlnd plugins
The power of mysqlnd pluginsUlf Wendel
1.6K views72 slides
Mysqlnd query cache plugin benchmark report by
Mysqlnd query cache plugin benchmark reportMysqlnd query cache plugin benchmark report
Mysqlnd query cache plugin benchmark reportUlf Wendel
870 views45 slides
Mysqlnd Async Ipc2008 by
Mysqlnd Async Ipc2008Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008Ulf Wendel
14.2K views36 slides

More from Ulf Wendel(6)

HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL by Ulf Wendel
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQLHTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
Ulf Wendel41.9K views
PHPopstar der PHP Unconference 2011 by Ulf Wendel
PHPopstar der PHP Unconference 2011PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011
Ulf Wendel2K views
Award-winning technology: Oxid loves the query cache by Ulf Wendel
Award-winning technology: Oxid loves the query cacheAward-winning technology: Oxid loves the query cache
Award-winning technology: Oxid loves the query cache
Ulf Wendel3.2K views
The power of mysqlnd plugins by Ulf Wendel
The power of mysqlnd pluginsThe power of mysqlnd plugins
The power of mysqlnd plugins
Ulf Wendel1.6K views
Mysqlnd query cache plugin benchmark report by Ulf Wendel
Mysqlnd query cache plugin benchmark reportMysqlnd query cache plugin benchmark report
Mysqlnd query cache plugin benchmark report
Ulf Wendel870 views
Mysqlnd Async Ipc2008 by Ulf Wendel
Mysqlnd Async Ipc2008Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008
Ulf Wendel14.2K views

Recently uploaded

Info Session November 2023.pdf by
Info Session November 2023.pdfInfo Session November 2023.pdf
Info Session November 2023.pdfAleksandraKoprivica4
15 views15 slides
Melek BEN MAHMOUD.pdf by
Melek BEN MAHMOUD.pdfMelek BEN MAHMOUD.pdf
Melek BEN MAHMOUD.pdfMelekBenMahmoud
17 views1 slide
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveNetwork Automation Forum
43 views35 slides
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...The Digital Insurer
24 views52 slides
Mini-Track: Challenges to Network Automation Adoption by
Mini-Track: Challenges to Network Automation AdoptionMini-Track: Challenges to Network Automation Adoption
Mini-Track: Challenges to Network Automation AdoptionNetwork Automation Forum
17 views27 slides
Future of AR - Facebook Presentation by
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentationssuserb54b561
22 views27 slides

Recently uploaded(20)

Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
Future of AR - Facebook Presentation by ssuserb54b561
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
ssuserb54b56122 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays17 views
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
Case Study Copenhagen Energy and Business Central.pdf by Aitana
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdf
Aitana17 views
Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely29 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
The Forbidden VPN Secrets.pdf by Mariam Shaba
The Forbidden VPN Secrets.pdfThe Forbidden VPN Secrets.pdf
The Forbidden VPN Secrets.pdf
Mariam Shaba20 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays33 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院

Built-in query caching for all PHP MySQL extensions/APIs

  • 1. MySQL native driver for PHP: The query cache plugin
  • 2. mysqlnd_qc: The query cache plugin Ulf Wendel, Andrey Hristov MySQL Connectors Team Sun Microsystems
  • 3.
  • 4.
  • 6.
  • 8.
  • 10.
  • 11.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 19.
  • 20.
  • 21. Chart compares handler – no more
  • 22. f Basic Usage $mysqli = new mysqli($host, $user, $passwd, $db, $port, $socket);; // Cached: SQL hint used $res = $mysqli->query(&quot;/*qc=on*/&quot; . &quot;SELECT id, label FROM test&quot;); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free(); SQL hint /*qc=on*/ to control caching // Uncached: no SQL hint $res = $mysqli->query(&quot;SELECT id, label FROM test&quot;); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free();
  • 23.
  • 24. ./configure --help | grep mysql
  • 27.
  • 28. Shorter distance to the app (round trip times)
  • 32.
  • 33. Often used for storing cached data
  • 34.
  • 35. caching of raw data instead of processed data
  • 36.
  • 37.
  • 39.
  • 41. How PHP connects to MySQL PHP MySQL Server Library: implements MySQL Client-Server Protocol PHP API for PHP applications MySQL native driver for PHP / MySQL Client Library
  • 42. Inside PHP (on the C level!) PHP Extensions Zend Engine PDO ext/mysql ext/mysqli SAPI PDO_MYSQL PDO_XYZ MySQL Client Library (libmysql) or MySQL native driver for PHP (default as of PHP 5.3)
  • 43. PHP 5.3.2: mysqlnd plugin interface <?php /* Any PHP MySQL application */ ?> ext/*mysql* ext/*mysql* Plugin MySQL native driver for PHP C plugins operate transparently: no user API changes
  • 44. “Object-orientation” in mysqlnd <?php mysqli_query($link, … ); ?> Inside mysqlnd: “objects” of data and function pointer mysqlnd_connection connection_methods host user password ... connection_methods query() store_result() use_result() free_result() ...
  • 45. Plugins as “Proxies” or “Decorators” <?php mysqli_query($link, … ); ?> Plugins replace or extend internal mysqlnd objects connection_methods query() store_result() use_result() free_result() ... Plugin query() store_result() use_result() free_result() ... mysqlnd_connection connection_methods host user password ...
  • 46.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52. via result set object
  • 54. Query cache: proxy-style plugin Cache MySQL <?php /* Any PHP MySQL application */ ?> ext/*mysql* ext/*mysql* mysqlnd mysqlnd Cache miss: record wire data, cache hit: replay Query Cache Plugin
  • 55. Query Cache: Miss mysqlnd orig. mysqlnd_conn object mysqlnd_qc object query() Should cache? Yes! Is cached? No! Activate data recorder query() Send query to MySQL Deactivate recorder Cache wire data Decode data Fetch reply
  • 56. Query Cache: Hit mysqlnd mysqlnd_qc object query() Should cache? Yes! Is cached? Yes! Fetch data from cache Decode data
  • 57.
  • 60.
  • 61.
  • 62.
  • 64. User can flush cache
  • 65.
  • 67. User can flush cache
  • 69.
  • 70. Location: can be local or remote
  • 72.
  • 73. Internal and not exposed to PHP land
  • 74.
  • 75. Scope, if using file: single (multiple) machine
  • 76. Location: can be local or remote
  • 77. User can flush cache
  • 78.
  • 79.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85. Choice of distance to client
  • 86. Cache Miss Client 2...100 Client 2...100 Client 2...100 BTW, plan your cache carefully! What if a shared cache entry expires? Client 1 Client 2...n Cache Hit MySQL Client 2...100 Client 2...100 Client 2...100 Client 1 Client 2...n MySQL
  • 87. Cache Miss Optimize storage for reuse? Memory consumption versus peak loads Client 1 Client 2 MySQL Client 2...100 Client 2...100 Client 2...100 Client 1 Client 2...n MySQL Cache Miss Cache Hit Client 3..n
  • 88.
  • 89.
  • 91.
  • 93.
  • 94. Control which query gets cached
  • 95. Control where to store cached data
  • 96. Implement your own invalidation strategy
  • 97. Maintain your own statistics
  • 98.
  • 99.
  • 101.
  • 102. you must implement all storage handler functions
  • 103. Procedural user storage handler void mysqlnd_qc_set_user_handlers( string get_hash_key, string find_query_in_cache, string return_to_cache, string add_query_to_cache_if_not_exists, string query_is_select, string update_cache_stats, string clear_cache ) Yes, this API may be ugly. BUT: Prototype! BUT: See extra presentation for vodoo! mysqlnd_qc_set_user_handlers()
  • 104. User storage handler interface (I) function get_hash($host_info, $port, $user, $db, $query) { /* returns string */ return $key; } function find($key) { /* returns boolean */ return $found; } function return_to_cache($key) { /* void – leave empty */ } function add($key, $data, $ttl, $runtime, $store_time, $row_count) { /* returns boolean */ return $added_to_cache; }
  • 105. User storage handler interface (II) function query_is_select($query) { /* returns mixed - boolean false if the query shall not be cached boolean true or double 0 to use mysqlnd_qc.ttl default double >= 0 to set TTL different from mysqlnd_qc.ttl */ return $to_cache_or_not; } function update_stats($key, $run_time, $store_time) { /* void – data to update your per query cache statistics *// } function clear_cache() { /* returns boolean */ return $cache_has_been_flushed; }
  • 106.
  • 108. See also extra presentation!
  • 109.
  • 117. Changing the storage handler bool mysqlnd_qc_change_handler(string handler) Changes the storage handler. Returns false if the current handler cannot be shutdown or the requested handler cannot be initialized. Failing to change the handler should be considered as a fatal error unless the change fails because the requested handler is unknown.
  • 118. Procedural user storage handler void mysqlnd_qc_set_user_handlers( string get_hash_key, string find_query_in_cache, string return_to_cache, string add_query_to_cache_if_not_exists, string query_is_select, string update_cache_stats, string clear_cache ) Sets the function names of a user defined storage handler and puts them into use. See also extra presentation!
  • 119. Available handlers array mysqlnd_qc_get_handlers() Returns a list of available handler and their versions. The handler “default”, “user”, the class “ myslqnd_qc_handler_default” are always available. “ apc” and “memcache” will be reported if support for those handlers has been enabled at compile time.
  • 120. Flushing the cache The function is not supported by the Memcache handler! bool mysqlnd_qc_clear_cache() Returns true if the handler supports the operation, and has flushed the cache.
  • 121. Cache info and handler statistics // Run some queries and generate cache hits $res = $mysqli->query(&quot;/*qc=1*/&quot; . &quot;SELECT id, label FROM test&quot;); var_dump($res->fetch_all(MYSQLI_ASSOC)); C-based handler cache information and statistics var_dump(mysqlnd_qc_get_cache_info()); array(4) { [&quot;num_entries&quot;]=> int(%d) [&quot;handler&quot;]=> string(7) &quot;default&quot; [&quot;handler_version&quot;]=> string(5) &quot;1.0.0&quot; [&quot;data&quot;]=> array(%d) { [&quot;%s”] => array(2) { [“statistics”] => array(...), [“metadata”] => array(...) } [, ...] }
  • 122.
  • 123.
  • 126.
  • 127. Core statistics php.ini setting: mysqlnd_qc_collect_statistics = 1 array mysqlnd_qc_get_core_statistics() Returns a list 20+ statistics collected by the core of the query cache plugin, if the PHP configuration setting mysqlnd_qc_collect_statistics is set to 1. The statistics are provided by the core and therefore available with all storage handlers and when using user-defined storage handlers. The statistics cover cache accesses, failures, network traffic as well as aggregated run and store times.
  • 128. Query back trace php.ini setting: mysqlnd_qc.query_trace = 1 array mysqlnd_qc_get_query_trace_log() Returns a query back trace for every query that has been inspected by the query cache regardless if the query ended up being cached or not. The trace tells you where a query has been issues (see also debug_backtrace()). Together with the back trace you get run and store times and information on if the query has been cached.
  • 129. Normalized query back trace mysqlnd_qc.collect_normalized_query_trace = 1 array mysqlnd_qc_get_normalized_query_trace_log() Similar to mysqlnd_qc_get_query_trace_log() but with SQL statements normalized and aggregated by the normalized query string. Normalization refers to replacing actual parameters, for example in “WHERE a > 1”, with questionmarks like “ WHERE a > ?”. “WHERE a > ?” will match any value for “?”, for example “1”, “2”, “'abc'” but not other identifiers.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137. EXPERIMENTAL – use default of “0”
  • 138.
  • 139.
  • 140.
  • 141. APC handler stores data in the APC user cache
  • 142. Users can manipulate the cache entries!
  • 143.
  • 144.
  • 145.
  • 146. You should use SQLite as an in-memory storage
  • 147.
  • 148.
  • 149.
  • 150. All constants can be changed as compile time!
  • 151. Exported PHP classes class mysqlnd_qc_handler_default { public function init() {} public function is_select(...) {} public function get_hash_key(...) {} public function return_to_cache(...) {} public function add_to_cache(...) {} public function find_in_cache(...) {} public function update_cache_stats(...) {} public function get_stats(...) {} public function clear_cache() {} public function shutdown() {} }
  • 152. Exported PHP interfaces interface mysqlnd_qc_handler { public function is_select(...) {} public function get_hash_key(...) {} public function return_to_cache(...) {} public function add_to_cache(...) {} public function find_in_cache(...) {} public function update_cache_stats(...) {} public function get_stats(...) {} public function clear_cache() {} }
  • 153.
  • 154. this is what you look at ;-)
  • 155. Query cache plugin benchmark impressions
  • 156. Dig deeper with QC statistics
  • 157. Developing user storage handler Further reading
  • 158. The End Feedback: ulf.wendel@sun.com The End Feedback: [email_address] , [email_address]