<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gergely Hodicska &#187; mysql</title>
	<atom:link href="http://blog.felho.hu/stock/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.felho.hu</link>
	<description>Random secrets of PHP, web development</description>
	<lastBuildDate>Fri, 30 Nov 2007 10:16:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What is new in PHP 5.3 &#8211; part 3: mysqlnd</title>
		<link>http://blog.felho.hu/what-is-new-in-php-53-part-3-mysqlnd.html</link>
		<comments>http://blog.felho.hu/what-is-new-in-php-53-part-3-mysqlnd.html#comments</comments>
		<pubDate>Sat, 17 Nov 2007 20:49:40 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[mysqlnd]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/what-is-new-in-php-53-part-3-mysqlnd.html</guid>
		<description><![CDATA[In the first two parts of this series I wrote about namespaces and late static binding. In this part I will cover mysqlnd (MySQL native driver for PHP), which is also a really cool feature of PHP 5.3. This a replacement library (not an extension) for libmysql (MySQL Client Library) offering a lot of advantage [...]]]></description>
			<content:encoded><![CDATA[<p>In the first two parts of this series I wrote about <a href="http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html" title="Whatâ€™s new in PHP 5.3 - part 1: namespaces">namespaces</a> and <a href="http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html" title="What is new in PHP 5.3 - part 2: late static binding">late static binding</a>. In this part I will cover <a href="http://dev.mysql.com/downloads/connector/php-mysqlnd/" title="MySQL native driver for PHP - mysqlnd">mysqlnd</a> (MySQL native driver for PHP), which is also a really cool feature of PHP 5.3. This a replacement library (not an extension) for libmysql (MySQL Client Library) offering a lot of advantage over it. While libmysql was developed primarily for C and C++, mysqlnd was designed and optimized for PHP, it takes advantages of the internal structure of Zend Engine. By the time of this writing <a href="http://php.net/mysql" title="mysql">mysql</a> and <a href="http://php.net/mysql" title="mysqli">mysqli</a> are supported, and the integration to <a href="http://php.net/pdo" title="PDO">PDO</a> is in <a href="http://blog.ulf-wendel.de/?p=166" title="PHP: mysqlnd to support PDO, mysqlnd in PHP 5.3">working progress</a>.<span id="more-45"></span></p>
<p>The overview of the advantages of mysqlnd over libmysql: high integration with the Zend Engine, faster execution, lower memory footprint, performance statistics for bottle-neck analysis, easier compilation, own development life cycle, usage of PHP stream API, client side query cache and a lot of possibility for future features.  </p>
<p>Over the technical advantages theres is a licensing one. MySQL is rely on the GPL license which is not compatible with the PHP license, using libmysql with PHP needs the <a href="http://www.mysql.com/company/legal/licensing/foss-exception.html" title="FLOSS exception">FLOSS exception</a>, but mysqlnd is licensed under the PHP license so this is no more a problem (check the <a href="http://blogs.mysql.com/kaj/?p=71" title="mysqlnd FAQ">mysqlnd FAQ</a>).</p>
<p>The installation process will be easier, you won&#8217;t need to have libmysql on the server. Libmysql is shipped with the MySQL server, updating libmysql means that you have to update the MySQL server too. With myslqnd this bounding no more exists, it has its own release and development life cycle. Mysqlnd has a common code base for PHP5 and PHP6, so the maintenance of the code is easier, a bug should be fixed only at one place. </p>
<h2>High integration with the Zend Engine</h2>
<p>Mysqlnd is highly integrated with the Zend engine, so it can leverage all the internal PHP infrastructure for communication and memory management, it can do a lot of tricky thing to improve performance. If you are curious about the details you should check <a href="http://www.hristov.com/andrey/" title="Andrey Hristov's homepage">Andrey Hristov&#8217;s</a> <a href="http://www.hristov.com/andrey/projects/php_stuff/pres/mysqlnd_vikinger.pdf" title="presentation from mysqlnd">presentation from mysqlnd</a> (I get some illustration from it). You can find here <a href="http://blog.ulf-wendel.de/?p=138" title="About the performance of mysqlnd-5.0.0-alpha">a little benchmark</a> about the performance of mysqlnd.</p>
<h2>Comply with the <code>memory_limit</code> settings</h2>
<p><img src='http://blog.felho.hu/wp-content/memorylimitsupport.png' alt='Memory limit support' /><br />
Myslqnd uses the Zend Engine&#8217;s memory management so it comply (in contrast to libmysql) with the memory limit setting. It allocates bigger chunk of memory (allocating memory is relatively expensive task), it tries to reuse zvals using zval cache to save CPU cycle.</p>
<h2>Memory savings</h2>
<p><img src='http://blog.felho.hu/wp-content/readonlyvariables.png' alt='Read only variables' /><br />
Mysqlnd introduces an interesting concept: read only variables with which it can save a huge amount of memory. You can read about it in the mentioned presentation or in <a href="http://blog.ulf-wendel.de" title="Ulf Wendel's blog">Ulf Wendel&#8217;s</a> <a href="http://blog.ulf-wendel.de/?p=157" title="PHP: mysqlnd saves 40% memory, finally (new tuning options)!">blog entry</a>. To completely understand this you should be familiar with the internal representation of variables in PHP (I&#8217;ll write about it soon), the short version is the following. When you run a buffered query libmysql reads first the data into its own buffer, and then when you run <code>mysql_fetch_*()</code> function the data is get copied from this buffer into another memory area which represents the PHP variables. Mysqlnd uses a read buffer too, but when you run the <code>mysql_fetch_*()</code> function it isn&#8217;t copy the data but links the PHP variables to this memory area. This way it can consume only half as much memory as libmysql.</p>
<h2>Usage of the PHP stream API</h2>
<p><img src='http://blog.felho.hu/wp-content/mysqlndstreamapi.png' alt='Mysqlnd uses the PHP stream API' /><br />
Mysqlnd uses the PHP stream API, which holds a lot of possibility because it makes possible to hook into the communication with MySQL. Exposing the streams to users is not yet implemented, if it is done one can easily write a custom proxy using stream filters.</p>
<h2>Persistent connections</h2>
<p>You may know that persistent connections was disabled in mysqli extension, but with mysqlnd you will have again the possibility to use persistent connection (<code>mysqli_connect('p:localhost', ...);</code>). <a href="http://www.mysqlperformanceblog.com/2006/11/12/are-php-persistent-connections-evil/" title="Are PHP persistent connections evil ?">This is an interesting</a> post about this topic.</p>
<h2>Statistical data collection</h2>
<p>Mysqlnd collects a lot of statistics which you can use for tuning your application. This statistical information will simplify the process of finding bottlenecks in your system, you can easily monitor your application and you can even build automatic problem detection into it. You can access these data via <code>phpinfo()</code>, <code>mysqli_get_client_stats()</code> (per process statistics), <code>mysqli_get_connection_stats()</code> (per connections statistics) functions. Currently 59 statistical information are collected by mysqlnd, you can read more about this at <a href="http://blog.ulf-wendel.de/?p=163" title="PHP: 59 tuning screws for mysqlnd">Ulf&#8217;s blog</a>. It collects information about its internal zval cache, this you can get with the <code>mysqli_get_cache_stat()</code>, which could bu useful to tweak you zval cahe related php.ini settings, or to find an incidental bad habits in your code (for example you needlessly modify the fetched data and hence you lose the memory saving option). </p>
<h2>Client-side query cache</h2>
<p>Mysqlnd supports client-side query cache which could be very useful in some situation because it eliminates communication overhead (network latency), memory allocation overhead. By client side query cache it could be interesting the invalidation of the cache, mysqlnd currently supports only TTL (time-to-live) invalidation which you can control via <code>php.ini</code> settings. This part of the library is experimental, will change a lot in the future.</p>
<h2>Possible future improvements, features</h2>
<p>There is a lot of possibility in this area, just to name a few: improving the client side query cache, exposing PHP streams to users, prepared statement cache, automatic load balancing, built-in profiling. More foretastes for gourmands: the <a href="http://pooteeweet.org/files/mysqlabphpdriver06/php_driver_wish_list.pdf" title="Wishlist of Lukas for mysqlnd">wishlist of Lukas</a>, and <a href="http://www.mysqlperformanceblog.com/2006/10/28/wishes-for-new-pure-php-mysql-driver/" title="wishlist of Peter Zaitsev for mysqlnd">Peter Zaitsev</a> (<a href="http://www.mysqlperformanceblog.com" title="MySQL Performance Blog">MySQL Performance Blog</a>).</p>
<p>Enjoy your meal! <img src='http://blog.felho.hu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/what-is-new-in-php-53-part-3-mysqlnd.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>


