SlideShare a Scribd company logo
2014/10/11 
PHP Conference Japan 2014 
do_aki 
1 
updated 2014-10-21
@do_aki 
@do_aki 
http://do-aki.net/ 
2
agenda 
1. about mysqlnd 
– position, role 
2. libmysql vs mysqlnd 
– functions, features 
3. mysqlnd internals 
I. Type of returning values 
II.Memory usage 
3
1.about mysqlnd 
4
MySQL client libraries in PHP 
mysql_connect 
mysqli 
PDO 
Doctrine 
Eloquent 
ZendDB 
Aura.Sql 
ADOdb 
PEAR_MDB2 
PEAR_DB 
5
Those libraries use 
mysql/mysqli/pdo extensions 
6 
MySQL 
client 
libraries 
in PHP 
is 
/ 
use 
mysql 
extension 
mysqli 
extension 
pdo 
extension 
(mysql driver)
mysql / mysqli / 
pdo (mysql_driver) 
access MySQL server 
directly? 
7
No 
8
Communication between 
MySQL server and PHP 
• Network access (tcp, socket) 
• Encryption (SSL) or not 
• Analyze Wire Protocol 
• Compression (deflate) or not 
• Convert to (or from) PHP variables 
• Error Handring (Exception) 
9
Communication between 
MySQL server and PHP 
• Network access (tcp, socket) 
• Encryption (SSL) or not 
• Analyze Wire Protocol 
• Compression (deflate) or not 
• Convert to (or from) PHP variables 
• Error Handring (Exception) 
mysql/mysqli/pdo 
mysqlnd 
/ 
libmysql 
10
Application 
ADOdb Zend_DB Doctrine 
Chart of 
mysqli 
PEAR::MDB2 
mysql PDO 
mysqlnd 
libmysql 
Zend 
Engine 
PHP 
Script 
MySQL Server 
ref:http://d.hatena. 
ne.jp/do_aki/2011121 
4/1323831558 
11
2.libmysql vs mysqlnd 
12
Application 
ADOdb Zend_DB Doctrine 
Chart of 
mysqli 
PEAR::MDB2 
mysql PDO 
mysqlnd 
libmysql 
Zend 
Engine 
PHP 
Script 
MySQL Server 
ref:http://d.hatena. 
ne.jp/do_aki/2011121 
4/1323831558 
13
libmysql vs mysqlnd 
• libmysqlclient (Connector/C) 
– C library 
– Oracle (MySQL AB) 
– matured 
• mysqlnd 
– PHP extension 
– PHP Community (Andrey, Johannes, Ulf) 
– relatively recent 
14
compile options (example) 
• libmysql 
./configure  
--with-mysql=/usr  
--with-mysqli=/usr/bin/mysql_config  
--with-pdo-mysql=/usr 
• mysqlnd 
./configure  
--with-mysql=mysqlnd  
--with-mysqli=mysqlnd  
--with-pdo-mysql=mysqlnd 
15
mysqlnd and php versions 
• not available 
(only libmysql) 
before 
php 5.3 
• bundled but optional 
(default is libmysql) 
php 5.3 
series 
• default 
(libmysql is optional) 
php 5.4 
or later 
16 
*libmysql is not deprecated
which is used? 
• php –i or phpinfo() 
– mysql : Client API version 
– mysqli : Client API library version 
– pdo_mysql : Client API version 
mysqli : mysqli_get_client_info() 
PDO : $pdo->getAttribute( 
PDO::ATTR_CLIENT_VERSION) 
• include “mysqlnd” string or not 
17 
same as
pros 
• resolve license problems 
– libmysql: GPLv2 
with FOSS License Exception 
– mysqlnd : PHP license 
• not need build libmysqlclient before 
compile PHP 
– mysqlnd is bundled in php source code 
• “highly optimized for and tightly 
integrated into PHP” 
18
cons 
• not yet matured 
– libmysqlclient is widely used. 
at any os, as any language bindings… 
– mysqlnd is limited used. 
compared with libmysql 
19
incompatibilities 
• mysqlnd cannot use OLD_PASSWORD 
– not support MySQL server before 4.1 
– "mysqlnd cannot connect to MySQL 4.1+ using 
old authentication" 
• mysqlnd don’t read “my.cnf” 
– affect charset 
– no PDO::MYSQL_ATTR_READ_DEFAULT_FILE 
• Type of returning value is different 
– example: BIT type written by manual 
– It mentions later 
20
mysqlnd only 
• Asynchronous, non-blocking queries 
– MYSQLI_ASYNC (mysqli::query) 
– mysqli::reap_async_query 
– mysqli::poll 
• performance statistics 
– mysqli_get_client_stats 
– mysqli::get_connection_stats 
– http://php.net/manual/mysqlnd.stats.php 
• functions 
– mysqli_stmt::get_result 
– mysqli_result::fetch_all 
21
plugins 
• mysqlnd_ms 
– Replication and load balancing 
– http://pecl.php.net/package/mysqlnd_ms 
• mysqlnd_qc 
– Client side query cache 
– http://pecl.php.net/package/mysqlnd_qc 
• mysqlnd_uh 
– MySQLnd Userland Handler 
– http://pecl.php.net/package/mysqlnd_uh 
• mysqlnd_memcache 
– Translating SQL for InnoDB Memcached 
– http://pecl.php.net/package/mysqlnd_memcache 
22
plugins (removed?) 
• mysqlnd_mux 
– Simuler mysqlnd_ms? 
• mysqlnd_pscache 
– Prepared statement cache 
• mysqlnd_sip 
– SQL Injection Protection 
• mysqlnd_mc 
– Multi Connect 
23
3.mysqlnd internals 
24
Server side prepared statement 
type of prepared statement 
prepare SELECT age FROM user COM_PREPARE 
WHERE name = ? 
execute bind parameter "do_aki" COM_EXECUTE 
[Preventing SQL Injection] 
Client side prepared statement 
prepare 
SELECT age FROM user 
WHERE name = ? 
execute SELECT age FROM user COM_QUERY 
WHERE name = ‘do_aki’ 
25 
[default PDO settings]
Server side prepared statement 
prepared statement and protocols 
prepare SELECT age FROM user COM_PREPARE 
WHERE name = ? 
execute bind parameter "do_aki" COM_EXECUTE 
result set 29 (integer) 
binary protocol 
Client side prepared statement 
prepare 
SELECT age FROM user 
WHERE name = ? 
execute SELECT age FROM user COM_QUERY 
WHERE name = ‘do_aki’ 
result set “29” (string) 
text protocol 
26
I.Type of returning values 
27
example 
mysql> CREATE TABLE bits( 
id int, 
b bit(8), 
f float 
); 
mysql> insert into bits values 
(1, b'1010', 1.1); 
28
PDO 
text protocol 
$pdo->setAttribute( 
PDO::ATTR_EMULATE_PREPARES, true); 
$stmt = $pdo->prepare("SELECT * FROM bits"); 
$stmt->execute(); 
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC); 
binary protocol 
$pdo->setAttribute( 
PDO::ATTR_EMULATE_PREPARES, false); 
$stmt = $pdo->prepare("SELECT * FROM bits"); 
$stmt->execute(); 
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); 
29
PDO (libmysql) 
text protocol 
array(1) { 
[0]=> 
array(3) { 
[“id”]=> 
string(1) "1" 
[“b”]=> 
string(1) " 
" 
[“f”]=> 
string(3) "1.1" 
} 
} 
binary protocol 
array(1) { 
[0]=> 
array(3) { 
[“id”]=> 
string(1) "1" 
[“b”]=> 
string(1) " 
" 
[“f”]=> 
string(3) "1.1" 
} 
} 
“b” value is 0x10 30
PDO (mysqlnd) 
text protocol 
array(1) { 
[0]=> 
array(3) { 
[“id”]=> 
string(1) "1" 
[“b”]=> 
string(2) "10" 
[“f”]=> 
string(3) "1.1" 
} 
} 
binary protocol 
array(1) { 
[0]=> 
array(3) { 
[“id”]=> 
int(1) 
[“b”]=> 
int(10) 
[“f”]=> 
float(1.1000000238419) 
} 
} 
31
PDO 
text protocol 
array(3) { 
[“id”]=> 
string(1) "1" 
[“b”]=> 
string(1) “x10" 
[“f”]=> 
string(3) "1.1" 
} 
binary protocol 
array(3) { 
[“id”]=> 
string(1) "1" 
[“b”]=> 
string(1) “x10" 
[“f”]=> 
string(3) "1.1" 
} 
mysql 
nd 
array(3) { 
[“id”]=> 
string(1) "1" 
[“b”]=> 
string(2) "10" 
[“f”]=> 
string(3) "1.1" 
} 
array(3) { 
[“id”]=> 
int(1) 
[“b”]=> 
int(10) 
[“f”]=> 
float(1.1000000238419) 
} 
libmy 
sql 
32
mysqli 
text protocol 
$res = $mysqli->query("SELECT * FROM bits"); 
while($row = $res->fetch_assoc()) { 
$rows[] = $row; 
} 
var_dump($rows); 
binary protocol 
$stmt = $mysqli->prepare("SELECT * FROM bits"); 
$stmt->execute(); 
$stmt->bind_result($id, $b, $f); 
while($stmt->fetch()) { 
$rows[] = [‘id’=>$id, ‘b’=>$b, ‘f’=>$f]; 
} 
var_dump($rows); 
33
mysqli (libmysql/mysqlnd) 
text protocol 
array(1) { 
[0]=> 
array(3) { 
[0]=> 
string(1) "1" 
[1]=> 
string(2) "10" 
[2]=> 
string(3) "1.1" 
} 
} 
binary protocol 
array(1) { 
[0]=> 
array(3) { 
[0]=> 
int(1) 
[1]=> 
int(10) 
[2]=> 
float(1.1000000238419) 
} 
} 
34
II.Memory usage 
35
fetch (libmysql) 
$id $name $dt 
123 “do_aki” “2014-10-11 11:30:00” 
MySQL Server 
mysqli/pdo 
libmysql 
“id”:123, “name”:”do_aki”, 
“datetime”: “2014-10-11 11:30:00” 
36
fetch (mysqlnd) 
$id $name $dt 
mysqli/pdo 
3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” 
MySQL Server 
mysqlnd 
37
fetch (detail) 
zval* zval* zval* 
$id $name $dt 
zval zval zval MYSQLND_RES 
3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” 
MySQL Server 
MEMORY POOL (use malloc) 
38 
line 1 
internal buffers
fetch * N 
zval* zval* zval* 
$id $name $dt 
zval zval zval MYSQLND_RES 
3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” 
MySQL Server 
MEMORY POOL (use malloc) 
39 
line N 
internal buffers 
geting fat 
until statement 
released
solution for reduced memory 
• use MYSQLI_STORE_RESULT_COPY_DATA 
– fetch with copy 
– php >= 5.6.0 
– Not for PDO yet… 
– http://blog.ulf-wendel.de/2014/php-5- 
7-mysqlnd-memory-optimizations/ 
40
fetch with copy 
zval zval zval 
no internal buffers 
3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” 
MySQL Server 
MYSQLND_RES 
$id $name $dt 
41 
line N 
123 “do_aki” “2014-10-11 11:30:00”
but but but 
42
fetch (text protocol) 
$id $name $dt 
mysqli/pdo 
3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” 
MySQL Server 
mysqlnd 
43
fetch (binary protocol) 
$id $name $dt 
MySQL Server 
mysqlnd 
123 6 "do_aki” 2014-10-11 
11:30:00 
mysqli/pdo 
123 “do_aki” “2014-10-11 11:30:00” 
44
fetch (detail) 
zval* zval* zval* 
$id $name $dt 
zval zval zval MYSQLND_RES 
123 “do_aki” “2014-10-11 11:30:00” 
123 6 "do_aki” 2014-10-11 
11:30:00 
MySQL Server 
45 
line 1
fetch*N 
zval* zval* zval* 
$id $name $dt 
zval zval zval MYSQLND_RES 
111222333 “““dddooo___aaakkkiii””” ““2“2020101414-4-1-1010-0-1-1111 1 1 1111:1:3:3030:0:0:0000”0”” 
123 “do_aki” “2014-10-11 11:30:00” 
123 6 "do_aki” 2014-10-11 
11:30:00 
MySQL Server 
46 
line N
fetch*N 
zval* zval* zval* 
$id $name $dt 
zval zval zval MYSQLND_RES 
111222333 “““dddooo___aaakkkiii””” ““2“2020101414-4-1-1010-0-1-1111 1 1 1111:1:3:3030:0:0:0000”0”” 
123 “do_aki” “2014-10-11 11:30:00” 
123 6 "do_aki” 2014-10-11 
11:30:00 
MySQL Server 
47 
increases 
memory usage 
each fetch 
(until statement 
released) 
cannot use 
“MYSQLI_STORE_RE 
SULT_COPY_DATA” 
to prepared 
statement
Conclusion 
• I explain mysqlnd 
• mysqlnd and libmysql work 
differently 
• use mysqli if you want to full 
functions of mysqlnd 
• Prepared statement…… 
48
Thank you 
2014/10/11 
PHP Conference Japan 2014 
do_aki 
49

More Related Content

PDF
Redis - Usability and Use Cases
PDF
veracruz
PDF
Kickin' Ass with Cache-Fu (with notes)
 
PDF
Kickin' Ass with Cache-Fu (without notes)
 
PDF
Redis and its many use cases
PDF
4069180 Caching Performance Lessons From Facebook
PPTX
Webinar: Replication and Replica Sets
PDF
Redis SoCraTes 2014
Redis - Usability and Use Cases
veracruz
Kickin' Ass with Cache-Fu (with notes)
 
Kickin' Ass with Cache-Fu (without notes)
 
Redis and its many use cases
4069180 Caching Performance Lessons From Facebook
Webinar: Replication and Replica Sets
Redis SoCraTes 2014

What's hot (19)

PDF
WiredTiger In-Memory vs WiredTiger B-Tree
KEY
Redis in Practice
PDF
Memcached Presentation @757rb
PPTX
Webinar: Replication and Replica Sets
PDF
Undelete (and more) rows from the binary log
ODP
High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani
PPT
Introducing PHP Data Objects
PDF
What's New in the PHP Driver
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
ODP
Beyond PHP - it's not (just) about the code
PDF
Datagrids with Symfony 2, Backbone and Backgrid
PDF
Mongodb workshop
PDF
Nko workshop - node js crud & deploy
PPTX
My sql administration
PPTX
On secure application of PHP wrappers
PDF
PDF
How to stand on the shoulders of giants
PDF
PHP Data Objects
PDF
ROracle
WiredTiger In-Memory vs WiredTiger B-Tree
Redis in Practice
Memcached Presentation @757rb
Webinar: Replication and Replica Sets
Undelete (and more) rows from the binary log
High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani
Introducing PHP Data Objects
What's New in the PHP Driver
From mysql to MongoDB(MongoDB2011北京交流会)
Beyond PHP - it's not (just) about the code
Datagrids with Symfony 2, Backbone and Backgrid
Mongodb workshop
Nko workshop - node js crud & deploy
My sql administration
On secure application of PHP wrappers
How to stand on the shoulders of giants
PHP Data Objects
ROracle
Ad

