Creation Zone

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Monday, 26 January 2009

Demonstrating the Features of MySQL Native Driver for PHP, mysqlnd

Posted on 01:06 by Unknown
Support for Persistent Connections

ext/mysqli does not support persistent connections when built with libmysql. However ext/mysqli does support persistent connections when built with mysqlnd. To establish a persistent connection with the MySQL server using ext/mysqli and mysqlnd, prepend the database host with the string "p:" (p stands for persistent) as shown below.

$host="p:localhost";
$port=3306;
$socket="/tmp/mysql.sock";
$user="root";
$password="admin";
$dbname="test";

$cxn = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());


ext/mysql, ext/mysqli and PDO_MySQL support persistent connections when built with mysqlnd.

The new API call mysqli_fetch_all()

mysqlnd extends the ext/mysqli API with one brand new method, mysqli_fetch_all().

mysqli_fetch_all() fetches all result rows and return the result set as an associative array, a numeric array, or both. The method signature is shown below for both procedural as well as object oriented style of programming.

Procedural style:
 mixed mysqli_fetch_all (mysqli_result $result [, int $resulttype])

Object oriented style:
 mixed mysqli_result::fetch_all ([int $resulttype])

where: $result is a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result(), and $resulttype is an optional constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH. Defaults to MYSQLI_NUM.

Because mysqli_fetch_all() returns all the rows as an array in a single step, it may consume more memory than some of its counterparts like mysqli_fetch_array(). mysqli_fetch_array() returns one row at a time from the result set, hence consumes less memory relative to mysqli_fetch_array(). Besides, if you need to iterate over the result set, you may need a foreach() loop and this approach might be little slower compared to the result set retrieval using mysqli_fetch_array(). Hence consider using mysqli_fetch_all() only in those situations where the fetched result set will be sent to another layer for post processing. If you have to process the fetched result set in the same layer with the help of iterators, then the benefit of using the mysqli_fetch_all() method might be minimal, if there is any.

Statistical Data Collection

mysqlnd collects a lot of statistics which you can use to tune your application. mysqlnd enhances ext/mysqli API with three mysqli_get_XX_stats() methods for easy monitoring and to simplify the bottleneck analysis. For example, using a combination of mysqli_get_XX_stats() methods, one can easily identify a PHP client script that is opening more database connections than it needs or selecting more rows than it consumes.

Accessing Client Statistics:

To access per process client statistics, simply call mysqli_get_client_stats() with no arguments. Similarly to access client statistics per connection, call mysqli_get_connection_stats() with the database connection handle as the argument. Both of these methods return an associated array with the name of the statistic parameter as the key and the corresponding data as the value.

Alternatively per process client statistics can be accessed by calling the phpinfo() method.

The above methods return statistics like bytes_sent, bytes_received to represent the number of bytes sent to and received from the MySQL server, result_set_queries to show the number of queries which generated a result set, buffered_sets, unbuffered_sets to show the number of buffered and unbuffered result sets for the queries generating a result set but not run as a prepared statement. rows_fetched_from_server_normal shows the number of rows that have been fetched from the server using buffered and unbuffered result sets. rows_buffered_from_client_normal shows the number of rows fetched from the server and buffered on the client-side, and rows_skipped_normal shows the number of rows generated by the server but not read from the client.

Accessing Zval Cache Statistics:

mysqlnd collects statistics from its internal zval cache, that you can access by calling mysqli_get_cache_stat() method. This method returns an associative array with the name of the statistic as the key and the corresponding data as the value. The zval cache statistics might be useful to tweak zval cahe related php.ini settings for better performance.

Sample PHP Script Demonstrating mysqlnd's Features

The following sample PHP script demonstrates how to:
  • establish persistent connections
  • use mysqli_fetch_all() to fetch and display the result set
  • access client, connection and zval cache statistics using mysqli_get_client_stats(), mysqli_get_connection_stats() and
    mysqli_get_cache_stat()
    methods

