<?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>Life &#187; DB</title>
	<atom:link href="http://life.co-hey.com/tag/db/feed/" rel="self" type="application/rss+xml" />
	<link>http://life.co-hey.com</link>
	<description></description>
	<lastBuildDate>Tue, 17 Jan 2012 08:15:38 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>VMをつくり直すときに役に立ったサイト</title>
		<link>http://life.co-hey.com/2010/10/vm%e3%82%92%e3%81%a4%e3%81%8f%e3%82%8a%e7%9b%b4%e3%81%99%e3%81%a8%e3%81%8d%e3%81%ab%e5%bd%b9%e3%81%ab%e7%ab%8b%e3%81%a3%e3%81%9f%e3%82%b5%e3%82%a4%e3%83%88/</link>
		<comments>http://life.co-hey.com/2010/10/vm%e3%82%92%e3%81%a4%e3%81%8f%e3%82%8a%e7%9b%b4%e3%81%99%e3%81%a8%e3%81%8d%e3%81%ab%e5%bd%b9%e3%81%ab%e7%ab%8b%e3%81%a3%e3%81%9f%e3%82%b5%e3%82%a4%e3%83%88/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 07:35:51 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[web & pc]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[VM]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=899</guid>
		<description><![CDATA[プログラムテスト環境として利用していたVMのハードディスク容量が足りなくなったので、新たに作り直しました。前回は勉強のためとソースからコンパイルして作ったんですが、今回はyumを使った方法を試してみることに。 途中うまく [...]]]></description>
			<content:encoded><![CDATA[<p>
プログラムテスト環境として利用していたVMのハードディスク容量が足りなくなったので、新たに作り直しました。前回は勉強のためとソースからコンパイルして作ったんですが、今回はyumを使った方法を試してみることに。<br />
<br />
途中うまく動かない箇所があったんですが、yumリポジトリを変えて再度構築しなおしたら、意図通りに動作する環境ができあがりました。そのときに参考にしたサイトを忘れないようにメモです。<br />
<br />
いくつかのサイトを参考にしながら作成しましたが、うまくいったのはこのサイトの方法で構築したときでした。<br />
【VMware Player上のCentOSに開発環境を構築 #2CommentsAdd Star】<br />
<a href="http://rewish.org/php_mysql/vm_player_config_2">http://rewish.org/php_mysql/vm_player_config_2</a><br />
<br />
Gitのインストールの際に参考にしたサイト<br />
【centos5.3でyumを使ってgitをインストールする】<br />
<a href="http://d.hatena.ne.jp/uk_oasis/20090807/1249633626">http://d.hatena.ne.jp/uk_oasis/20090807/1249633626</a><br />
<br />
<br />
ついでに調べた、Gitのクライアント。<br />
【tortoiseGit】　tortoiseSVNのgit版。Win専用。<br />
<a href="http://code.google.com/p/tortoisegit/">http://code.google.com/p/tortoisegit/</a><br />
<br />
【SmartGit】 Win、Mac、Linuxに対応<br />
<a href="http://www.syntevo.com/smartgit/index.html">http://www.syntevo.com/smartgit/index.html</a><br />
Gitだけじゃなくて、CSV、SVNのクライアントも。慣れたら同じ使い勝手で全部いけるとしたら一番いいのかも。<br />
<br />
【Gitti】 Mac専用。画面がいい感じ。<br />
<a href="http://www.gittiapp.com/">http://www.gittiapp.com/</a></p>

<a href="http://twitter.com/?status=RT%20%40%3A%20VM%E3%82%92%E3%81%A4%E3%81%8F%E3%82%8A%E7%9B%B4%E3%81%99%E3%81%A8%E3%81%8D%E3%81%AB%E5%BD%B9%E3%81%AB%E7%AB%8B%E3%81%A3%E3%81%9F%E3%82%B5%E3%82%A4%E3%83%88%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F2dc4crb&amp;in_reply_to_status_id=26628715152" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/10/vm%e3%82%92%e3%81%a4%e3%81%8f%e3%82%8a%e7%9b%b4%e3%81%99%e3%81%a8%e3%81%8d%e3%81%ab%e5%bd%b9%e3%81%ab%e7%ab%8b%e3%81%a3%e3%81%9f%e3%82%b5%e3%82%a4%e3%83%88/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL複合INDEXの威力</title>
		<link>http://life.co-hey.com/2010/09/mysql%e8%a4%87%e5%90%88index%e3%81%ae%e5%a8%81%e5%8a%9b/</link>
		<comments>http://life.co-hey.com/2010/09/mysql%e8%a4%87%e5%90%88index%e3%81%ae%e5%a8%81%e5%8a%9b/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 15:32:19 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[web & pc]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=836</guid>
		<description><![CDATA[予想以上の効果がでたので記事に残します。 DBの情報を元に統計を行うSQLとプログラムの性能改善で、複合INDEXを試してみました。 MySQLは、4.2です。 対象となったSQLは複数ありますが、そのうちほとんどは４つ [...]]]></description>
			<content:encoded><![CDATA[<p>
予想以上の効果がでたので記事に残します。<br />
DBの情報を元に統計を行うSQLとプログラムの性能改善で、複合INDEXを試してみました。<br />
MySQLは、4.2です。<br />
<br />
対象となったSQLは複数ありますが、そのうちほとんどは４つのテーブルをJOINしています。取得条件を変えながら情報を取得し、最後に目的別に合算するっていうことをやっています。４つのテーブルのうち３つはデータ数200万オーバー、１つは100万オーバー。<br />
<br />
最初は、こんな状態<br />
<br />

<div class="wp_codebox"><table><tr id="p8365"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p836code5"><pre class="txt" style="font-family:Osaka-mono,monospace;">+----+-------------+-------+--------+----------------------------------------------+
| id | select_type | table | type   | Extra                                        |
+----+-------------+-------+--------+----------------------------------------------+
|  1 | SIMPLE      | tb    | range  | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | tc    | ref    | Using where                                  |
|  1 | SIMPLE      | tp    | ref    | Using where                                  |
|  1 | SIMPLE      | td    | eq_ref | Using where                                  |
+----+-------------+-------+--------+----------------------------------------------+</pre></td></tr></table></div>

<br />
<br />
まずは、一番最初に検索されている <strong>tb</strong> に複合インデックス（検索条件カラム、取得カラム全部、合計１１カラム）を設定してみるも結果変わらず。カラムを検索条件だけにしてみたり、更に減らしたりと試すも結果変わらず。<br />
<br />
とりあえず、最初に設定した複合インデックスを設定して、<strong>tc</strong>にも複合インデックス（全８カラム）を設定してみる。<br />
<br />
すると変化が現れた。<br />
<br />

<div class="wp_codebox"><table><tr id="p8366"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p836code6"><pre class="txt" style="font-family:Osaka-mono,monospace;">+----+-------------+-------+--------+----------------------------------------------------------+
| id | select_type | table | type   | Extra                                                    |
+----+-------------+-------+--------+----------------------------------------------------------+
|  1 | SIMPLE      | tc    | ref    | Using where; Using index Using temporary; Using filesort |
|  1 | SIMPLE      | tb    | ref    | Using where                                              |
|  1 | SIMPLE      | tp    | ref    | Using where                                              |
|  1 | SIMPLE      | td    | eq_ref | Using where                                              |
+----+-------------+-------+--------+----------------------------------------------------------+</pre></td></tr></table></div>

<br />
<br />
<strong>tc</strong> に設定したINDEXが利用されてuse indexが出てきたのはわかるが、テーブルをJOINする順番が変わっている！！気になって、<strong>tb</strong> に設定した複合INDEXを削除すると、また<strong>tb</strong> からJOINされるようになった、possible key（利用可能index)にはいて、key（利用index）にはでてこないけど、なんらかの影響は及ぼしているようだ。<br />
<br />
<strong>td tp</strong>にも同じ要領で検索対象、取得対象のカラムに複合INDEXを設定する。その結果、<strong>td tp</strong>にもuse indexが表示されるようになった。<br />
<br />

<div class="wp_codebox"><table><tr id="p8367"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p836code7"><pre class="txt" style="font-family:Osaka-mono,monospace;">+----+-------------+-------+------+----------------------------------------------------------+
| id | select_type | table | type | Extra                                                    |
+----+-------------+-------+------+----------------------------------------------------------+
|  1 | SIMPLE      | tc    | ref  | Using where; Using index Using temporary; Using filesort |
|  1 | SIMPLE      | tb    | ref  | Using where                                              |
|  1 | SIMPLE      | tp    | ref  | Using where; Using index                                 |
|  1 | SIMPLE      | td    | ref  | Using where; Using index                                 |
+----+-------------+-------+------+----------------------------------------------------------+</pre></td></tr></table></div>

<br />
<br />
だが、肝心のusing temporary、using filesortが消えない。using filesortって、今回のSQLにはgroup by は入っているけど、order byは入っていない。いくつかサイトを見たけど、order by には言及してあるものが多かった。そこで、order by をgroup by に読み替えて、実験してみることにした。<br />
<strong><a href="http://www.mysqlpracticewiki.com/index.php/Extra_field">http://www.mysqlpracticewiki.com/index.php/Extra_field</a></strong><br />
上記のサイトと、リンク先を読んでいて今回のケースに当てはまりそうなものは、最初にJOINされるテーブルではないカラムで、group by を掛けていること。<strong>tb</strong>のカラムでかけていた。<br />
幸い<strong>tc tb</strong>は１対１対応のテーブルでで、group by を<strong>tc</strong> のカラムでかけても取得結果は変わらないので、試してみることに。<br />
<br />
その結果がこれ<br />
<br />

<div class="wp_codebox"><table><tr id="p8368"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p836code8"><pre class="txt" style="font-family:Osaka-mono,monospace;">+----+-------------+-------+------+--------------------------+
| id | select_type | table | type | Extra                    |
+----+-------------+-------+------+--------------------------+
|  1 | SIMPLE      | tc    | ref  | Using where; Using index |
|  1 | SIMPLE      | tb    | ref  | Using where              |
|  1 | SIMPLE      | tp    | ref  | Using where; Using index |
|  1 | SIMPLE      | td    | ref  | Using where; Using index |
+----+-------------+-------+------+--------------------------+</pre></td></tr></table></div>

<br />
<br />
消えた！やった！<br />
ってことで、実行されるSQL（20ちょいくらい）にexplainをかけてみた。サブクエリ以外でexplainをとってみると、ほとんどのものでusing temporary、using filesortが消えていた。消えなかったのは、取得したいデータの量的にどうしようもなさそうなもの（データの半分以上を一旦取得するもの）くらい。<br />
<br />
これで統計プログラムを実行したところ、実行時間が40分オーバーから5分前後へ！<br />
かなり早くなった。予想以上の結果にびっくりしたので、メモ！<br />
<br />
複合INDEXについては、以下のサイトも参考にさせていただきました。<br />
<a href="http://d.hatena.ne.jp/k_yamamot/20100831/1283262245">http://d.hatena.ne.jp/k_yamamot/20100831/1283262245</a><br />
<a href="http://txqz.net/blog/2006/12/13/0943">http://txqz.net/blog/2006/12/13/0943</a><br />
<a href="http://dev.seesaa.net/article/238633.html">http://dev.seesaa.net/article/238633.html</a></p>

<a href="http://twitter.com/?status=RT%20%40%3A%20MySQL%E8%A4%87%E5%90%88INDEX%E3%81%AE%E5%A8%81%E5%8A%9B%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F2fdrnv2" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/09/mysql%e8%a4%87%e5%90%88index%e3%81%ae%e5%a8%81%e5%8a%9b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZendでDbのMeta情報をキャッシュする</title>
		<link>http://life.co-hey.com/2010/09/zend%e3%81%a7db%e3%81%aemeta%e6%83%85%e5%a0%b1%e3%82%92%e3%82%ad%e3%83%a3%e3%83%83%e3%82%b7%e3%83%a5%e3%81%99%e3%82%8b/</link>
		<comments>http://life.co-hey.com/2010/09/zend%e3%81%a7db%e3%81%aemeta%e6%83%85%e5%a0%b1%e3%82%92%e3%82%ad%e3%83%a3%e3%83%83%e3%82%b7%e3%83%a5%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 03:50:46 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=788</guid>
		<description><![CDATA[なんのこっちゃ？って感じですよね。僕もそう思います。 ZendFrameworkを利用する場合、Zend_Db_Table（Row、Rowsetも）を利用することが多いと思います。Zend_Db_Table_Abstra [...]]]></description>
			<content:encoded><![CDATA[<p>
なんのこっちゃ？って感じですよね。僕もそう思います。<br />
<br />
ZendFrameworkを利用する場合、Zend_Db_Table（Row、Rowsetも）を利用することが多いと思います。Zend_Db_Table_Abstractを継承して、テーブル名と他テーブルとのリレーションを定義するだけで、DBの登録、更新、削除ができてしまう優れものです。個人的にはこれだけ、ZendFrameworkを使ってみようってきっかけになりました。<br />
<br />
これらがテーブル名とリレーションだけしか定義してないのに、存在しないカラム名を指定して処理しようとすると、”そんなカラムはないよ！”ってエラーを出してくれます。賢いです。これができるのって、テーブル関連の処理をするたびに&#8221;describe&#8221;が実行されて、テーブルの情報を取得しているからなのです。気がきいてます。<br />
<br />
ただ１点、&#8221;describe&#8221;が実行される量が多すぎるんです。これが処理が遅くなる原因になることがあります。ここででてくるのがDbのMeta情報のキャッシュ。DbのMeta情報というのは、&#8221;describe&#8221;の実行結果です。なので、これをキャッシュとして保持しておくと、&#8221;describe&#8221;をしなくても、情報が手に入るため余計なSQLをDBに実行しなくてすみます。<br />
<br />
たかが&#8221;describe&#8221;ですが、実行回数がほんとに多いので、だいぶ変わります。<br />
Zend_Db_Table使うときは、セットで設定するくらいでいいのではと思っています。<br />
<br />
Bootstrap.phpの中にこんな感じで書いておくと使えます。<br />
<br />

<div class="wp_codebox"><table><tr id="p78810"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p788code10"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _initDbCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$frontendOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'automatic_serialization'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$backendOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_dir'</span> <span style="color: #339933;">=&gt;</span> APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'cacheディレクトリへの相対パス'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> Zend_Cache<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Core'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'File'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$frontendOptions</span><span style="color: #339933;">,</span> <span style="color: #000088;">$backendOptions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Pb_Db_Table_Abstract<span style="color: #339933;">::</span><span style="color: #004000;">setDefaultMetadataCache</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend%E3%81%A7Db%E3%81%AEMeta%E6%83%85%E5%A0%B1%E3%82%92%E3%82%AD%E3%83%A3%E3%83%83%E3%82%B7%E3%83%A5%E3%81%99%E3%82%8B%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F2eszsnx" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/09/zend%e3%81%a7db%e3%81%aemeta%e6%83%85%e5%a0%b1%e3%82%92%e3%82%ad%e3%83%a3%e3%83%83%e3%82%b7%e3%83%a5%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UNIONの落とし穴</title>
		<link>http://life.co-hey.com/2010/08/union%e3%81%ae%e8%90%bd%e3%81%a8%e3%81%97%e7%a9%b4/</link>
		<comments>http://life.co-hey.com/2010/08/union%e3%81%ae%e8%90%bd%e3%81%a8%e3%81%97%e7%a9%b4/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 01:02:20 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[web & pc]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=778</guid>
		<description><![CDATA[UNIONを利用してselectの結果を結合する場合、カラム名はUNION文の一番最初に書かれたSQL文のものが優先される。 その後のSQLの取得結果は、カラム名を見て同じものを同じ列に並べてくれるわけではなく、取得項目 [...]]]></description>
			<content:encoded><![CDATA[<p>
UNIONを利用してselectの結果を結合する場合、カラム名はUNION文の一番最初に書かれたSQL文のものが優先される。<br />
<br />
その後のSQLの取得結果は、カラム名を見て同じものを同じ列に並べてくれるわけではなく、取得項目の順番のみで判別され、結合される。<br />
<br />
例えば、１つめのselect結果が、<br />
col1, col2, col3, col4 <br />
という順番で取得され、２つめのselect結果が<br />
col1, col3, col2, col4<br />
という順番で取得されていた場合、2つめのselect結果のcol3は、col2として、col2はcol3としてUNIONの取得結果が表示される。<br />
<br />
項目名とは意味の異なるデータが表示されるので、まったく使えないデータとなる。<br />
<br />
自分でSQL文を直接書いている場合には考えられない現象だが、フレームワークを用いてSQL文を生成している場合は、この落とし穴にはまりやすい。<br />
<br />
その場合は、多少性能が落ちるかもしれないが、各取得結果をテーブルとみなしたサブクエリを用いて、取得項目の並びを指定して取得しなおすという方法が有効でした。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20UNION%E3%81%AE%E8%90%BD%E3%81%A8%E3%81%97%E7%A9%B4%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F33p3eu3" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/08/union%e3%81%ae%e8%90%bd%e3%81%a8%e3%81%97%e7%a9%b4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysqlのサブクエリとgroup、orderの優先度</title>
		<link>http://life.co-hey.com/2010/08/mysql%e3%81%ae%e3%82%b5%e3%83%96%e3%82%af%e3%82%a8%e3%83%aa%e3%81%a8group%e3%80%81order%e3%81%ae%e5%84%aa%e5%85%88%e5%ba%a6/</link>
		<comments>http://life.co-hey.com/2010/08/mysql%e3%81%ae%e3%82%b5%e3%83%96%e3%82%af%e3%82%a8%e3%83%aa%e3%81%a8group%e3%80%81order%e3%81%ae%e5%84%aa%e5%85%88%e5%ba%a6/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 02:51:26 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[web & pc]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=775</guid>
		<description><![CDATA[Mysqlを使ってシステムを組んでいると、いろんな条件でデータを取得したい場面がでてくる。その中で、使用頻度が高いのがgroup by、order by、サブクエリの組み合わせ。 group byをすると、指定したキーで [...]]]></description>
			<content:encoded><![CDATA[<p>
Mysqlを使ってシステムを組んでいると、いろんな条件でデータを取得したい場面がでてくる。その中で、使用頻度が高いのがgroup by、order by、サブクエリの組み合わせ。<br />
<br />
group byをすると、指定したキーで一番最初に現れたものが残り、後から現れたものは削除される。order by と併用した場合は、group by した結果に対して、order byが適用される。<br />
<br />
order by を適用した後に、group by を適用したい場合は、サブクエリを用いてorder by を実行するSQLをテーブルとみなして、group by をかけるという方法になる。<br />
<br />
例えば、mixiのようなSNSで各ユーザの最新の日記を取得したい場合は、日記テーブルを作成日で、降順にソートするSQLをテーブルとみなして、ユーザIDでgroup by をかけるというサブクエリを利用すれば可能。<br />
<br />
group by と order by だと group by が優先されるというのは、結構覚えておくと役にたつことが多かったです。<br />
<br />
</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Mysql%E3%81%AE%E3%82%B5%E3%83%96%E3%82%AF%E3%82%A8%E3%83%AA%E3%81%A8group%E3%80%81order%E3%81%AE%E5%84%AA%E5%85%88%E5%BA%A6%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F28vk9pz" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/08/mysql%e3%81%ae%e3%82%b5%e3%83%96%e3%82%af%e3%82%a8%e3%83%aa%e3%81%a8group%e3%80%81order%e3%81%ae%e5%84%aa%e5%85%88%e5%ba%a6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MYSQLのUNIONとUNION ALLの違い</title>
		<link>http://life.co-hey.com/2010/08/mysql%e3%81%aeunion%e3%81%a8union-all%e3%81%ae%e9%81%95%e3%81%84/</link>
		<comments>http://life.co-hey.com/2010/08/mysql%e3%81%aeunion%e3%81%a8union-all%e3%81%ae%e9%81%95%e3%81%84/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 04:20:13 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[web & pc]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=764</guid>
		<description><![CDATA[今やっているプログラムで使わざるを得なくなったUNION構文。 UNIONとUNION ALL何が違うんだろう？と調べてみました。 SQLをわかっている方向けに説明すると、行全体にdistinctをかけるのがUNIONで [...]]]></description>
			<content:encoded><![CDATA[<p>
今やっているプログラムで使わざるを得なくなったUNION構文。<br />
UNIONとUNION ALL何が違うんだろう？と調べてみました。<br />
<br />
SQLをわかっている方向けに説明すると、行全体にdistinctをかけるのがUNIONで、かけないのがUNION ALL。<br />
<br />
つまりUNIONで連結した場合、全てのカラムの値が同じ行が複数あった場合、１行を残してすべてまとめられます。<br />
<br />

<div class="wp_codebox"><table><tr id="p76414"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p764code14"><pre class="sql" style="font-family:Osaka-mono,monospace;"><span style="color: #808080; font-style: italic;">----------------------</span>
<span style="color: #66cc66;">|</span>　col_a　<span style="color: #66cc66;">|</span>　col_b　<span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">----------------------</span>
<span style="color: #66cc66;">|</span>　　<span style="color: #cc66cc;">0</span>　　<span style="color: #66cc66;">|</span>　　a　　<span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">----------------------</span></pre></td></tr></table></div>

<br />
こんな取得結果をUNIONとUNION ALLにて連結した場合。<br />
<br />

<div class="wp_codebox"><table><tr id="p76415"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p764code15"><pre class="sql" style="font-family:Osaka-mono,monospace;"><span style="color: #808080; font-style: italic;">----------------------</span>
<span style="color: #66cc66;">|</span>　col_a　<span style="color: #66cc66;">|</span>　col_b　<span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">----------------------</span>
<span style="color: #66cc66;">|</span>　　<span style="color: #cc66cc;">0</span>　　<span style="color: #66cc66;">|</span>　　a　　<span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">----------------------</span></pre></td></tr></table></div>

<br />
これがUNION<br />
<br />

<div class="wp_codebox"><table><tr id="p76416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p764code16"><pre class="sql" style="font-family:Osaka-mono,monospace;"><span style="color: #808080; font-style: italic;">----------------------</span>
<span style="color: #66cc66;">|</span>　col_a　<span style="color: #66cc66;">|</span>　col_b　<span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">----------------------</span>
<span style="color: #66cc66;">|</span>　　<span style="color: #cc66cc;">0</span>　　<span style="color: #66cc66;">|</span>　　a　　<span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">----------------------</span>
<span style="color: #66cc66;">|</span>　　<span style="color: #cc66cc;">0</span>　　<span style="color: #66cc66;">|</span>　　a　　<span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">----------------------</span></pre></td></tr></table></div>

<br />
これがUNION ALL</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20MYSQL%E3%81%AEUNION%E3%81%A8UNION%20ALL%E3%81%AE%E9%81%95%E3%81%84%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F292pqa7" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/08/mysql%e3%81%aeunion%e3%81%a8union-all%e3%81%ae%e9%81%95%e3%81%84/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Macにapacheとかインストール</title>
		<link>http://life.co-hey.com/2010/02/mac%e3%81%abapache%e3%81%a8%e3%81%8b%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab/</link>
		<comments>http://life.co-hey.com/2010/02/mac%e3%81%abapache%e3%81%a8%e3%81%8b%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 02:06:28 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[web & pc]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=523</guid>
		<description><![CDATA[先日、OSクリーンインストールすることになったMacの開発環境を作り直してる途中です。前は、ネットで調べながらapache、php、mysqlをソースからインストールしたんですが、今回は楽そうな記事を見つけたので、mac [...]]]></description>
			<content:encoded><![CDATA[<p>
先日、OSクリーンインストールすることになったMacの開発環境を作り直してる途中です。前は、ネットで調べながらapache、php、mysqlをソースからインストールしたんですが、今回は楽そうな記事を見つけたので、macportでインストール。<br />
<br />
参考にさせていただきた記事はこちらです。<br />
<a href="http://www.serendip.ws/archives/3234">http://www.serendip.ws/archives/3234</a><br />
<br />
とりあえず、mysqlコマンドでDbにつながること、ブラウザでページが見えることを確認したところまで。php経由で、DBに接続できたら家でまたプログラムを作れるようになります。会社でVM使っているからか、VM Fusionが欲しくなったけど、何も考えなかったことにしました。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Mac%E3%81%ABapache%E3%81%A8%E3%81%8B%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%20-%20Life%20http%3A%2F%2Ftinyurl.com%2Fyhpl2pq" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/02/mac%e3%81%abapache%e3%81%a8%e3%81%8b%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend_Db_Tableを利用して複数DBサーバを参照する</title>
		<link>http://life.co-hey.com/2009/12/zend_db_table%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e8%a4%87%e6%95%b0db%e3%82%b5%e3%83%bc%e3%83%90%e3%82%92%e5%8f%82%e7%85%a7%e3%81%99%e3%82%8b/</link>
		<comments>http://life.co-hey.com/2009/12/zend_db_table%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e8%a4%87%e6%95%b0db%e3%82%b5%e3%83%bc%e3%83%90%e3%82%92%e5%8f%82%e7%85%a7%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 03:55:48 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/2009/12/zend_db_table%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e8%a4%87%e6%95%b0db%e3%82%b5%e3%83%bc%e3%83%90%e3%82%92%e5%8f%82%e7%85%a7%e3%81%99%e3%82%8b/</guid>
		<description><![CDATA[DBサーバの負荷分散のために、select文を発行する先のDBを複数用意して運用することがあると思います。マスター（更新＆参照）1台に、スレーブ（参照）1台とか。 今回、マスター1台、スレーブ1台の構成で、selectク [...]]]></description>
			<content:encoded><![CDATA[<p>
DBサーバの負荷分散のために、select文を発行する先のDBを複数用意して運用することがあると思います。マスター（更新＆参照）1台に、スレーブ（参照）1台とか。<br />
<br />
今回、マスター1台、スレーブ1台の構成で、selectクエリを振り分けるプログラムを作りました。Zend_Db_TableのfetchRow、fetchAll等が呼ばれる度に、クエリを実行するDBサーバを決定します。<br />
<br />
まずは、DB接続先の設定をiniファイルに定義します。<br />
接続先情報は、ダミーです。<br />
<br />

<div class="wp_codebox"><table><tr id="p45720"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p457code20"><pre class="php" style="font-family:Osaka-mono,monospace;">database<span style="color: #339933;">.</span>adapter         <span style="color: #339933;">=</span> Pdo_Mysql
database<span style="color: #339933;">.</span>params<span style="color: #339933;">.</span>host     <span style="color: #339933;">=</span> master<span style="color: #339933;">.</span>test<span style="color: #339933;">.</span>com
database<span style="color: #339933;">.</span>params<span style="color: #339933;">.</span>username <span style="color: #339933;">=</span> dbuser
database<span style="color: #339933;">.</span>params<span style="color: #339933;">.</span>password <span style="color: #339933;">=</span> hogehoge
database<span style="color: #339933;">.</span>params<span style="color: #339933;">.</span>dbname   <span style="color: #339933;">=</span> master
&nbsp;
<span style="color: #666666; font-style: italic;"># 参照DBhost
</span>database<span style="color: #339933;">.</span>reference<span style="color: #339933;">.</span>host  <span style="color: #339933;">=</span> slave1</pre></td></tr></table></div>

<br />
<br />
<br />
次に、Zend_Db_Table_Abstractと各テーブルクラスの間に入れるクラス。<br />
Zend_Db_Table_Abstractクラスの拡張クラスです。<br />
<br />

<div class="wp_codebox"><table><tr id="p45721"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code" id="p457code21"><pre class="php" style="font-family:Osaka-mono,monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> My_Db_Table_Abstract <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetchAll<span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// 2回に1回はmasterに接続</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// 2回に1回はslaveに接続</span>
        <span style="color: #000088;">$adapter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_setAdapter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReferenceAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_setAdapter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$adapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$rows</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetchRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// 2回に1回はmasterに接続</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// 2回に1回はslaveに接続</span>
        <span style="color: #000088;">$adapter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_setAdapter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReferenceAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_setAdapter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$adapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getReferenceAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">isRegistered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ref_Db_Adapter'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ref_Db_Adapter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Ini<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'↑で作ったconfigファイル名'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reference</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">host</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">adapter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFetchMode</span><span style="color: #009900;">&#40;</span>Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_ASSOC</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ref_Db_Adapter'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$db</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
<br />
<br />
テーブルクラスの定義は以下のように<br />
<br />

<div class="wp_codebox"><table><tr id="p45722"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p457code22"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000000; font-weight: bold;">class</span> Users <span style="color: #000000; font-weight: bold;">extends</span> My_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_name</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">'users'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_primary</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
<br />
<br />
これで、fetchRow、fetchAllを使う際は、masterとslaveにクエリが分散されます</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend_Db_Table%E3%82%92%E5%88%A9%E7%94%A8%E3%81%97%E3%81%A6%E8%A4%87%E6%95%B0DB%E3%82%B5%E3%83%BC%E3%83%90%E3%82%92%E5%8F%82%E7%85%A7%E3%81%99%E3%82%8B%20-%20Life%20http%3A%2F%2Ftinyurl.com%2Fylqfeol" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2009/12/zend_db_table%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e8%a4%87%e6%95%b0db%e3%82%b5%e3%83%bc%e3%83%90%e3%82%92%e5%8f%82%e7%85%a7%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQLのViewを使った感想</title>
		<link>http://life.co-hey.com/2009/11/mysql%e3%81%aeview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e6%84%9f%e6%83%b3/</link>
		<comments>http://life.co-hey.com/2009/11/mysql%e3%81%aeview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e6%84%9f%e6%83%b3/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 15:59:59 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[web & pc]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=451</guid>
		<description><![CDATA[100万件以上のデータを持つDBで、Viewテーブルを使える機会があったので使ってみました。結果としては残念なことに。Viewテーブルを利用するだけでは性能改善につながりません。 Viewテーブルについて調べているときに [...]]]></description>
			<content:encoded><![CDATA[<p>
100万件以上のデータを持つDBで、Viewテーブルを使える機会があったので使ってみました。結果としては残念なことに。Viewテーブルを利用するだけでは性能改善につながりません。<br />
<br />
Viewテーブルについて調べているときに、「MySQLのViewテーブルは裏側でcreateしたときのSQL発行するだけだから、単独では性能改善につながらない」という内容の記事を見たんですが、まさにその通りの結果になりました。ViewテーブルをcreateしたときのSQLでの検索性能と、Viewテーブルを使って同様のデータを取得しようとしたときの性能はほぼ一緒。改善された様子はありませんでした。<br />
<br />
DBにOracleを使っていたときは、性能改善の手段として、まず変数のbind化やストアドプロシージャと供にViewテーブルを使って性能改善をしていた記憶があるので、Viewテーブルに対して検索をかけたときは単一テーブルに対して検索かけた程度の性能が期待できるのかと勘違いしていました。OracleではSQL発行の窓口としてのViewテーブルではなく、実際にデータを持っていたんでしょうか。（もう５年近く前の経験なので、今は全く変わっているかもしれません。Oracle）</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20MySQL%E3%81%AEView%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E6%84%9F%E6%83%B3%20-%20Life%20http%3A%2F%2Ftinyurl.com%2Fyjpnv8r" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2009/11/mysql%e3%81%aeview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e6%84%9f%e6%83%b3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Db_Tableでの勘違い</title>
		<link>http://life.co-hey.com/2009/09/zend_db_table%e3%81%a7%e3%81%ae%e5%8b%98%e9%81%95%e3%81%84/</link>
		<comments>http://life.co-hey.com/2009/09/zend_db_table%e3%81%a7%e3%81%ae%e5%8b%98%e9%81%95%e3%81%84/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 10:38:38 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/2009/09/zend_db_table%e3%81%a7%e3%81%ae%e5%8b%98%e9%81%95%e3%81%84/</guid>
		<description><![CDATA[Zend Framework: Documentation Zend_Db_Table で UPDATE や DELETE の連鎖操作をエミュレートする場合は、 配列 $_dependentTables を親テーブルで宣 [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://framework.zend.com/manual/ja/zend.db.table.relationships.html#zend.db.table.relationships.defining">Zend Framework: Documentation</a><br />
</p>
<blockquote><p>Zend_Db_Table で UPDATE や DELETE の連鎖操作をエミュレートする場合は、 配列 $_dependentTables を親テーブルで宣言し、 従属しているテーブルをそこで指定します。<br />
<br />
各従属テーブルのクラス内で、配列 $_referenceMap  を宣言します。これは、参照の &#8220;ルール&#8221; を定義する連想配列となります。 参照ルールとは、リレーションの親テーブルが何になるのか、 従属テーブルのどのカラムと親テーブルのどのカラムが対応するのかを示すものです。 <br />
</p></blockquote>
<p>
<br />
引用した１つめの文章を読み飛ばしてしまっていたため、find～を使うためには、$_referenceMapと$_dependentTablesは、対を成す形式で記述するものだと勘違いしていました。onUpdateとかonDeleteをZendを使って実現しなければ、$_dependentTablesは不要ってことですね。<br />
<br />
これだけのことなんですが、かなり「はっ！！」としました。<br />
作っているプログラムの行が少なくなることは見やすくなっていいことです。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend_Db_Table%E3%81%A7%E3%81%AE%E5%8B%98%E9%81%95%E3%81%84%20-%20Life%20http%3A%2F%2Ftinyurl.com%2Fyzbkzyl" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2009/09/zend_db_table%e3%81%a7%e3%81%ae%e5%8b%98%e9%81%95%e3%81%84/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

