{"action":"created","issue":{"url":"https://api.github.com/repos/ManageIQ/manageiq/issues/10019","repository_url":"https://api.github.com/repos/ManageIQ/manageiq","labels_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/10019/labels{/name}","comments_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/10019/comments","events_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/10019/events","html_url":"https://github.com/ManageIQ/manageiq/pull/10019","id":167272080,"number":10019,"title":"Fix EmsCluster.total_hosts to avoid n+1","user":{"login":"kbrock","id":1930,"avatar_url":"https://avatars.githubusercontent.com/u/1930?v=3","gravatar_id":"","url":"https://api.github.com/users/kbrock","html_url":"https://github.com/kbrock","followers_url":"https://api.github.com/users/kbrock/followers","following_url":"https://api.github.com/users/kbrock/following{/other_user}","gists_url":"https://api.github.com/users/kbrock/gists{/gist_id}","starred_url":"https://api.github.com/users/kbrock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kbrock/subscriptions","organizations_url":"https://api.github.com/users/kbrock/orgs","repos_url":"https://api.github.com/users/kbrock/repos","events_url":"https://api.github.com/users/kbrock/events{/privacy}","received_events_url":"https://api.github.com/users/kbrock/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ManageIQ/manageiq/labels/core","name":"core","color":"5319e7"},{"url":"https://api.github.com/repos/ManageIQ/manageiq/labels/darga/no","name":"darga/no","color":"000000"},{"url":"https://api.github.com/repos/ManageIQ/manageiq/labels/performance","name":"performance","color":"fad8c7"}],"state":"open","locked":false,"assignee":{"login":"gtanzillo","id":76593,"avatar_url":"https://avatars.githubusercontent.com/u/76593?v=3","gravatar_id":"","url":"https://api.github.com/users/gtanzillo","html_url":"https://github.com/gtanzillo","followers_url":"https://api.github.com/users/gtanzillo/followers","following_url":"https://api.github.com/users/gtanzillo/following{/other_user}","gists_url":"https://api.github.com/users/gtanzillo/gists{/gist_id}","starred_url":"https://api.github.com/users/gtanzillo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gtanzillo/subscriptions","organizations_url":"https://api.github.com/users/gtanzillo/orgs","repos_url":"https://api.github.com/users/gtanzillo/repos","events_url":"https://api.github.com/users/gtanzillo/events{/privacy}","received_events_url":"https://api.github.com/users/gtanzillo/received_events","type":"User","site_admin":false},"assignees":[{"login":"gtanzillo","id":76593,"avatar_url":"https://avatars.githubusercontent.com/u/76593?v=3","gravatar_id":"","url":"https://api.github.com/users/gtanzillo","html_url":"https://github.com/gtanzillo","followers_url":"https://api.github.com/users/gtanzillo/followers","following_url":"https://api.github.com/users/gtanzillo/following{/other_user}","gists_url":"https://api.github.com/users/gtanzillo/gists{/gist_id}","starred_url":"https://api.github.com/users/gtanzillo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gtanzillo/subscriptions","organizations_url":"https://api.github.com/users/gtanzillo/orgs","repos_url":"https://api.github.com/users/gtanzillo/repos","events_url":"https://api.github.com/users/gtanzillo/events{/privacy}","received_events_url":"https://api.github.com/users/gtanzillo/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/ManageIQ/manageiq/milestones/38","html_url":"https://github.com/ManageIQ/manageiq/milestones/Sprint%2044%20Ending%20Aug%201,%202016","labels_url":"https://api.github.com/repos/ManageIQ/manageiq/milestones/38/labels","id":1877736,"number":38,"title":"Sprint 44 Ending Aug 1, 2016","description":"","creator":{"login":"martinpovolny","id":51095,"avatar_url":"https://avatars.githubusercontent.com/u/51095?v=3","gravatar_id":"","url":"https://api.github.com/users/martinpovolny","html_url":"https://github.com/martinpovolny","followers_url":"https://api.github.com/users/martinpovolny/followers","following_url":"https://api.github.com/users/martinpovolny/following{/other_user}","gists_url":"https://api.github.com/users/martinpovolny/gists{/gist_id}","starred_url":"https://api.github.com/users/martinpovolny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martinpovolny/subscriptions","organizations_url":"https://api.github.com/users/martinpovolny/orgs","repos_url":"https://api.github.com/users/martinpovolny/repos","events_url":"https://api.github.com/users/martinpovolny/events{/privacy}","received_events_url":"https://api.github.com/users/martinpovolny/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":262,"state":"open","created_at":"2016-07-12T07:08:07Z","updated_at":"2016-07-27T19:40:54Z","due_on":"2016-08-01T04:00:00Z","closed_at":null},"comments":3,"created_at":"2016-07-25T02:46:48Z","updated_at":"2016-07-27T19:41:02Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/ManageIQ/manageiq/pulls/10019","html_url":"https://github.com/ManageIQ/manageiq/pull/10019","diff_url":"https://github.com/ManageIQ/manageiq/pull/10019.diff","patch_url":"https://github.com/ManageIQ/manageiq/pull/10019.patch"},"body":"I was looking at the data to figure out how many hosts were per cluster\r\n\r\n```ruby\r\nEmsCluster.group(:id)\r\n          .select(:id, EmsCluster.arel_attribute(:total_hosts).as(\"total_hosts\"))\r\n          .map {|x| [x.id, x.total_hosts]} }\r\n```\r\n\r\nI was surprised to see an N+1\r\n\r\nIt looks like the method `total_hosts` was manually defined. I removed this and 2 other virtual totals. There are specs in `ems_cluster_spec.rb` that exercise all 3 of these methods.\r\n\r\nbefore\r\n-------\r\n\r\n|      @ |    ms | bytes | objects |   ms- |queries | query (ms) |     rows |`comments`\r\n|    ---:|   ---:|   ---:|   ---:|   ---:|  ---:|   ---:|      ---:| ---\r\n|    0.0 |  60.6 | 1,493,715 | 19,254 |   0.0 |   21 |   9.0 |       20 |`before`\r\n|   26.8 |   1.4 |       |       |       |    1 |   1.2 |       20 |`.SELECT \"ems_clusters\".\"id\", ((SELECT COUNT(*) FROM \"hosts\" WHERE \"ems_clusters\".\"id\" = \"hosts\".\"ems_cluster_id\")) AS total_hosts FROM \"ems_clusters\" GROUP BY \"ems_clusters\".\"id\" []`\r\n|   29.7 |   6 |       |       |       |    20|   6 |          |`.SELECT COUNT(*) FROM \"hosts\" WHERE \"hosts\".\"ems_cluster_id\" = $1 [8]`\r\n\r\nafter\r\n-----\r\n\r\n|      @ |    ms | bytes | objects |   ms- |queries | query (ms) |     rows |`comments`\r\n|    ---:|   ---:|   ---:|   ---:|   ---:|  ---:|   ---:|      ---:| ---\r\n|    0.0 |  24.1 | 67,426 |   794 |   0.0 |    1 |   2.1 |       20 |`after`\r\n|   21.9 |   2.1 |       |       |       |   1   |   1.1 |       20 |`.SELECT \"ems_clusters\".\"id\", ((SELECT COUNT(*) FROM \"hosts\" WHERE \"ems_clusters\".\"id\" = \"hosts\".\"ems_cluster_id\")) AS total_hosts FROM \"ems_clusters\" GROUP BY \"ems_clusters\".\"id\" []`\r\n"},"comment":{"url":"https://api.github.com/repos/ManageIQ/manageiq/issues/comments/235696859","html_url":"https://github.com/ManageIQ/manageiq/pull/10019#issuecomment-235696859","issue_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/10019","id":235696859,"user":{"login":"gtanzillo","id":76593,"avatar_url":"https://avatars.githubusercontent.com/u/76593?v=3","gravatar_id":"","url":"https://api.github.com/users/gtanzillo","html_url":"https://github.com/gtanzillo","followers_url":"https://api.github.com/users/gtanzillo/followers","following_url":"https://api.github.com/users/gtanzillo/following{/other_user}","gists_url":"https://api.github.com/users/gtanzillo/gists{/gist_id}","starred_url":"https://api.github.com/users/gtanzillo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gtanzillo/subscriptions","organizations_url":"https://api.github.com/users/gtanzillo/orgs","repos_url":"https://api.github.com/users/gtanzillo/repos","events_url":"https://api.github.com/users/gtanzillo/events{/privacy}","received_events_url":"https://api.github.com/users/gtanzillo/received_events","type":"User","site_admin":false},"created_at":"2016-07-27T19:41:02Z","updated_at":"2016-07-27T19:41:02Z","body":"👍 "}}
{"actor":{"display_login":"gtanzillo"}}