Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize difficult query (possibly with squeel)
    text
    copied!<p>There is such code(using PublicActivity gem &amp; Squeel)</p> <pre><code> def index @activities = Activity.limit(20).order { created_at.desc } @one = @activities.where{trackable_type == 'Post'}.includes(trackable: [:author, :project]) @two = @activities.where{trackable_type == 'Project'}.includes trackable: [:owner] @activities = @one + @two end </code></pre> <p>But it creates <strong>8</strong> SQL requests:</p> <pre><code> SELECT "activities".* FROM "activities" WHERE "activities"."trackable_type" = 'Post' ORDER BY "activities"."created_at" DESC LIMIT 20 SELECT "posts".* FROM "posts" WHERE "posts"."id" IN (800, 799, 798, 797, 796, 795, 794, 793, 792, 791, 790, 789, 788, 787, 786, 785, 784, 783, 782, 781) SELECT "users".* FROM "users" WHERE "users"."id" IN (880, 879, 878, 877, 876, 875, 874, 873, 872, 871, 869, 868, 867, 866, 865, 864, 863, 862, 861, 860) SELECT "projects".* FROM "projects" WHERE "projects"."id" IN (80, 79) SELECT "activities".* FROM "activities" WHERE "activities"."trackable_type" = 'Project' ORDER BY "activities"."created_at" DESC LIMIT 20 SELECT "projects".* FROM "projects" WHERE "projects"."id" IN (80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61) SELECT "users".* FROM "users" WHERE "users"."id" IN (870, 859, 848, 837, 826, 815, 804, 793, 782, 771, 760, 749, 738, 727, 716, 705, 694, 683, 672, 661) </code></pre> <ol> <li>activites request are not joined</li> <li>some users (post owner and project owner) are loaded twice</li> <li>some projects are loaded twice</li> <li>@activities is Array. Rails relations merge methods(except <code>+</code>) don't work with the code above.</li> </ol> <p>Any ideas to optimize it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload