在Rails中,有一个有用的方法可以显示查询需要多长时间以及查询的内容。
方法是'解释'
所以我能做到:
User.where(name: 'Johnny').explain
它将显示实际的sql查询。
答案 0 :(得分:1)
这有效:
User.select('count(*)').where(name: 'Johnny').explain
编辑:这确实完全相同:
irb(main):004:0> Benutzer.where(login: 'xxx').count
(2.2ms) SELECT COUNT(*) FROM "BENUTZER" WHERE "BENUTZER"."LOGIN" = 'xxx'
=> 0
irb(main):005:0> Benutzer.select("count(*)").where(login: 'xxx').explain
Benutzer Load (0.9ms) SELECT count(*) FROM "BENUTZER" WHERE "BENUTZER"."LOGIN" = 'xxx'
=> EXPLAIN for: SELECT count(*) FROM "BENUTZER" WHERE "BENUTZER"."LOGIN" = 'xxx'
Plan hash value: 1339361075
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 12 | | |
|* 2 | TABLE ACCESS FULL| BENUTZER | 1 | 12 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("BENUTZER"."LOGIN"='xxx')
Note
-----
- dynamic sampling used for this statement (level=2)