I’m using rack-mini-profiler in a rails application, and when it loads a page, it sometimes shows some strange queries like:
SHOW FULL FIELDS FROM `tablename`
SHOW TABLES LIKE 'tablename'
SHOW CREATE TABLE `tablename`
They say the queries are coming from the controller that handles the request, from a line which has
@model = Model.first()
These queries are not executed on successive page loads.
Why does rails generate these strange queries?
You are probably aware that Rails automagically maps fields in the database to properties in your models by convention, so that you don’t have to explicitly define “id”, “name”, etc in your ActiveRecord class implementations.
In order for your ActiveRecord models to generate the right properties, Rails must interrogate your database schema. Mysql supports the commands above in order to inspect the schema.
ActiveRecord makes the assumption that your schema won’t change during the lifetime of the application running, however, so it’s not necessary to ask these questions on subsequent requests.