Viewers also liked (14)

PPTX
『例えば、PHPを避ける』以降PHPはどれだけ安全になったか
PPTX
N対1 レプリケーション + Optimizer Hint
PDF
WP-CLI (WordBench Sendai 20140628)
PPTX
PHP AST 徹底解説(補遺)
PPTX
Writing php extensions in golang
PPTX
php7's ast
PPTX
signal の話 或いは Zend Signals とは何か
PDF
Asynchronous PHP and Real-time Messaging
PDF
PHP classの教室
PDF
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
PDF
今さらだけどMySQLとライセンス
PPTX
PHP AST 徹底解説
ODP
The promise of asynchronous PHP
PDF
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス
『例えば、PHPを避ける』以降PHPはどれだけ安全になったか
N対1 レプリケーション + Optimizer Hint
WP-CLI (WordBench Sendai 20140628)
PHP AST 徹底解説(補遺)
Writing php extensions in golang
php7's ast
signal の話 或いは Zend Signals とは何か
Asynchronous PHP and Real-time Messaging
PHP classの教室
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
今さらだけどMySQLとライセンス
PHP AST 徹底解説
The promise of asynchronous PHP
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス
Ad

Similar to 20141011 mastering mysqlnd (20)

KEY
Mysqlnd uh
PDF
Mysqlnd, an unknown powerful PHP extension
PDF
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
PPTX
Compare mysql5.1.50 mysql5.5.8
KEY
Intro to PECL/mysqlnd_ms (4/7/2011)
PDF
Perl Programming - 04 Programming Database
PDF
My SQL 101
PDF
Service discovery and configuration provisioning
ODP
Built-in query caching for all PHP MySQL extensions/APIs
PDF
The powerful toolset of the go-mysql library
PPT
Php classes in mumbai
KEY
dotCloud and go
KEY
Scaling php applications with redis
PDF
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
PDF
veracruz
PDF
veracruz
PDF
veracruz
PPT
PHP - Getting good with MySQL part II
PDF
My Sq Ldb Tut
PDF
All Things Open 2016 -- Database Programming for Newbies
Mysqlnd uh
Mysqlnd, an unknown powerful PHP extension
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
Compare mysql5.1.50 mysql5.5.8
Intro to PECL/mysqlnd_ms (4/7/2011)
Perl Programming - 04 Programming Database
My SQL 101
Service discovery and configuration provisioning
Built-in query caching for all PHP MySQL extensions/APIs
The powerful toolset of the go-mysql library
Php classes in mumbai
dotCloud and go
Scaling php applications with redis
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
veracruz
veracruz
veracruz
PHP - Getting good with MySQL part II
My Sq Ldb Tut
All Things Open 2016 -- Database Programming for Newbies

