如何显示在Rails控制台中运行的SQL查询?

时间:2022-06-08 00:57:51

When I run queries (e.g. MyModel.where(...) or record.associated_things) in the console, how can I see the actual database queries being run so I can gain more understanding of what is happening?

当我运行查询时(例如MyModel.where(…)或record.associated_things),我如何看到正在运行的实际数据库查询,这样我就能对正在发生的事情有更多的了解了?

4 个解决方案

#1


163  

Enter this line in the console:

在控制台输入这一行:

ActiveRecord::Base.logger = Logger.new(STDOUT)

#2


21  

In Rails 3+ you can use ActiveRecord::Relation’s to_sql method:

在Rails 3+中,您可以使用ActiveRecord: Relation的to_sql方法:

User.where(:id => 3).to_sql
#=> "SELECT \"users\".* FROM \"users\"  WHERE \"users\".\"id\" = 3"

#3


16  

There is an .explain method in Rails 4.
(.to_sql works too, but won't show includes)

Rails 4中有一个.explain方法。(。to_sql也可以工作,但不会显示包含)

Category.includes(:products).explain
=> EXPLAIN for: SELECT "categories".* FROM "categories" 0|0|0|SCAN TABLE categories

EXPLAIN for: SELECT "categories_products".* FROM "categories_products" WHERE "categories_products"."category_id" IN (1, 2) 0|0|0|SCAN TABLE categories_products

EXPLAIN for: SELECT "products".* FROM "products" WHERE "products"."id" IN (1, 2, 3, 4, 5, 6, 7) 0|0|0|SEARCH TABLE products USING INTEGER PRIMARY KEY (rowid=?) 0|0|0|EXECUTE LIST SUBQUERY 1

#4


0  

As from recently, you can use this:

从最近开始,你可以使用这个:

https://github.com/dejan/rails_panel

https://github.com/dejan/rails_panel

It consists of developer console panel add-on for chrome, and gem file which needs to be added to your application's Gemfile like this:

它由chrome的开发人员控制台面板插件和需要添加到应用程序Gemfile的gem文件组成:

group :development do
  gem 'meta_request'
end

Then run again:

然后再次运行:

bundle install

Restart your application, open it, and launch developer console, and you should see it like this: 如何显示在Rails控制台中运行的SQL查询?

重新启动应用程序,打开它,并启动开发人员控制台,您应该如下所示:

#1


163  

Enter this line in the console:

在控制台输入这一行:

ActiveRecord::Base.logger = Logger.new(STDOUT)

#2


21  

In Rails 3+ you can use ActiveRecord::Relation’s to_sql method:

在Rails 3+中,您可以使用ActiveRecord: Relation的to_sql方法:

User.where(:id => 3).to_sql
#=> "SELECT \"users\".* FROM \"users\"  WHERE \"users\".\"id\" = 3"

#3


16  

There is an .explain method in Rails 4.
(.to_sql works too, but won't show includes)

Rails 4中有一个.explain方法。(。to_sql也可以工作,但不会显示包含)

Category.includes(:products).explain
=> EXPLAIN for: SELECT "categories".* FROM "categories" 0|0|0|SCAN TABLE categories

EXPLAIN for: SELECT "categories_products".* FROM "categories_products" WHERE "categories_products"."category_id" IN (1, 2) 0|0|0|SCAN TABLE categories_products

EXPLAIN for: SELECT "products".* FROM "products" WHERE "products"."id" IN (1, 2, 3, 4, 5, 6, 7) 0|0|0|SEARCH TABLE products USING INTEGER PRIMARY KEY (rowid=?) 0|0|0|EXECUTE LIST SUBQUERY 1

#4


0  

As from recently, you can use this:

从最近开始,你可以使用这个:

https://github.com/dejan/rails_panel

https://github.com/dejan/rails_panel

It consists of developer console panel add-on for chrome, and gem file which needs to be added to your application's Gemfile like this:

它由chrome的开发人员控制台面板插件和需要添加到应用程序Gemfile的gem文件组成:

group :development do
  gem 'meta_request'
end

Then run again:

然后再次运行:

bundle install

Restart your application, open it, and launch developer console, and you should see it like this: 如何显示在Rails控制台中运行的SQL查询?

重新启动应用程序,打开它,并启动开发人员控制台,您应该如下所示: