Forum: Ruby on Rails Caching model metadata?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B733dca081901a16ac713f8e1e10023f?d=identicon&s=25 J Smith (Guest)
on 2006-03-08 23:20
(Received via mailing list)
Hi list.

Having just started with Rails, I'm probably going to ask on obvious
question. I've looked through the archives and googled every
combination of words relating to the subject at hand that I can think
of, and I've kind of got an answer, but I'd like to be absolutely sure
that the behaviour I'm seeing is the correct one.

How exactly does Rails/ActiveRecord cache metadata it fetches from the
database on table structure, column information, etc.? I'm under the
impression that it's on a per-request basis -- once it has the
metadata, ActiveRecord won't fetch the same metadata again for the
rest of the request.

Is this the only behaviour that ActiveRecord has out of the box, or is
there a way to store this data in a way that doesn't require
constantly hitting the database for it? For many of my page requests,
I seem to be spending more time looking up column information than I
actually do fetching the actual data.

My set up looks like this:

- Rails 1.0
- PostgreSQL 8.1.3
- mod_ruby 1.2.4
- apache 1.3.34

I have switched over my app to use the production settings by setting
the appropriate environment variable in environment.rb, and I've
confirmed that it's connecting to my production database by looking at
the production.log and the PostgreSQL logs, but I have noticed
absolutely no change in the number of queries compared to the
development environment. Shouldn't I be noticing a drop off in the
number of queries that involve looking up table structures and such?
(I realize it's going to depend on my database structure, but it seems
odd that there's been zero change from the development configuration.
To me, at least, and I will plainly admit I'm a noob to Rails, so my
view on the situation is obviously skewed.)

Is there any way to actually cache this metadata so I can cut down on
the number of queries that are being performed? Now that I'm in the
production environment, it doesn't make sense (again, to me) that this
metadata is constantly being fetched on every request, when it could
really be fetched once and written down in some binary file that
ActiveRecord can go to rather than trying up the database. The
metadata queries according to PostgreSQL's EXPLAIN ANALYZE statements
are actually taking longer to perform than the actual request queries
themselves, and I curious to know if a cached version of the same data
in a file would be quicker than what I'm currently experiencing.

Also, as a sidebar question dealing with performance, Rails seems to
like generating queries like this:

SELECT * FROM whatever WHERE (whatever.id = 9) LIMIT 1

Without the LIMIT clause, there's an average time savings of about 10%
to 15% per query. There's really no need for the LIMIT clause here,
since Rails should assume it's only going to get at most a single
record returned when it's querying against the primary key for the
table. 10% to 15% might not seem like much when we're dealing in
queries that take less than a tenth of a second, but they can really
add up, and every little bit helps. (It's strange that PostgreSQL's
query optimizer doesn't realize this either, so maybe I'm looking in
the wrong place for optimization...)

Wow, long first post. I'll wrap it up by saying I'm enjoying working
with Rails and Ruby so far, and that y'all should go easy on a noob.

Cheers,

J
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-08 23:32
(Received via mailing list)
active record should cache the data when running in production...it
might not be true when running webrick.
On fcgi it does cache it

On Wednesday, March 08, 2006, at 5:19 PM, J Smith wrote:
>impression that it's on a per-request basis -- once it has the
>
>development environment. Shouldn't I be noticing a drop off in the
>really be fetched once and written down in some binary file that
>
>Wow, long first post. I'll wrap it up by saying I'm enjoying working
>with Rails and Ruby so far, and that y'all should go easy on a noob.
>
>Cheers,
>
>J
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


Mikkel Bruun

www.strongside.dk    - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
This topic is locked and can not be replied to.