More from do_aki (20)

PPTX
Tritonn から Elasticsearch への移行話
PPTX
php-src の歩き方
PPTX
PHP と SAPI と ZendEngine3 と
PPTX
PHPとシグナル、その裏側
PPTX
再考:列挙型
PPTX
20150212 プレゼンテーションzen
PPTX
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」
PPTX
20141017 introduce razor
PPTX
php in ruby
PPTX
PHP から Groonga を使うにはこんなコードになるよ!
PPTX
N:1 Replication meets MHA
PDF
Php radomize
PPTX
php and sapi and zendengine2 and...
PPTX
セキュアそうでセキュアじゃない少しセキュアな気分になれるmysql_config_editor
PPTX
マスタN対スレーブ1レプリケーションの作り方 ~あれから~
PPTX
Immortal
PPTX
Excel is image viewer
PDF
A bridge between php and ruby
PDF
Ruby and comparison_and...php
PPTX
Sore php
Tritonn から Elasticsearch への移行話
php-src の歩き方
PHP と SAPI と ZendEngine3 と
PHPとシグナル、その裏側
再考:列挙型
20150212 プレゼンテーションzen
MySQL Casual Talks 7 「N:1 レプリケーション ~進捗どうですか?~」
20141017 introduce razor
php in ruby
PHP から Groonga を使うにはこんなコードになるよ!
N:1 Replication meets MHA
Php radomize
php and sapi and zendengine2 and...
セキュアそうでセキュアじゃない少しセキュアな気分になれるmysql_config_editor
マスタN対スレーブ1レプリケーションの作り方 ~あれから~
Immortal
Excel is image viewer
A bridge between php and ruby
Ruby and comparison_and...php
Sore php

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Spectroscopy.pptx food analysis technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Electronic commerce courselecture one. Pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Chapter 3 Spatial Domain Image Processing.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
Agricultural_Statistics_at_a_Glance_2022_0.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectroscopy.pptx food analysis technology