The code sample in this tutorial try to retrieve the data from the City table in the MySQL test database. The table structure and the data from the City table are shown below by using the mysql client. MySQL server is running on the default port 3306.

bash# mysql -u root -p
Enter password: admin
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.24-rc-standard Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> USE test;
Database changed

mysql> DESCRIBE City;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| CityName | varchar(30) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
1 row in set (0.07 sec)

mysql> SELECT * FROM City;
+--------------------+
| CityName |
+--------------------+
| Hyderabad, India |
| San Francisco, USA |
| Sydney, Australia |
+--------------------+
3 rows in set (0.17 sec)


The main purpose of the following example is only to illustrate the syntactical use of the new features of mysqlnd. The sample code does not represent any real world scenarios.

bash# cat PHPmysqliClientmysqlnd.php

<?php

/* create a persistent connection to the MySQL server */
$cxn = new mysqli("p:localhost", "root", "admin", "test", 3306, "/tmp/mysql.sock")
or die ('Could not connect to the database server' . mysqli_connect_error());

$query = "SELECT * FROM City";

/* execute the query */
if ($cxn->real_query ($query)) {

/* initiate the result set retrieval */
if ($result = $cxn->store_result()) {

/* find the number of rows in the result set */
$nrows = $result->num_rows;

echo "\nRetrieved $nrows row(s).\n\n";
echo "CityName\n--------\n";

$all_rows = $result->fetch_all(MYSQLI_ASSOC);

for($i = 0; $i < count($all_rows); $i++) {
echo $all_rows[$i][CityName] . "\n";
}
}

/* close the result set */
$result->close();
}

echo "\n\nClient Statistics After One Query\n---------------------------------";
$client_stats = mysqli_get_client_stats();
#var_dump($client_stats);
foreach ($client_stats as $key=>$value) {
if ($value > 0) {
echo "\n$key : $value";
}
}

echo "\n\nStatistics for Connection #1\n----------------------------";
$conn_stats = mysqli_get_connection_stats($cxn);
#var_dump($conn_stats);
foreach ($conn_stats as $key=>$value) {
if ($value > 0) {
echo "\n$key : $value";
}
}

echo "\n\nCache Statistics After One Query\n--------------------------------";
$cache_stats = mysqli_get_cache_stats();
#var_dump($cache_stats);
foreach ($cache_stats as $key=>$value) {
if ($value > 0) {
echo "\n$key : $value";
}
}

echo "\n\n=================================\n\n";
echo "\nEstablishing connection #2 to the MySQL server ..\n\n";

/* create a non-persistent connection to the MySQL server */
$cxn2 = new mysqli("localhost", "root", "admin", "mysql", 3306, "/tmp/mysql.sock")
or die ('Could not connect to the database server' . mysqli_connect_error());

$query = "SELECT Host, User FROM user";

/* execute the query */
if ($cxn2->real_query ($query)) {

/* initiate the result set retrieval */
if ($result = $cxn2->store_result()) {

/* find the number of rows in the result set */
$nrows = $result->num_rows;
echo "\nRetrieved $nrows row(s).\n\n";

echo "Host\t\tUser\n----\t\t----\n";

$all_rows = $result->fetch_all(MYSQLI_ASSOC);

for($i = 0; $i < count($all_rows); $i++) {
echo $all_rows[$i][Host] . "\t" . $all_rows[$i][User] . "\n";
}
}

/* close the result set */
$result->close();
}

echo "\n\nClient Statistics After Two Queries\n-----------------------------------";
$client_stats = mysqli_get_client_stats();
#var_dump($client_stats);
foreach ($client_stats as $key=>$value) {
if ($value > 0) {
echo "\n$key : $value";
}
}

echo "\n\nStatistics for Connection #2\n----------------------------";
$conn_stats = mysqli_get_connection_stats($cxn2);
#var_dump($conn_stats);
foreach ($conn_stats as $key=>$value) {
if ($value > 0) {
echo "\n$key : $value";
}
}

