<?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>Derivante &#187; SQL</title>
	<atom:link href="http://www.derivante.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.derivante.com</link>
	<description>to obtain or receive from a source</description>
	<lastBuildDate>Mon, 26 Apr 2010 18:44:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>PHP ActiveRecord Available for Beta Testing</title>
		<link>http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/</link>
		<comments>http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/#comments</comments>
		<pubDate>Wed, 20 May 2009 01:09:27 +0000</pubDate>
		<dc:creator>Kien La</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=548</guid>
		<description><![CDATA[We've been working hard to get this ready for people to start poking around in and we're happy to announce that it's now ready for public beta testing! You can grab it from http://github.com/kla/php-activerecord/. Play with it... break it... and give us your feedback to help us make a better library for everyone! We want [...]]]></description>
			<content:encoded><![CDATA[<p>We've been working hard to get this ready for people to start poking around in and we're happy to announce that it's now ready for public beta testing! You can grab it from <a href="http://github.com/kla/php-activerecord/">http://github.com/kla/php-activerecord/</a>. Play with it... break it... and give us your feedback to help us make a better library for everyone! We want to hear from you.</p>
<p><span id="more-548"></span></p>
<h2>Quick Start</h2>
<p>We'll start first with a bare bones example to show how little you need to get up and running. There's <i>very</i> little to configure. We've adhered to the convention over configuration philosophy so there are no code generators you need to run and no xml/yaml mapping files to maintain.</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #808080; font-style: italic;">// make sure the ActiveRecord project is in your current directory</span>
<span style="color: #808080; font-style: italic;">// or your include_path</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'ActiveRecord/ActiveRecord.php'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// assumes a table name of &quot;books&quot; with a primay key named &quot;id&quot;</span>
<span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// initialize ActiveRecord</span>
ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'development'</span> =&gt; <span style="color: #ff0000;">'mysql://username:password@localhost/database_name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a style="text-decoration: none;" href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span>Book::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<p>That's it! The code for this is located in the examples/simple directory. You can run it with "php examples/simple/simple.php". Make sure to modify the connection string to suit your system and run the simple.sql script appropriately.</p>
<h2>Serious Business Time</h2>
<p>Now for a not so completely trivial example. This example will simulate a very dumb ordering and payment model. See the examples/orders directory for the source. You can run this sample by executing "php examples/orders/orders.php". Again, make sure you modify the connection string to suit your system and be sure the schema in orders.sql has been created in your test database.</p>
<p>First, let's look at the models:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// a person can have many orders and payments</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$has_many</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'orders'</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'payments'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// must have a name and a state which has a custom error message</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_presence_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span>, <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'state'</span>, <span style="color: #ff0000;">'message'</span> =&gt; <span style="color: #ff0000;">'Where do you live then?'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Order <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// order belongs to a person</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$belongs_to</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'person'</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'order'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// order can have many payments by many people</span>
    <span style="color: #808080; font-style: italic;">// the conditions is just there as an example as it makes no logical sense</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$has_many</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'payments'</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'people'</span>,
            <span style="color: #ff0000;">'through'</span>    =&gt; <span style="color: #ff0000;">'payments'</span>,
            <span style="color: #ff0000;">'select'</span>     =&gt; <span style="color: #ff0000;">'people.*, payments.amount'</span>,
            <span style="color: #ff0000;">'conditions'</span> =&gt; <span style="color: #ff0000;">'payments.amount &lt; 200'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// order must have a price and tax &gt; 0</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$validates_numericality_of</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'price'</span>, <span style="color: #ff0000;">'greater_than'</span> =&gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>,
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'tax'</span>,   <span style="color: #ff0000;">'greater_than'</span> =&gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// setup a callback to automatically apply a tax</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$before_validation_on_create</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'apply_tax'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> apply_tax<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">state</span> == <span style="color: #ff0000;">'VA'</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$tax</span> = <span style="color: #cc66cc;">0.045</span>;
        <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">state</span> == <span style="color: #ff0000;">'CA'</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$tax</span> = <span style="color: #cc66cc;">0.10</span>;
        <span style="color: #b1b100;">else</span>
            <span style="color: #0000ff;">$tax</span> = <span style="color: #cc66cc;">0.02</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">tax</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">price</span> * <span style="color: #0000ff;">$tax</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Payment <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// payment belongs to a person</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$belongs_to</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'person'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>And here's the code that does everything:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #b1b100;">require_once</span> <a style="text-decoration: none;" href="http://www.php.net/dirname"><span style="color: #000066;">dirname</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">'/../../ActiveRecord.php'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// initialize ActiveRecord</span>
ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/dirname"><span style="color: #000066;">dirname</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">'/models'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'development'</span> =&gt; <span style="color: #ff0000;">'mysql://test:test@127.0.0.1/orders_test'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// you can change the default connection with the below</span>
    <span style="color: #808080; font-style: italic;">//$cfg-&gt;set_default_connection('production');</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create some people</span>
<span style="color: #0000ff;">$jax</span> = <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span> =&gt; <span style="color: #ff0000;">'Jax'</span>, <span style="color: #ff0000;">'state'</span> =&gt; <span style="color: #ff0000;">'CA'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$jax</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// compact way to create and save a model</span>
<span style="color: #0000ff;">$tito</span> = Person::<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span> =&gt; <span style="color: #ff0000;">'Tito'</span>, <span style="color: #ff0000;">'state'</span> =&gt; <span style="color: #ff0000;">'VA'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// place orders. tax is automatically applied in a callback</span>
<span style="color: #808080; font-style: italic;">// create_orders will automatically place the created model into $tito-&gt;orders</span>
<span style="color: #808080; font-style: italic;">// even if it failed validation</span>
<span style="color: #0000ff;">$pokemon</span> = <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">create_orders</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'item_name'</span> =&gt; <span style="color: #ff0000;">'Live Pokemon'</span>, <span style="color: #ff0000;">'price'</span> =&gt; <span style="color: #cc66cc;">6999.99</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$coal</span>    = <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">create_orders</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'item_name'</span> =&gt; <span style="color: #ff0000;">'Lump of Coal'</span>, <span style="color: #ff0000;">'price'</span> =&gt; <span style="color: #cc66cc;">100.00</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$freebie</span> = <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">create_orders</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'item_name'</span> =&gt; <span style="color: #ff0000;">'Freebie'</span>, <span style="color: #ff0000;">'price'</span> =&gt; <span style="color: #cc66cc;">-100.99</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$freebie</span>-&gt;<span style="color: #006600;">errors</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;[FAILED] saving order $freebie-&gt;item_name: &quot;</span> .
        <a style="text-decoration: none;" href="http://www.php.net/join"><span style="color: #000066;">join</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span>,<span style="color: #0000ff;">$freebie</span>-&gt;<span style="color: #006600;">errors</span>-&gt;<span style="color: #006600;">full_messages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// payments</span>
<span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">create_payments</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'amount'</span> =&gt; <span style="color: #cc66cc;">1.99</span>, <span style="color: #ff0000;">'person_id'</span> =&gt; <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">create_payments</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'amount'</span> =&gt; <span style="color: #cc66cc;">4999.50</span>, <span style="color: #ff0000;">'person_id'</span> =&gt; <span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">create_payments</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'amount'</span> =&gt; <span style="color: #cc66cc;">2.50</span>, <span style="color: #ff0000;">'person_id'</span> =&gt; <span style="color: #0000ff;">$jax</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// reload since we don't want the freebie to show up (because it failed validation)</span>
<span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">reload</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;$tito-&gt;name has &quot;</span> . <a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">orders</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot; orders for: &quot;</span> .
    <a style="text-decoration: none;" href="http://www.php.net/join"><span style="color: #000066;">join</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">', '</span>,ActiveRecord\collect<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">orders</span>,<span style="color: #ff0000;">'item_name'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// get all orders placed by Tito</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span>Order::<span style="color: #006600;">find_all_by_person_id</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$order</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Order #$order-&gt;id for $order-&gt;item_name &quot;</span> .
        <span style="color: #ff0000;">&quot;($$order-&gt;price + $$order-&gt;tax tax) &quot;</span> .
        <span style="color: #ff0000;">&quot;ordered by &quot;</span> . <span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">name</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">payments</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// display each payment for this order</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">payments</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$payment</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;  payment #$payment-&gt;id of $$payment-&gt;amount by &quot;</span> .
            <span style="color: #0000ff;">$payment</span>-&gt;<span style="color: #006600;">person</span>-&gt;<span style="color: #006600;">name</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
        <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;  no payments<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// display summary of all payments made by Tito and Jax</span>
<span style="color: #0000ff;">$conditions</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'conditions'</span> =&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'id IN(?)'</span>,<a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tito</span>-&gt;<span style="color: #006600;">id</span>,<span style="color: #0000ff;">$jax</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
    <span style="color: #ff0000;">'order'</span>      =&gt; <span style="color: #ff0000;">'name desc'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span>Person::<span style="color: #006600;">all</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conditions</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$person</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$n</span> = <a style="text-decoration: none;" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">payments</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$total</span> = <a style="text-decoration: none;" href="http://www.php.net/array_sum"><span style="color: #000066;">array_sum</span></a><span style="color: #66cc66;">&#40;</span>ActiveRecord\collect<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">payments</span>,<span style="color: #ff0000;">'amount'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;$person-&gt;name made $n payments for a total of $$total<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// using order has_many people through payments with options</span>
<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #808080; font-style: italic;">// array('people',</span>
<span style="color: #808080; font-style: italic;">//    'through' =&gt; 'payments',</span>
<span style="color: #808080; font-style: italic;">//    'select' =&gt; 'people.*, payments.amount',</span>
<span style="color: #808080; font-style: italic;">//    'conditions' =&gt; 'payments.amount &lt; 200'));</span>
<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #808080; font-style: italic;">// this means our people in the loop below also has the payment information since</span>
<span style="color: #808080; font-style: italic;">// it is part of an inner join we will only see 2 of the people instead of 3</span>
<span style="color: #808080; font-style: italic;">// because 1 of the payments is greater than 200</span>
<span style="color: #0000ff;">$order</span> = Order::<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pokemon</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>;
<a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Order #$order-&gt;id for $order-&gt;item_name ($$order-&gt;price + $$order-&gt;tax tax)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$order</span>-&gt;<span style="color: #006600;">people</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$person</span><span style="color: #66cc66;">&#41;</span>
    <a style="text-decoration: none;" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;  payment of $$person-&gt;amount by &quot;</span> . <span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">name</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<p>The orders example should produce the following output:</p>
<pre>[FAILED] saving order Freebie: Price must be greater than 0, Tax must be greater than 0

Tito has 2 orders for: Live Pokemon, Lump of Coal

Order #3 for Live Pokemon ($6999.99 + $315 tax) ordered by Tito
  payment #4 of $1.99 by Tito
  payment #5 of $4999.5 by Tito
  payment #6 of $2.5 by Jax

Order #4 for Lump of Coal ($100 + $4.5 tax) ordered by Tito
  no payments

Tito made 2 payments for a total of $5001.49

Jax made 1 payments for a total of $2.5

Order #3 for Live Pokemon ($6999.99 + $315 tax)
  payment of $2.50 by Jax
  payment of $1.99 by Tito</pre>
<h2>EXTENDED CONFIGURATION</h2>
<p>Here's one more example that is basically the same as the first, but the point here is to display some of the ways to extend configuration (while remaining close to convention).</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord\Model
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// explicit table name since our table is not &quot;books&quot;</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$table_name</span> = <span style="color: #ff0000;">'simple_book'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// explicit pk since our pk is not &quot;id&quot;</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$primary_key</span> = <span style="color: #ff0000;">'book_id'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// explicit connection name since we always want production with this model</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$connection</span> = <span style="color: #ff0000;">'production'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// explicit database name will generate sql like so =&gt; db.table_name</span>
    <a style="text-decoration: none;" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$db</span> = <span style="color: #ff0000;">'test'</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// ActiveRecord allows the use of multiple connections</span>
<span style="color: #0000ff;">$connections</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'development'</span> =&gt; <span style="color: #ff0000;">'mysql://test:test@127.0.0.1/development'</span>,
    <span style="color: #ff0000;">'production'</span> =&gt; <span style="color: #ff0000;">'mysql://test:test@127.0.0.1/production'</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// initialize ActiveRecord</span>
ActiveRecord\Config::<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cfg</span><span style="color: #66cc66;">&#41;</span> use <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$connections</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_model_directory</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$cfg</span>-&gt;<span style="color: #006600;">set_connections</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$connections</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a style="text-decoration: none;" href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span>Book::<span style="color: #006600;">first</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h2>Simplicity</h2>
<p>From the configuration, all the way to a <i>serious-business</i> example, ActiveRecord takes care of the heavy-lifting and the gritty details for you. This allows you, the developer, to focus more on business logic and complex code instead of composing custom SQL queries or designing ways to handle your data. Because we have embraced a convention over configuration philosophy, using our library is not painful. The conventions are easy to remember which will also contribute to stream-lining your productivity as a developer.</p>
<h2>Where can you find ActiveRecord?</h2>
<p>Again, the code is hosted on <a href="http://github.com/kla/php-activerecord/">http://github.com/kla/php-activerecord/</a>. We also have a <a href="http://www.phpactiverecord.org/">website</a> which will be available in the near future. We hope to have all the necessary content such as: tutorials, screencasts, and documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/05/19/php-activerecord-available-for-beta-testing/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>PHP GIS Functions</title>
		<link>http://www.derivante.com/2009/04/14/php-gis-functions/</link>
		<comments>http://www.derivante.com/2009/04/14/php-gis-functions/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 17:35:53 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=313</guid>
		<description><![CDATA[I have been working a lot of with PHP and GIS consulting for CitySquares and the History Engine. I found searching for everything I needed to do basic processing &#38; Google Integration tedious and painful. So here is a collection of common functions that helped me get through the massaging of the data and ready [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-425" style="margin: 15px;" title="php-med-trans-light" src="http://www.derivante.com/wp-content/uploads/2009/05/php-med-trans-light.gif" alt="php-med-trans-light" width="95" height="51" />I have been working a lot of with PHP and GIS consulting for <a href="http://citysquares.com">CitySquares </a>and the <a href="http://historyengine.richmond.edu/">History Engine</a>.  I found searching for everything I needed to do basic processing &amp; Google Integration tedious and painful.  So here is a collection of common functions that helped me get through the <em>massaging</em> of the data and ready for integration.</p>
<ul style="padding-left: 20px;padding-bottom: 10px;"> <strong><a href="http://www.derivante.com/files/phpgis.txt">pnPoly</a></strong> - Used to determine if a coordinate falls inside a polygon.<br />
<strong><a href="http://www.derivante.com/files/phpgis.txt">Centroid </a></strong>- Find the center of a polygon..<br />
<strong><a href="http://www.derivante.com/files/phpgis.txt">Area</a></strong> - Calculate the area of a polygon.<br />
<strong><a href="http://www.derivante.com/files/geocoder.txt">googleGeoCoder </a></strong> - Extracts GIS information from Google Maps from an address.<br />
<strong><a href="http://www.derivante.com/files/polyline.txt">PolylineEncoder</a></strong> - Takes a set of coordinates and encodes it for Google Maps.</ul>
<p>If you ran into the problem I did, which is that a lot of the data is coming in the form of shp/dbf files and needs to be parsed out to something friendlier either KML or CSV, there are a couple of solutions for that.  You can parse out the data with <a href="http://www.obviously.com/gis/shp2text/">shp2text</a> if your source coordinate format is already in lat/lng or if you have different coordinate system and use ArcGIS, you can try the plugin <a href="http://arcscripts.esri.com/details.asp?dbid=14273">Export to KML 2.5.3</a> to help with the exporting of data with the ESRI suite of products.</p>
<p>Once your data is in SQL, the following query is an example of distance sorting with SQL. You can grab a copy of the zip_codes database <a href="http://www.derivante.com/files/zip_codes.sql.gz">here</a> and play around with it.</p>
<pre style="padding-left: 20px;padding-bottom: 10px;" lang="sql">SELECT *,
sqrt((69.1 * ("37.6" - latitude)) * (69.1 * ("37.6" - latitude)) +
(53.0 * ("-77.6" - longitude)) * (53.0 * ("-77.6" - longitude)))
AS distance
FROM `zip_codes`
HAVING distance &lt; 10
ORDER BY distance ASC</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/04/14/php-gis-functions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>SQL script to grab the worst performing indexes</title>
		<link>http://www.derivante.com/2009/02/11/sql-script-to-grab-the-worst-performing-indexes/</link>
		<comments>http://www.derivante.com/2009/02/11/sql-script-to-grab-the-worst-performing-indexes/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 17:52:23 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=231</guid>
		<description><![CDATA[I have been doing a lot of auditing and clean up of database performance the last few days. We are currently in the middle of a migration and with hardware infrastructure in place it is time to go back and see what changes we can do on the code and database side of things to [...]]]></description>
			<content:encoded><![CDATA[<p>I have been doing a lot of auditing and clean up of database performance the last few days.  We are currently in the middle of a migration and with hardware infrastructure in place it is time to go back and see what changes we can do on the code and database side of things to help bring the site up to optimal performance and lower query times.  I found this gem in the MySQL Forge site; which turned out to be a great resource for MySQL tidbits.</p>
<pre class="sql"><span style="color: #808080; font-style: italic;">/*
SQL script to grab the worst performing indexes
in the whole server
*/</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
t.TABLE_SCHEMA <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`db`</span>
, t.TABLE_NAME <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`table`</span>
, s.INDEX_NAME <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`inde name`</span>
, s.COLUMN_NAME <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`field name`</span>
, s.SEQ_IN_INDEX <span style="color: #ff0000;">`seq in index`</span>
, s2.max_columns <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`# cols`</span>
, s.CARDINALITY <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`card`</span>
, t.TABLE_ROWS <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`est rows`</span>
, ROUND<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>s.CARDINALITY / IFNULL<span style="color: #66cc66;">&#40;</span>t.TABLE_ROWS, <span style="color: #cc66cc;">0.01</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`sel %`</span>
<span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA.STATISTICS s
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> INFORMATION_SCHEMA.<span style="color: #993333; font-weight: bold;">TABLES</span> t
<span style="color: #993333; font-weight: bold;">ON</span> s.TABLE_SCHEMA = t.TABLE_SCHEMA
<span style="color: #993333; font-weight: bold;">AND</span> s.TABLE_NAME = t.TABLE_NAME
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
TABLE_SCHEMA
, TABLE_NAME
, INDEX_NAME
, MAX<span style="color: #66cc66;">&#40;</span>SEQ_IN_INDEX<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> max_columns
<span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA.STATISTICS
<span style="color: #993333; font-weight: bold;">WHERE</span> TABLE_SCHEMA != <span style="color: #ff0000;">'mysql'</span>
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> TABLE_SCHEMA, TABLE_NAME, INDEX_NAME
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> s2
<span style="color: #993333; font-weight: bold;">ON</span> s.TABLE_SCHEMA = s2.TABLE_SCHEMA
<span style="color: #993333; font-weight: bold;">AND</span> s.TABLE_NAME = s2.TABLE_NAME
<span style="color: #993333; font-weight: bold;">AND</span> s.INDEX_NAME = s2.INDEX_NAME
<span style="color: #993333; font-weight: bold;">WHERE</span> t.TABLE_SCHEMA != <span style="color: #ff0000;">'mysql'</span>                         <span style="color: #808080; font-style: italic;">/* Filter out the mysql system DB */</span>
<span style="color: #993333; font-weight: bold;">AND</span> t.TABLE_ROWS &amp;gt; <span style="color: #cc66cc;">10</span>                                   <span style="color: #808080; font-style: italic;">/* Only tables with some rows */</span>
<span style="color: #993333; font-weight: bold;">AND</span> s.CARDINALITY <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>                           <span style="color: #808080; font-style: italic;">/* Need at least one non-NULL value in the field */</span>
<span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">&#40;</span>s.CARDINALITY / IFNULL<span style="color: #66cc66;">&#40;</span>t.TABLE_ROWS, <span style="color: #cc66cc;">0.01</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> &amp;lt; <span style="color: #cc66cc;">1.00</span> <span style="color: #808080; font-style: italic;">/* Selectivity &amp;lt; 1.0 b/c unique indexes are perfect anyway */</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">`sel %`</span>, s.TABLE_SCHEMA, s.TABLE_NAME          <span style="color: #808080; font-style: italic;">/* Switch to `sel %` DESC for best non-unique indexes */</span>
<span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">10</span>;</pre>
<p>To audit just one database if you are running on a server with several different databases, just adjust the where clause to WHERE t.TABLE_SCHEMA = 'mytable'.  This would have been very useful when working with the cluster to recover memory/space from indexes that aren't being used and to optimize  queries to hit indexes that are meaningful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/02/11/sql-script-to-grab-the-worst-performing-indexes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Proxy Quick Start</title>
		<link>http://www.derivante.com/2009/02/10/mysql-proxy-quick-start/</link>
		<comments>http://www.derivante.com/2009/02/10/mysql-proxy-quick-start/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 16:40:39 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=229</guid>
		<description><![CDATA[There isn't too much out there for easy reliable mysql load balancing. I recently built a master/master mysqld setup and needed automatic roll over on the software side. After going through the options I ended up going with MySQL Proxy 0.60 for low overhead, ease of setup, and ease of maintenance. I installed the proxy [...]]]></description>
			<content:encoded><![CDATA[<p>There isn't too much out there for easy reliable mysql load balancing.   I recently built a master/master mysqld setup and needed automatic roll over on the software side.  After going through the options I ended up going with MySQL Proxy 0.60 for low overhead, ease of setup, and ease of maintenance.  I installed the proxy on each of my web servers (already load balanced through hardware) and configured the code to just use the local proxy.   A quick start for RHEL systems,  you can get it up and running in just a few simple steps:</p>
<ol>
<li>Download the latest version at <a href="http://dev.mysql.com/downloads/mysql-proxy/">http://dev.mysql.com/downloads/mysql-proxy/</a>.</li>
<li><strong>yum install</strong> <strong>glib2-devel</strong>.x86_64 <strong>ncurses-devel</strong>.x86_64 <strong>libevent-devel</strong>.x86_64 <strong>mysql-devel</strong>.x86_64</li>
<li><strong>./configure -without-lua</strong> (for a straight proxy, you will not need LUA and this will save you having to install additional dependencies).</li>
<li><strong>make &amp;&amp; make install</strong></li>
</ol>
<p>Easy enough to get it installed in the system, but I noticed that the default parameters and as with most software is rather wide open so be sure to lock down the ip addresses you are listening on.</p>
<p>mysql-proxy --proxy-backend-addresses=192.168.1.101:3306 --proxy-backend-addresses=192.168.1.102:3306 --admin-address=127.0.0.1:4041 --proxy-address=127.0.0.1:4040 --proxy-skip-profiling --daemon</p>
<p>The admin address and proxy address will be accessible by anyone hitting your server unless you lock it down to your local host.   Now configure your web servers to use localhost:4040 to connect to MySQL and all should be working fine.   You can find additional configuration parameters, admin commands, and LUA scripts at <a href="http://forge.mysql.com/wiki/MySQL_Proxy">http://forge.mysql.com/wiki/MySQL_Proxy</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/02/10/mysql-proxy-quick-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>There and Back Again, an EC2 MySQL Cluster</title>
		<link>http://www.derivante.com/2009/01/26/there-and-back-again-an-ec2-mysql-cluster/</link>
		<comments>http://www.derivante.com/2009/01/26/there-and-back-again-an-ec2-mysql-cluster/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 04:40:20 +0000</pubDate>
		<dc:creator>Clay vanSchalkwijk</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[Web Technology]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=223</guid>
		<description><![CDATA[Limitations of EC2 as a web platform: Price- An m1.xlarge instance will run you ~$600 with data transfer costs. Managed hosting solutions run cheaper especially if you plan on purchasing in bulk. The grid is designed for on-demand computation and not as a cost efficient web services. Configuration- There are a limited number of options [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Limitations of EC2 as a web platform:</strong></p>
<ul style="padding-left: 20px;">
<li> <strong>Price</strong>-  An m1.xlarge instance will run you ~$600 with data transfer costs. Managed hosting solutions run cheaper especially if you plan on purchasing in bulk. The grid is designed for on-demand computation and not as a cost efficient web services.</li>
<li><strong>Configuration</strong>- There are a limited number of options and you will not be able to tailor the hardware to your application. Databases over 10GB of size will have performance issues since that is the memory cap.</li>
<li><strong>Network storage</strong>- The primary disks offers limited storage, additional volumes will need to be attached across the network and at an additional cost.</li>
<li><strong>Software</strong> - No hardware based solutions for load balancing or custom application servers.  The model is software driven so all needs must be met with a software solution.  In a managed hosting or collocation  solution you will at least have the option of adding additional hardware and having a private network.  No dedicated switching, routers, firewall, or load balancers.</li>
</ul>
<p><br><br />
<strong>EC2 might be right for you if:</strong></p>
<ul style="padding-left: 20px;">
<li><strong>Distribution Awareness</strong>-  Your application was designed to scale horizontally from the get-go and you can take advantage of grid computing.</li>
<li><strong>Research and Development</strong>- EC2 &amp; Rightscale will allow for you to bring up new servers, test configuration, and scale quickly.  If you are not sure what your hardware demands will be or the scope of the project it will allow for some flexibility to get this right before committing to rather lengthy contracts with other hosting options.</li>
<li><strong>Disaster Recovery</strong>- If you need an off-site mirror for your site that you can keep dormant and activate as needed.</li>
</ul>
<p><br><br />
Over the past several months I have been doing extensive development using Amazon's EC2 as my hardware infrastructure.  I was tasked with taking CitySquares.com from a New England area hyper-local search and business directory to a national site in a few months.   Due to the memory limitations of EC2 instances, m1.xlarge only providing 15GB the jump from a 15GB database to anything larger becomes very costly.  When everything was able to be contained in two servers in a master/slave environment we were able to provide redundancy, performance, and easy management when working with the database. The final estimations of the national roll out would put our core data at 50GB.  Far too large for any one EC2 instance.  Going to disk was not an option as everything works off of EBS attached storage and that any disk writes means traveling over the network.  An additional overhead which degrades performance even more when switching off RAM.  Then, there was also the nature of the data, which means that at any page load, any piece of data could be requested.</p>
<p>With OS overhead, index storage, and ndb overhead, each x1.mlarge instance gave about 12GB of usable storage.  Include replication, and it comes down to 6GB of storage per node.  To store a ~50GB database that I had planned on requires 8 storage nodes, two management servers, and two mysqld api servers.  This is where it became important to understand the advantages of vertical scaling versus horizontal scaling.  EC2 provided fast horizontal scaling and configuration.  Servers can be launched on demand and their configurations scripted.   While I appreciated that aspect of cloud computing and being able to bring that many servers up and configure each one relatively quickly I really just needed two decent boxes with 64GB of RAM in each and a master/slave setup.  The operations costs for the cluster was $6000/month, a hefty bill considering I could buy all the hardware needed to run the cluster in just a few months of paying for EC2.</p>
<p>Twelve servers later and a working cluster we were able to successfully roll out our MySQL Cluster with minimal performance loss.   A lot of it was due to tweaking every query top to bottom.  The site runs on a Drupal core, which meant a lot of the queries were not designed with distribution awareness both from code in house and the core.  This was another added growing pain since the network overhead of running 12 servers on shared resources, with mediocre latency, and throughput limitations was amplifying flaws in the database design and every poorly designed query and join degraded performance significantly.</p>
<p>To give credit where credit is due, EC2 did allow us to scale the site up rather quickly.  We were able to test server configurations, new applications, and have easy management.  It would not have been possible for us to push out the data, handle influx of new traffic, and expand as fast as we had without it.  Long term however, it made absolutely no sense that once we were finished scaling up, to stay on EC2.  It is a great platform for start-ups to be able to configure and launch servers for their service or application and grow rapidly.</p>
<p>As a database platform, until EC2 offers more configuration options in it's hardware and the ability to increase memory, the cap of 15GB will make EC2 problematic for any database that plans on growing past that. It is important to understand your application and database needs before considering EC2.  It is no surprise to me that even with the RightScale interface, and easy management of EC2 web sites are reluctant to switch off their own hardware or managed hosting.</p>
<p>Both Sun and Continuent are pursuing MySQL Clustering on cloud computing.  As of this post, Continuent for EC2 was still in closed beta and testing and Sun is doing their own research into offering more support for database clusters on compute clouds.    Maybe in the future this can be something to revisit, but it would require more ndb configuration options on the network layer to cope with shared bandwidth and additional hardware configurations by Amazon (someday).</p>
<p>The 6.4 release which is in beta now offers new features which would make MySQL Clustering more attractive on cloud computing architecture:</p>
<ul>
<li><em>Ability to add nodes and node groups online.</em> This will allow the database to scale up without taking the cluster down.</li>
<li><em>Data node multithreading support. </em> The m1.xlarge instance comes with 4 cores.</li>
</ul>
<p>If you are setting up a MySQL cluster, the following resources will help get you up and running quickly:</p>
<ul>
<li><a href="http://www.severalnines.com/" target="_blank">http://www.severalnines.com/</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-configuration.html" target="_blank">http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-configuration.html</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2009/01/26/there-and-back-again-an-ec2-mysql-cluster/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->