The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
· 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s...
Transcript of · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s...
![Page 1: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/1.jpg)
![Page 2: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/2.jpg)
<Insert Picture Here>
MySQL Client Side Caching
Johannes SchlüterTwitter: @phperrorMySQL Engineering – Connectors and Client Connectivity
![Page 3: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/3.jpg)
# pecl install mysqlnd_qc-beta
![Page 4: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/4.jpg)
Gracias por vuestra atención!
![Page 5: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/5.jpg)
mysqlnd
Server API (SAPI)
CGI CLI Embed ISAPI NSAPI phttpd thttpd ...
Zend Engine PHP Runtime
PHP Extensions
bcmath mysql mysqli mysqlnd pdo pdo_mysql xml ...
![Page 6: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/6.jpg)
PHP 5.3 and mysqlndP
HP
PH
P M
em
ory
PH
P S
tre
ams
Infrastructure
mysqlnd – MySQL native driver for PHP
MySQL Server
ext/mysql mysqli PDO_mysql…
PHP Module (Extension) API
![Page 7: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/7.jpg)
Building PHP with mysqlnd
• ./configure \ --with-mysql=mysqlnd \ --with-mysqli=msqlnd \ --with-pdo-mysql=mysqlnd
• Default on Windows and some distributions
![Page 8: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/8.jpg)
mysqlnd vs. libmysql
MySQL Server MySQL Server
mysqlnd libmysql
PHP PHP
PHP Memory
libmysql Memory
PHP Memory
PHP Memory
copy
copy
use
dire
ctly
copy
![Page 9: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/9.jpg)
mysqlnd Statistics
• Around 150 statistic values collected• mysqli_get_client_stats (),
mysqli_get_connection_stats()
![Page 10: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/10.jpg)
Asynchronous Queries
/* Do something */
PHP Script MySQL
query
result
query
poll
result
$conn = new MySQLi(...);
$conn->query( "SELECT * FROM t WHERE ....",
MYSQLI_ASYNC);
/* Process query results */
mysqli_poll($links, $errors, $reject, 1);
![Page 11: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/11.jpg)
Sharding
<?php…?>
![Page 12: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/12.jpg)
Sharding
foreach ($all_links as $link) $link->query("SELECT 'test' ", MYSQLI_ASYNC);
$processed = 0;do {
$links = $all_links; if (!mysqli_poll($links, $errors, $reject, 1)) continue; /* TIMEOUT */ foreach ($links as $link) { if ($result = $link->reap_async_query()) { print_r($result->fetch_row()); mysqli_free_result($result); $processed++; } }} while ($processed < count($all_links));
![Page 13: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/13.jpg)
mysqlnd plugins
Plugin Hook
mysqlnd Query
mysqli::query()mysql_query() PDO::query()
Wire Protocol
Plugin Hook
Network
![Page 14: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/14.jpg)
Drupal, Symphony, phpMyFAQ, phpMyAdmin, Oxid, ...
ext/mysql, ext/mysqli, ext/PDO_MYSQL
MySQL Server
mysqlnd
Load Balancing Monitoring Performance
mysqlnd plugin
![Page 15: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/15.jpg)
Query Cache Plugin vs. MySQL Proxy
Application client Application client Application client
MySQL Proxy
PHP client PHP client PHP client
mysqlnd pluginmysqlnd plugin mysqlnd plugin
MySQL Server
MySQL Server
![Page 16: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/16.jpg)
Experimental Extensions
• By Oracle:– mysqlnd_sip– mysqlnd_mc– mysqlnd_ms– mysqlnd_pscache
• By Community:– mysqlnd_uh
(David Soria Parra / Mayflower GmbH)
• http://pecl.php.net/
![Page 17: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/17.jpg)
The Database Is The Bottleneck
![Page 18: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/18.jpg)
Caching!
![Page 19: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/19.jpg)
“Traditional” Caches
• MySQL Server Cache– MySQL Query Cache
• Application-Level Cache– PEAR::Cache, Zend_Cache, …
![Page 20: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/20.jpg)
MySQL Query Cache (Server-Side)
✔ Integrated with the MySQL Server✔ No application changes✔ Automatic invalidation
– table based
✗ Needs network communication with server✗ Needs server resources (memory, CPU)
![Page 21: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/21.jpg)
Application-Level Caches
✔ Can cache higher level structures– Complete rendered HTML blocks
✔ Multiple backends– Can be shared over multiple servers (via memcache etc.)
✗ No automatic invalidation✗ Requires application changes
![Page 22: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/22.jpg)
<Insert Picture Here>
Introducing:mysqlnd Client Side Cache
![Page 23: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/23.jpg)
# pecl install mysqlnd_qc-beta
Andrey HristovUlf Wendel
![Page 24: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/24.jpg)
mysqlnd Query Cache
PHP
mysql / mysqli / PDO_mysql
mysqlnd
Cache Backend
Query Cache
MySQL Server
SELECT a FROM t No!
SELECT aFROM t
ResultResult
Local Memory, APC, Memcache, Custom Handler
![Page 25: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/25.jpg)
Key Properties
• Transparent– PHP Extension hooking into mysqlnd
• Works with ext/mysql, mysqli, pdo_mysql
• Pluggable storage handler– By default: local memory, APC, memcache, SQLite– PHP Userspace
• Invalidation via TTL– No automatic invalidation by server– Custom handlers may use custom invalidation logic
![Page 26: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/26.jpg)
Transparent?
$mysqli = new mysqli($host, $user, $pw, $db);$sql = “SELECT SLEEP(10) FROM table”;
$start = microtime(true);
$res = $mysqli->query($sql);$res = $mysqli->query($sql);
$end = microtime(true);echo $end - $start;
→ 20.019539117813
![Page 27: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/27.jpg)
Transparent?
$mysqli = new mysqli($host, $user, $pw, $db);$sql = sprintf(“/*%s*/SELECT SLEEP(10) FROM table”, MYSQLND_QC_ENABLE_SWITCH);
$start = microtime(true);
$res = $mysqli->query($sql);$res = $mysqli->query($sql);
$end = microtime(true);echo $end - $start;
→ 10.142804088593
![Page 28: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/28.jpg)
Transparent!
mysqlnd_qc.cache_by_default = 1
! Usually you should NOT do this!
![Page 29: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/29.jpg)
SQL Hints
• MYSQLND_QC_ENABLE_SWITCH– qc=on
• MYSQLND_QC_DISABLE_SWITCH– qc=off
• MYSQLND_QC_TTL_SWITCH– qc_ttl=
![Page 30: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/30.jpg)
Storage Handlers
●Storage– Scope: request, process, machine, multi-machine– Location: distance to cache– Replacement strategy– Slam defense strategy
● Decide what to cache– is_select() - detect SQL hints
● Extended statistics– Storage statistics, traces, timings
![Page 31: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/31.jpg)
Cache Expiry
Client 1
MySQL
Client 2...n
Cache Hit
MySQL
Client 1 Client 2...n
![Page 32: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/32.jpg)
Slam Defense
Client 1
MySQL
Client 2...n
Expired
MySQL
Client 1 Client 2...n
Cache Hit
Refresh
![Page 33: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/33.jpg)
Core Statistics
• Collected by: mysqlnd_qc core– php.net/manual/en/function.mysqlnd_qc_get_core_stats.php
• Scope: process– Process: mysqlnd_qc_get_core_stats()– Aggregated values from all PHP MySQL APIs
• Contents: wide range– Cache usage and efficiency– Network related– Timings
![Page 34: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/34.jpg)
Query Statistics and Backtraces
• Collected by: mysqlnd_qc core– php.net/manual/en/function.mysqlnd_qc_get_query_trace_log
.php
• Scope: process– mysqlnd_qc_get_query_trace_log()– mysqlnd_qc_get_normalized_query_trace_log()
• Contents– Origin - backtrace– Timings
![Page 35: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/35.jpg)
Storage Handler Statistics
• Collected by: storage handler– php.net/manual/en/function..mysqlnd_qc_get_cache_info.php
• Scope: cache entry– Depends on storage handler scope– Aggregated values from all PHP MySQL APIs
• Contents: none or assorted– Depends on storage handler support– APC: timings, hit ratio, result set size– Default: APC plus result set meta data
![Page 36: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/36.jpg)
User-Defined Storage Handler
• Procedural– php.net/manual/en/function.mysqlnd_qc_set_user_handlers.php– Callback functions
• Object oriented– slideshare.net/nixnutz/mysqlnd-query-cache-plugin-userdefined-
storage-handler– Interface mysqlnd_qc_handler
• Extending mysqlnd_qc_handler_default
![Page 37: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/37.jpg)
<?php/* auto_prepend_file = /path/to/prepend.php */
class my_qc extends mysqlnd_qc_handler_default { public function is_select($query) { if (preg_match("@from employees where@ism", $query)) { /* cache query for mysqlnd_qc.ttl seconds */ return true; } return parent::is_select($query); }}
$qc = new my_qc();mysqlnd_qc_change_handler($qc);?>
![Page 38: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/38.jpg)
Resources
• php.net/mysqlnd_qc– Installation, Examples, Functions
• php.net/mysqlnd– Introduction, Changes, C plugin API
• slideshare.net/nixnutz/presentations– QC Basics– QC User defined storage handler– QC Statistics– QC Benchmark impressions
![Page 39: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/39.jpg)
![Page 40: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/40.jpg)
The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
![Page 41: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/41.jpg)
![Page 42: · 2010. 10. 31. · PHP 5.3 and mysqlnd P H P P H P M e m o r y P H P S t r e a m s Infrastructure mysqlnd – MySQL native driver for PHP MySQL Server](https://reader034.fdocuments.us/reader034/viewer/2022052009/601df0a6c044876132668eb4/html5/thumbnails/42.jpg)