has_manyの
will_paginateでちょっとイケてないSQLが吐かれるというのは
いろんなブログで書かれています。具体的には、ページ内で表示するのが10件だとしても、一度全件持ってくるSQLを発行します。そこで、別のpaginate pluginである、
paginating_findを試してみました。
結果的には paginating_find だと、無駄なSQLが吐かれません。(count(*)は無駄だと思うが・・) でも、、railsのコード的には will_paginate の方が好きなんだよな。。
両方のサンプルと吐かれるSQLを下記しておきます。
ここでのサンプルは、ユーザが書いたコメントを表示するページです。
コメントは他のユーザにも表示するため、ユーザとコメントの関係は 多対多 です。
will_paginateのcontroller
@user = User.find(params[:id])
@comments = @user.comments.paginate(:page=>params[:page],:per_page=>10)
paginating_findのcontroller
@user = User.find(params[:id])
@comments = @user.comments.find(:all,:page=>{:size=>10,:current=>params[:page]})
will_paginateが吐くSQL
SELECT * FROM comments INNER JOIN comments_users ON comments.id = comments_users.comment_id WHERE (comments_users.user_id = '123' )
SELECT count(*) AS count_all FROM comments INNER JOIN comments_users ON comments.id = comments_users.comment_id WHERE (( comments_users.user_id = '123' ) AND ( comments_users.user_id = '123' ))
SELECT * FROM comments INNER JOIN comments_users ON comments.id = comments_users.comment_id WHERE (comments_users.user_id = '123' ) LIMIT 0, 10
paginating_findが吐くSQL
SELECT count(comments.id) AS count_comments_id FROM comments INNER JOIN comments_users ON comments.id = comments_users.comment_id WHERE (comments_users.user_id = '123' )
SELECT * FROM comments INNER JOIN comments_users ON comments.id = comments_users.comment_id WHERE (comments_users.user_id = '123' ) LIMIT 0, 10
@
paginating_find@
will_paginate
# by etrojan2006 | 2007-09-04 11:36 | Ruby
Apacheの設定だけで可能。
ProxyPass / balancer://mongrel/ stickysession=jsessionid nofailover=On
ProxyPassReverse / balancer://mongrel/
ProxyPreserveHost On
<Proxy balancer://mongrel>
BalancerMember http://hogehoge.jp:10800
BalancerMember http://hogehoge.jp:10801
BalancerMember http://hogehoge.jp:10802
AuthUserFile /home/hogehoge/.htpasswd
AuthGroupFile /dev/null
AuthName "Please Enter Your Password"
AuthType Basic
Require valid-user
</Proxy>
</VirtualHost>
# by etrojan2006 | 2007-08-27 10:24 | Ruby
foo ローカル変数
@foo インスタンス変数
@@foo クラス変数
$foo グローバル変数
Foo 定数(大文字始まり)
"foo" 文字列
:foo シンボル
:foo と "foo" はかなり似ていますよね。混乱してました。
違いは
こちらのサイトを参照に。
# by etrojan2006 | 2007-08-17 18:26 | Ruby
mongrelはRailsを早く走らせてくれるWebサーバらしい。
ちょっと試してみる。
まず、mongrelはスレッド起動などが無いため、1ポート1プロセス。インターネットサービスとしては使えません。なので、後述するmongrel_cluster+Apache mod_proxyと併用します。
# rdocをインストールしてない人は
yum -y install rdoc
# mongrelのインストール
gem install mongrel --include-dependencies
cd RAILS_ROOT
mongrel_rails start -d development -p 3000 -B
mongrel_rails stop
とりあえず、これで動きます。-Bを付けるとデバッグモードです。これつけるとかなり遅い。
次にmongrel_clusterのインストール。
gem install mongrel_cluster --include-dependencies
cd RAILS_ROOT
mongrel_rails cluster::configure -e production -p 4000 -N 3
mongrel_rails cluster::start
mongrel_rails cluster::stop
一度設定ファイル(config/mongrel_cluster.yml)を作るようです。上記だとproductionに対してポート4000-4002で起動できるようになりました。この時点では、単に複数ポートでmongrelが起動しているだけなので、ロードバランスさせる必要があります。それはapacheのmod_proxyで実現。mod_proxyやらmod_proxy_balancerなどのインストールは割愛。
conf.d/proxy.conf
<VirtualHost *:12400>
ServerName mongrel.localhost:80
# アクセスログ、エラーログの出力先
CustomLog logs/proxy_access_log combined
ErrorLog logs/proxy_error_log
# リバース・プロキシの設定
ProxyPass / balancer://mongrel/ stickysession=jsessionid nofailover=On
ProxyPassReverse / balancer://mongrel/
<Proxy balancer://mongrel>
BalancerMember http://mongrel.localhost:4000
BalancerMember http://mongrel.localhost:4001
BalancerMember http://mongrel.localhost:4002
</Proxy>
</VirtualHost>
このファイルをhttpd.confでインクルードします。
ちなみに、mongrel(マングレル、モングレル)とは雑種とか混血という意味。
@
Nowhere Near - Mongrel を使ってみる
# by etrojan2006 | 2007-08-15 18:24 | Ruby
いつの間にか、RadRailsが
apatanaに統合されている。
とりあえず、開発環境の候補として試してみる事に。
基本はeclipseなので、まぁ、あまり違和感無い。
メニューが日本語だったり、が少し優しい。
HTMLやJavaScriptをコーディングしているときに、タグやメソッドがどのブラウザに対応しているか表示してくれる機能がいい。あと、JavaScriptのメジャーなライブラリは既にインストール済みで、プロジェクトを作るときに選択できたり、どちらかと言うとフロントエンドの開発に適している感じがする。
Rubyの入力補完があると、もっと使いやすいのだけれど。
とりあえず、apatanaをしばらく使ってみます。
# by etrojan2006 | 2007-08-10 14:12 | Ruby
そろそろ本格的にRailsの開発をやってみようかと。
とりあえず、自分のPCにインストール。
Download Ruby on Railsに沿ってインストールしてみることに。
まずは、Rubyのインストール。これはすんなりと。
次のにRubyGamesのインストール。これもすんなり、、と思っていると、なにやらzlib.dllが無いというエラーが。あれれ?
zlib HOme Siteからcompiled DLLをダウンロードして、zlib.dllをSystem32の下に配置。
再実行。お、今度は大丈夫だ。
それでもって、Railsのインストール。
最初のダウンロードはエラーで失敗。(一回目ってよく失敗しませんか?)2回目で無事終了。
# by etrojan2006 | 2007-08-10 11:30 | Ruby
production:
adapter: mysql
database: db01
username: user01
password: pass01
host: 192.168.1.8
# by etrojan2006 | 2007-08-09 19:13 | Ruby
DNS情報を変えて、nslookupでは変わってるのにブラウザなどで古いホストへアクセスしてる場合、DNSのキャッシュをクリアすればいい。
ipconfig /flushdns
# by etrojan2006 | 2007-07-18 14:12
build-modelで次のようなエラーが出た
build-propel.xml: Duplicate table found: propel. ...
実は、このコマンドを実行する前に、schema.ymlからschema.xmlを作っていた。
symfony propel-convert-yml-schemaxml
これがダメだったみたい。
DBレイアウトをymlで管理しているのであれば、schema.xmlは作ってはいけないらしい。両方の設定を読み込もうとして、tableが二重定義されていると怒られていたようだ。
# by etrojan2006 | 2007-07-18 11:45 | symfony
< 前のページ 次のページ >