{"action":"opened","issue":{"url":"https://api.github.com/repos/rails/rails/issues/21590","labels_url":"https://api.github.com/repos/rails/rails/issues/21590/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/21590/comments","events_url":"https://api.github.com/repos/rails/rails/issues/21590/events","html_url":"https://github.com/rails/rails/issues/21590","id":106011176,"number":21590,"title":"ActiveRecord raw sql very slower than MySql","user":{"login":"AugustoPedraza","id":1100414,"avatar_url":"https://avatars.githubusercontent.com/u/1100414?v=3","gravatar_id":"","url":"https://api.github.com/users/AugustoPedraza","html_url":"https://github.com/AugustoPedraza","followers_url":"https://api.github.com/users/AugustoPedraza/followers","following_url":"https://api.github.com/users/AugustoPedraza/following{/other_user}","gists_url":"https://api.github.com/users/AugustoPedraza/gists{/gist_id}","starred_url":"https://api.github.com/users/AugustoPedraza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AugustoPedraza/subscriptions","organizations_url":"https://api.github.com/users/AugustoPedraza/orgs","repos_url":"https://api.github.com/users/AugustoPedraza/repos","events_url":"https://api.github.com/users/AugustoPedraza/events{/privacy}","received_events_url":"https://api.github.com/users/AugustoPedraza/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-09-11T13:28:47Z","updated_at":"2015-09-11T13:28:47Z","closed_at":null,"body":"I'm running a simple raw MySql query with ActiveRecord(*#select_all* method) and the time used to return result is slower that running the same query directly in MySql Workbench.\r\n\r\nThe query is the following:\r\n\r\n```sql\r\nSELECT \r\n      CASE \r\n        WHEN\r\n        WeekDay(c.collect_date) >=0 AND WeekDay(c.collect_date) <5 THEN 1\r\n      ELSE 0 \r\n      END AS is_business_day, \r\n      HOUR(c.collect_date) consumption_hour, \r\n        SUM(c.energy), \r\n        AVG(c.power) \r\n    FROM consumptions c\r\n    INNER JOIN devices d ON c.device_id=d.device_id AND d.ftp_id=1    \r\n    GROUP BY CASE WHEN\r\n        WeekDay(c.collect_date) >=0 AND WeekDay(c.collect_date) <5 THEN 1\r\n      ELSE 0 \r\n      END, HOUR(c.collect_date)\r\n```\r\n\r\nFor your information, the *consumptions* table have ~3 millions of records and the devices have < 1000 records.\r\n\r\nThe times consumed for every approach are:\r\n\r\n  - ActiveRecord: **33.6** seconds.\r\n  - Mysql Bench: **14.0** seconds.\r\n\r\nThe result of the query are just ~100 records.\r\n\r\nShould I consider it a bug?"}}