echo "\n\nCache Statistics After Two Queries\n----------------------------------";
$cache_stats = mysqli_get_cache_stats();
#var_dump($cache_stats);
foreach ($cache_stats as $key=>$value) {
if ($value > 0) {
echo "\n$key : $value";
}
}

echo "\n";

//phpinfo();

/* close the database connections */
$cxn->close();
$cxn2->close();

?>

bash# /export/home/php53/bin/php PHPmysqliClientmysqlnd.php

Retrieved 3 row(s).

CityName
--------
Hyderabad, India
San Francisco, USA
Sydney, Australia


Client Statistics After One Query
---------------------------------
bytes_sent : 90
bytes_received : 222
packets_sent : 2
packets_received : 9
protocol_overhead_in : 36
protocol_overhead_out : 8
bytes_received_ok_packet : 11
bytes_received_eof_packet : 9
bytes_received_rset_header_packet : 5
bytes_received_rset_field_meta_packet : 54
bytes_received_rset_row_packet : 70
packets_sent_command : 1
packets_received_ok : 1
packets_received_eof : 1
packets_received_rset_header : 1
packets_received_rset_field_meta : 1
packets_received_rset_row : 4
result_set_queries : 1
buffered_sets : 1
rows_fetched_from_server_normal : 3
rows_buffered_from_client_normal : 3
rows_fetched_from_client_normal_buffered : 3
rows_skipped_normal : 3
copy_on_write_performed : 3
connect_success : 1
active_connections : 1
active_persistent_connections : 1
explicit_free_result : 1
mem_erealloc_count : 1
mem_efree_count : 2
mem_realloc_count : 1
proto_text_fetched_string : 3

Statistics for Connection #1
----------------------------
bytes_sent : 90
bytes_received : 222
packets_sent : 2
packets_received : 9
protocol_overhead_in : 36
protocol_overhead_out : 8
bytes_received_ok_packet : 11
bytes_received_eof_packet : 9
bytes_received_rset_header_packet : 5
bytes_received_rset_field_meta_packet : 54
bytes_received_rset_row_packet : 70
packets_sent_command : 1
packets_received_ok : 1
packets_received_eof : 1
packets_received_rset_header : 1
packets_received_rset_field_meta : 1
packets_received_rset_row : 4
result_set_queries : 1
buffered_sets : 1
rows_fetched_from_server_normal : 3
rows_buffered_from_client_normal : 3
rows_skipped_normal : 3
connect_success : 1
active_connections : 1
active_persistent_connections : 1
explicit_free_result : 1
proto_text_fetched_string : 3

Cache Statistics After One Query
--------------------------------
put_misses : 3
get_hits : 3
size : 2000
free_items : 1997
references : 3

=================================


Establishing connection #2 to the MySQL server ..


Retrieved 5 row(s).

Host User
---- ----
127.0.0.1 root
localhost
localhost root
unknown
unknown root


Client Statistics After Two Queries
-----------------------------------
bytes_sent : 190
bytes_received : 501
packets_sent : 4
packets_received : 21
protocol_overhead_in : 84
protocol_overhead_out : 16
bytes_received_ok_packet : 22
bytes_received_eof_packet : 18
bytes_received_rset_header_packet : 10
bytes_received_rset_field_meta_packet : 148
bytes_received_rset_row_packet : 157
packets_sent_command : 2
packets_received_ok : 2
packets_received_eof : 2
packets_received_rset_header : 2
packets_received_rset_field_meta : 3
packets_received_rset_row : 10
result_set_queries : 2
buffered_sets : 2
rows_fetched_from_server_normal : 8
rows_buffered_from_client_normal : 8
rows_fetched_from_client_normal_buffered : 8
rows_skipped_normal : 8
copy_on_write_performed : 13
connect_success : 2
active_connections : 2
active_persistent_connections : 1
explicit_free_result : 2
mem_erealloc_count : 1
mem_efree_count : 2
mem_realloc_count : 4
proto_text_fetched_string : 13
Statistics for Connection #2
----------------------------
bytes_sent : 100
bytes_received : 279
packets_sent : 2
packets_received : 12
protocol_overhead_in : 48
protocol_overhead_out : 8
bytes_received_ok_packet : 11
bytes_received_eof_packet : 9
bytes_received_rset_header_packet : 5
bytes_received_rset_field_meta_packet : 94
bytes_received_rset_row_packet : 87
packets_sent_command : 1
packets_received_ok : 1
packets_received_eof : 1
packets_received_rset_header : 1
packets_received_rset_field_meta : 2
packets_received_rset_row : 6
result_set_queries : 1
buffered_sets : 1
rows_fetched_from_server_normal : 5
rows_buffered_from_client_normal : 5
rows_skipped_normal : 5
connect_success : 1
active_connections : 1
explicit_free_result : 1
proto_text_fetched_string : 10

