-
Petrik authored
`explain` can be called on a relation to explain how the database would execute a query. Currently `explain` doesn't work for queries using `last`, `pluck` or `count`, as these return the actual result instead of a relation. This makes it difficult to optimize these queries. By letting `explain` return a proxy instead, we can support these methods. ```ruby User.all.explain.count \# => "EXPLAIN SELECT COUNT(*) FROM `users`" User.all.explain.maximum(:id) \# => "EXPLAIN SELECT MAX(`users`.`id`) FROM `users`" ``` This breaks the existing behaviour in that it requires calling inspect after explain. ```ruby User.all.explain.inspect \# => "EXPLAIN SELECT `users`.* FROM `users`" ``` However, as `explain` is mostly used from the commandline, this won't be a problem as inspect is called automatically in IRB. Co-authored-by:
Rafael Mendonça França <rafael@rubyonrails.org>
Petrik authored`explain` can be called on a relation to explain how the database would execute a query. Currently `explain` doesn't work for queries using `last`, `pluck` or `count`, as these return the actual result instead of a relation. This makes it difficult to optimize these queries. By letting `explain` return a proxy instead, we can support these methods. ```ruby User.all.explain.count \# => "EXPLAIN SELECT COUNT(*) FROM `users`" User.all.explain.maximum(:id) \# => "EXPLAIN SELECT MAX(`users`.`id`) FROM `users`" ``` This breaks the existing behaviour in that it requires calling inspect after explain. ```ruby User.all.explain.inspect \# => "EXPLAIN SELECT `users`.* FROM `users`" ``` However, as `explain` is mostly used from the commandline, this won't be a problem as inspect is called automatically in IRB. Co-authored-by:
Rafael Mendonça França <rafael@rubyonrails.org>
Loading