20141011 mastering mysqlnd

  • 1. 2014/10/11 PHP Conference Japan 2014 do_aki 1 updated 2014-10-21
  • 3. agenda 1. about mysqlnd – position, role 2. libmysql vs mysqlnd – functions, features 3. mysqlnd internals I. Type of returning values II.Memory usage 3
  • 5. MySQL client libraries in PHP mysql_connect mysqli PDO Doctrine Eloquent ZendDB Aura.Sql ADOdb PEAR_MDB2 PEAR_DB 5
  • 6. Those libraries use mysql/mysqli/pdo extensions 6 MySQL client libraries in PHP is / use mysql extension mysqli extension pdo extension (mysql driver)
  • 7. mysql / mysqli / pdo (mysql_driver) access MySQL server directly? 7
  • 9. Communication between MySQL server and PHP • Network access (tcp, socket) • Encryption (SSL) or not • Analyze Wire Protocol • Compression (deflate) or not • Convert to (or from) PHP variables • Error Handring (Exception) 9
  • 10. Communication between MySQL server and PHP • Network access (tcp, socket) • Encryption (SSL) or not • Analyze Wire Protocol • Compression (deflate) or not • Convert to (or from) PHP variables • Error Handring (Exception) mysql/mysqli/pdo mysqlnd / libmysql 10
  • 11. Application ADOdb Zend_DB Doctrine Chart of mysqli PEAR::MDB2 mysql PDO mysqlnd libmysql Zend Engine PHP Script MySQL Server ref:http://d.hatena. ne.jp/do_aki/2011121 4/1323831558 11
  • 13. Application ADOdb Zend_DB Doctrine Chart of mysqli PEAR::MDB2 mysql PDO mysqlnd libmysql Zend Engine PHP Script MySQL Server ref:http://d.hatena. ne.jp/do_aki/2011121 4/1323831558 13
  • 14. libmysql vs mysqlnd • libmysqlclient (Connector/C) – C library – Oracle (MySQL AB) – matured • mysqlnd – PHP extension – PHP Community (Andrey, Johannes, Ulf) – relatively recent 14
  • 15. compile options (example) • libmysql ./configure --with-mysql=/usr --with-mysqli=/usr/bin/mysql_config --with-pdo-mysql=/usr • mysqlnd ./configure --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd 15
  • 16. mysqlnd and php versions • not available (only libmysql) before php 5.3 • bundled but optional (default is libmysql) php 5.3 series • default (libmysql is optional) php 5.4 or later 16 *libmysql is not deprecated
  • 17. which is used? • php –i or phpinfo() – mysql : Client API version – mysqli : Client API library version – pdo_mysql : Client API version mysqli : mysqli_get_client_info() PDO : $pdo->getAttribute( PDO::ATTR_CLIENT_VERSION) • include “mysqlnd” string or not 17 same as
  • 18. pros • resolve license problems – libmysql: GPLv2 with FOSS License Exception – mysqlnd : PHP license • not need build libmysqlclient before compile PHP – mysqlnd is bundled in php source code • “highly optimized for and tightly integrated into PHP” 18
  • 19. cons • not yet matured – libmysqlclient is widely used. at any os, as any language bindings… – mysqlnd is limited used. compared with libmysql 19
  • 20. incompatibilities • mysqlnd cannot use OLD_PASSWORD – not support MySQL server before 4.1 – "mysqlnd cannot connect to MySQL 4.1+ using old authentication" • mysqlnd don’t read “my.cnf” – affect charset – no PDO::MYSQL_ATTR_READ_DEFAULT_FILE • Type of returning value is different – example: BIT type written by manual – It mentions later 20
  • 21. mysqlnd only • Asynchronous, non-blocking queries – MYSQLI_ASYNC (mysqli::query) – mysqli::reap_async_query – mysqli::poll • performance statistics – mysqli_get_client_stats – mysqli::get_connection_stats – http://php.net/manual/mysqlnd.stats.php • functions – mysqli_stmt::get_result – mysqli_result::fetch_all 21
  • 22. plugins • mysqlnd_ms – Replication and load balancing – http://pecl.php.net/package/mysqlnd_ms • mysqlnd_qc – Client side query cache – http://pecl.php.net/package/mysqlnd_qc • mysqlnd_uh – MySQLnd Userland Handler – http://pecl.php.net/package/mysqlnd_uh • mysqlnd_memcache – Translating SQL for InnoDB Memcached – http://pecl.php.net/package/mysqlnd_memcache 22
  • 23. plugins (removed?) • mysqlnd_mux – Simuler mysqlnd_ms? • mysqlnd_pscache – Prepared statement cache • mysqlnd_sip – SQL Injection Protection • mysqlnd_mc – Multi Connect 23
  • 25. Server side prepared statement type of prepared statement prepare SELECT age FROM user COM_PREPARE WHERE name = ? execute bind parameter "do_aki" COM_EXECUTE [Preventing SQL Injection] Client side prepared statement prepare SELECT age FROM user WHERE name = ? execute SELECT age FROM user COM_QUERY WHERE name = ‘do_aki’ 25 [default PDO settings]
  • 26. Server side prepared statement prepared statement and protocols prepare SELECT age FROM user COM_PREPARE WHERE name = ? execute bind parameter "do_aki" COM_EXECUTE result set 29 (integer) binary protocol Client side prepared statement prepare SELECT age FROM user WHERE name = ? execute SELECT age FROM user COM_QUERY WHERE name = ‘do_aki’ result set “29” (string) text protocol 26
  • 27. I.Type of returning values 27
  • 28. example mysql> CREATE TABLE bits( id int, b bit(8), f float ); mysql> insert into bits values (1, b'1010', 1.1); 28
  • 29. PDO text protocol $pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, true); $stmt = $pdo->prepare("SELECT * FROM bits"); $stmt->execute(); var_dump($stmt->fetchAll(PDO::FETCH_ASSOC); binary protocol $pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, false); $stmt = $pdo->prepare("SELECT * FROM bits"); $stmt->execute(); var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); 29
  • 30. PDO (libmysql) text protocol array(1) { [0]=> array(3) { [“id”]=> string(1) "1" [“b”]=> string(1) " " [“f”]=> string(3) "1.1" } } binary protocol array(1) { [0]=> array(3) { [“id”]=> string(1) "1" [“b”]=> string(1) " " [“f”]=> string(3) "1.1" } } “b” value is 0x10 30
  • 31. PDO (mysqlnd) text protocol array(1) { [0]=> array(3) { [“id”]=> string(1) "1" [“b”]=> string(2) "10" [“f”]=> string(3) "1.1" } } binary protocol array(1) { [0]=> array(3) { [“id”]=> int(1) [“b”]=> int(10) [“f”]=> float(1.1000000238419) } } 31
  • 32. PDO text protocol array(3) { [“id”]=> string(1) "1" [“b”]=> string(1) “x10" [“f”]=> string(3) "1.1" } binary protocol array(3) { [“id”]=> string(1) "1" [“b”]=> string(1) “x10" [“f”]=> string(3) "1.1" } mysql nd array(3) { [“id”]=> string(1) "1" [“b”]=> string(2) "10" [“f”]=> string(3) "1.1" } array(3) { [“id”]=> int(1) [“b”]=> int(10) [“f”]=> float(1.1000000238419) } libmy sql 32
  • 33. mysqli text protocol $res = $mysqli->query("SELECT * FROM bits"); while($row = $res->fetch_assoc()) { $rows[] = $row; } var_dump($rows); binary protocol $stmt = $mysqli->prepare("SELECT * FROM bits"); $stmt->execute(); $stmt->bind_result($id, $b, $f); while($stmt->fetch()) { $rows[] = [‘id’=>$id, ‘b’=>$b, ‘f’=>$f]; } var_dump($rows); 33
  • 34. mysqli (libmysql/mysqlnd) text protocol array(1) { [0]=> array(3) { [0]=> string(1) "1" [1]=> string(2) "10" [2]=> string(3) "1.1" } } binary protocol array(1) { [0]=> array(3) { [0]=> int(1) [1]=> int(10) [2]=> float(1.1000000238419) } } 34
  • 36. fetch (libmysql) $id $name $dt 123 “do_aki” “2014-10-11 11:30:00” MySQL Server mysqli/pdo libmysql “id”:123, “name”:”do_aki”, “datetime”: “2014-10-11 11:30:00” 36
  • 37. fetch (mysqlnd) $id $name $dt mysqli/pdo 3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” MySQL Server mysqlnd 37
  • 38. fetch (detail) zval* zval* zval* $id $name $dt zval zval zval MYSQLND_RES 3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” MySQL Server MEMORY POOL (use malloc) 38 line 1 internal buffers
  • 39. fetch * N zval* zval* zval* $id $name $dt zval zval zval MYSQLND_RES 3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” MySQL Server MEMORY POOL (use malloc) 39 line N internal buffers geting fat until statement released
  • 40. solution for reduced memory • use MYSQLI_STORE_RESULT_COPY_DATA – fetch with copy – php >= 5.6.0 – Not for PDO yet… – http://blog.ulf-wendel.de/2014/php-5- 7-mysqlnd-memory-optimizations/ 40
  • 41. fetch with copy zval zval zval no internal buffers 3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” MySQL Server MYSQLND_RES $id $name $dt 41 line N 123 “do_aki” “2014-10-11 11:30:00”
  • 43. fetch (text protocol) $id $name $dt mysqli/pdo 3 “123” 6 "do_aki” 19 “2014-10-11 11:30:00” MySQL Server mysqlnd 43
  • 44. fetch (binary protocol) $id $name $dt MySQL Server mysqlnd 123 6 "do_aki” 2014-10-11 11:30:00 mysqli/pdo 123 “do_aki” “2014-10-11 11:30:00” 44
  • 45. fetch (detail) zval* zval* zval* $id $name $dt zval zval zval MYSQLND_RES 123 “do_aki” “2014-10-11 11:30:00” 123 6 "do_aki” 2014-10-11 11:30:00 MySQL Server 45 line 1
  • 46. fetch*N zval* zval* zval* $id $name $dt zval zval zval MYSQLND_RES 111222333 “““dddooo___aaakkkiii””” ““2“2020101414-4-1-1010-0-1-1111 1 1 1111:1:3:3030:0:0:0000”0”” 123 “do_aki” “2014-10-11 11:30:00” 123 6 "do_aki” 2014-10-11 11:30:00 MySQL Server 46 line N
  • 47. fetch*N zval* zval* zval* $id $name $dt zval zval zval MYSQLND_RES 111222333 “““dddooo___aaakkkiii””” ““2“2020101414-4-1-1010-0-1-1111 1 1 1111:1:3:3030:0:0:0000”0”” 123 “do_aki” “2014-10-11 11:30:00” 123 6 "do_aki” 2014-10-11 11:30:00 MySQL Server 47 increases memory usage each fetch (until statement released) cannot use “MYSQLI_STORE_RE SULT_COPY_DATA” to prepared statement
  • 48. Conclusion • I explain mysqlnd • mysqlnd and libmysql work differently • use mysqli if you want to full functions of mysqlnd • Prepared statement…… 48
  • 49. Thank you 2014/10/11 PHP Conference Japan 2014 do_aki 49