Cache Statistics After Two Queries
----------------------------------
put_misses : 13
get_hits : 13
size : 2000
free_items : 1987
references : 4


Before concluding, be adviced that some of the experimental functions that are available with ext/mysqli and libmysql are not available with ext/mysqli and mysqlnd. eg., mysqli_embedded_*(), mysqli_*rpl*_()

Related Blog Post:
MySQL Native Driver for PHP, mysqlnd

Acknowledgments
Andrey Hristov & Ulf Wendel, Sun-MySQL AB

(Originally posted on blogs.sun.com at:
http://blogs.sun.com/mandalika/entry/demonstrating_the_features_of_mysql
)
_______________
Technorati Tags:
 PHP |  MySQL |  mysqlnd
Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • *nix: Workaround to cannot find zipfile directory in one of file.zip or file.zip.zip ..
    Symptom: You are trying to extract the archived files off of a huge (any file with size > 2 GB or 4GB, depending on the OS) ZIP file with...
  • JDS: Installing Sun Java Desktop System 2.0
    This document will guide you through the process of installing JDS 2.0 on a PC from integrated CDROM images Requirements I...
  • Linux: Installing Source RPM (SRPM) package
    RPM stands for RedHat Package Manager. RPM is a system for installing and managing software & most common software package manager used ...
  • Solaris: malloc Vs mtmalloc
    Performance of Single Vs Multi-threaded application Memory allocation performance in single and multithreaded environments is an important a...
  • C/C++: Printing Stack Trace with printstack() on Solaris
    libc on Solaris 9 and later, provides a useful function called printstack , to print a symbolic stack trace to the specified file descripto...
  • Installing MySQL 5.0.51b from the Source Code on Sun Solaris
    Building and installing the MySQL server from the source code is relatively very easy when compared to many other OSS applications. At least...
  • Oracle Apps on T2000: ORA-04020 during Autoinvoice
    The goal of this brief blog post is to provide a quick solution to all Sun-Oracle customers who may run into a deadlock when a handful of th...
  • Siebel Connection Broker Load Balancing Algorithm
    Siebel server architecture supports spawning multiple application object manager processes. The Siebel Connection Broker, SCBroker, tries to...
  • 64-bit dbx: internal error: signal SIGBUS (invalid address alignment)
    The other day I was chasing some lock contention issue with a 64-bit application running on Solaris 10 Update 1; and stumbled with an unexpe...
  • Oracle 10gR2/Solaris x64: Fixing ORA-20000: Oracle Text errors
    First, some facts: * Oracle Applications 11.5.10 (aka E-Business Suite 11 i ) database is now supported on Solaris 10 for x86-64 architectur...

