ActiveRecord generates expensive DESCRIBE queries for the has_and_belongs_to_many relationship with MySQL

We have a Rails3 application where we just started development. We use the has_and_belongs_to_many relationship between the two models. Each time we use one of these models, ActiveRecord executes a DESCRIBE query in the connection table, for example:

SQL (1.1ms)  describe `articles_tags`

These queries are relatively expensive (that is, more than 10 times slower than the actual SELECT query) and are executed very often. This does not seem to me necessary: ​​the table was automatically generated due to the has_and_belongs_to_many relationship, so ActiveRecord should already know about its structure (just the column article_id and tag_id).

Is there a good reason for this behavior? If not, how can I stop him?

+3
source share
2 answers

In development mode, the server constantly recreates all classes, so you do not need to restart the server for each change. In production, your models will be cached and need not constantly describe tables. In the development.rb file, you will see the following line:

config.cache_classes = false

This leads to this behavior. It must be set to true. Production.rb

+4
source

If your table does not have a primary key, it will work to describe each query. I fixed the addition of the primary key. It works even in dev mode.

+1
source