开发者

Why is this code slower?

开发者 https://www.devze.com 2023-02-13 14:11 出处:网络
Just updating my code to be Rails 3 ready, however I\'m experiencing a major performance issue with the code below.

Just updating my code to be Rails 3 ready, however I'm experiencing a major performance issue with the code below.

Old code (nice and quick)

@products = Product.all(
                        :order => 'name',
                        :include => [:category, :brand, :merchant]
                       ).paginate(:page => params[:page])

New code (10 x slower)

@products = Product.order("name")
                   .includes([:cat开发者_运维知识库egory, :brand, :merchant])
                   .paginate(:page => params[:page])

I added the line breaks for easy reading.

I'm using Postgres for my DB, maybe this is the issue?

Any other tips to make this code better will be much appreciated!


I'd check the outputted SQL (from logs or the console) to see if something's changed and/or how the query might be optimized.


Can it be a problem with the pagination? Which plugin are you using for pagination? You might want to try Kaminari.

0

精彩评论

暂无评论...
验证码 换一张
取 消