Categories

  • 80s music playlist
  • bandwidth iperf network solaris
  • best
  • black friday
  • breakdown database groups locality oracle pmap sga solaris
  • buy
  • deal
  • ebiz ebs hrms oracle payroll
  • emca oracle rdbms database ORA-01034
  • friday
  • Garmin
  • generic+discussion software installer
  • GPS
  • how-to solaris mmap
  • impdp ora-01089 oracle rdbms solaris tips upgrade workarounds zombie
  • Magellan
  • music
  • Navigation
  • OATS Oracle
  • Oracle Business+Intelligence Analytics Solaris SPARC T4
  • oracle database flashback FDA
  • Oracle Database RDBMS Redo Flash+Storage
  • oracle database solaris
  • oracle database solaris resource manager virtualization consolidation
  • Oracle EBS E-Business+Suite SPARC SuperCluster Optimized+Solution
  • Oracle EBS E-Business+Suite Workaround Tip
  • oracle lob bfile blob securefile rdbms database tips performance clob
  • oracle obiee analytics presentation+services
  • Oracle OID LDAP ADS
  • Oracle OID LDAP SPARC T5 T5-2 Benchmark
  • oracle pls-00201 dbms_system
  • oracle siebel CRM SCBroker load+balancing
  • Oracle Siebel Sun SPARC T4 Benchmark
  • Oracle Siebel Sun SPARC T5 Benchmark T5-2
  • Oracle Solaris
  • Oracle Solaris Database RDBMS Redo Flash F40 AWR
  • oracle solaris rpc statd RPC troubleshooting
  • oracle solaris svm solaris+volume+manager
  • Oracle Solaris Tips
  • oracle+solaris
  • RDC
  • sale
  • Smartphone Samsung Galaxy S2 Phone+Shutter Tip Android ICS
  • solaris oracle database fmw weblogic java dfw
  • SuperCluster Oracle Database RDBMS RAC Solaris Zones
  • tee
  • thanksgiving sale
  • tips
  • TomTom
  • windows

Blog Archive

  • ►  2013 (16)
    • ►  December (3)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (2)
    • ►  January (1)
  • ►  2012 (14)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (2)
  • ►  2011 (15)
    • ►  December (2)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (2)
    • ►  July (1)
    • ►  May (2)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2010 (19)
    • ►  December (3)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (5)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ▼  2009 (25)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
    • ►  July (2)
    • ►  June (1)
    • ►  May (2)
    • ►  April (3)
    • ►  March (1)
    • ►  February (5)
    • ▼  January (4)
      • PHP: Memory savings with mysqlnd
      • Demonstrating the Features of MySQL Native Driver ...
      • MySQL Native Driver for PHP, mysqlnd
      • Xorg Screen Resolution Woes on SXCE
  • ►  2008 (34)
    • ►  December (2)
    • ►  November (2)
    • ►  October (2)
    • ►  September (1)
    • ►  August (4)
    • ►  July (2)
    • ►  June (3)
    • ►  May (3)
    • ►  April (2)
    • ►  March (5)
    • ►  February (4)
    • ►  January (4)
  • ►  2007 (33)
    • ►  December (2)
    • ►  November (4)
    • ►  October (2)
    • ►  September (5)
    • ►  August (3)
    • ►  June (2)
    • ►  May (3)
    • ►  April (5)
    • ►  March (3)
    • ►  February (1)
    • ►  January (3)
  • ►  2006 (40)
    • ►  December (2)
    • ►  November (6)
    • ►  October (2)
    • ►  September (2)
    • ►  August (1)
    • ►  July (2)
    • ►  June (2)
    • ►  May (4)
    • ►  April (5)
    • ►  March (5)
    • ►  February (3)
    • ►  January (6)
  • ►  2005 (72)
    • ►  December (5)
    • ►  November (2)
    • ►  October (6)
    • ►  September (5)
    • ►  August (5)
    • ►  July (10)
    • ►  June (8)
    • ►  May (9)
    • ►  April (6)
    • ►  March (6)
    • ►  February (5)
    • ►  January (5)
  • ►  2004 (36)
    • ►  December (1)
    • ►  November (5)
    • ►  October (12)
    • ►  September (18)
Powered by Blogger.

About Me

Unknown
View my complete profile