如何缩短加载时间?

时间:2014-06-08 22:55:07

标签: sql ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

使用此代码完成加载大约需要30秒。

我该如何缩短它?有什么好主意吗? 它似乎正在检索所有类型的数据。

我在这里使用了4张表。

  1. 用户 - 用户有一个UserProfile,多个代码
  2. UserProfile - UserProfile属于用户
  3. 代码 - 代码属于用户,代码属于社区
  4. 社区 - 社区有很多代码
  5. 代码

    @communities = current_user.get_up_voted(Community)
    @codes = Code.includes(:user).where(:community_id => @communities.collect(&:id)).order('users.last_active_at DESC').page(params[:page]).per(10)
    

    它输出这个SQL查询(为什么它必须这么长?)

    SELECT  `codes`.`id` AS t0_r0, `codes`.`user_id` AS t0_r1, `codes`.`community_id` AS t0_r2, `codes`.`invisible` AS t0_r3, `codes`.`code` AS t0_r4, `codes`.`greeting` AS t0_r5, `codes`.`created_at` AS t0_r6, `codes`.`updated_at` AS t0_r7, `codes`.`deleted_at` AS t0_r8, `codes`.`notification` AS t0_r9, `codes`.`wanted` AS t0_r10, `users`.`id` AS t1_r0, `users`.`email` AS t1_r1, `users`.`encrypted_password` AS t1_r2, `users`.`username` AS t1_r3, `users`.`reset_password_token` AS t1_r4, `users`.`reset_password_sent_at` AS t1_r5, `users`.`remember_created_at` AS t1_r6, `users`.`sign_in_count` AS t1_r7, `users`.`current_sign_in_at` AS t1_r8, `users`.`last_sign_in_at` AS t1_r9, `users`.`current_sign_in_ip` AS t1_r10, `users`.`last_sign_in_ip` AS t1_r11, `users`.`banned` AS t1_r12, `users`.`confirmation_token` AS t1_r13, `users`.`confirmed_at` AS t1_r14, `users`.`confirmation_sent_at` AS t1_r15, `users`.`unconfirmed_email` AS t1_r16, `users`.`created_at` AS t1_r17, `users`.`updated_at` AS t1_r18, `users`.`deleted_at` AS t1_r19, `users`.`last_active_at` AS t1_r20, `users`.`comments_count` AS t1_r21, `users`.`follows_count` AS t1_r22, `users`.`codes_count` AS t1_r23, `users`.`communities_count` AS t1_r24, `users`.`nomail` AS t1_r25, `users`.`point_added_at` AS t1_r26, `user_profiles`.`id` AS t2_r0, `user_profiles`.`user_id` AS t2_r1, `user_profiles`.`language_id` AS t2_r2, `user_profiles`.`country_id` AS t2_r3, `user_profiles`.`prefecture_id` AS t2_r4, `user_profiles`.`gender_id` AS t2_r5, `user_profiles`.`nickname` AS t2_r6, `user_profiles`.`introduction` AS t2_r7, `user_profiles`.`picture_url` AS t2_r8, `user_profiles`.`created_at` AS t2_r9, `user_profiles`.`updated_at` AS t2_r10, `user_profiles`.`deleted_at` AS t2_r11, `user_profiles`.`user_avatar_file_name` AS t2_r12, `user_profiles`.`user_avatar_content_type` AS t2_r13, `user_profiles`.`user_avatar_file_size` AS t2_r14, `user_profiles`.`user_avatar_updated_at` AS t2_r15, `user_profiles`.`age` AS t2_r16, `user_profiles`.`activity_invisible` AS t2_r17, `user_profiles`.`total_point` AS t2_r18, `user_profiles`.`bonus_point` AS t2_r19, `user_profiles`.`introduction_html` AS t2_r20, `user_profiles`.`title` AS t2_r21, `user_profiles`.`next_level` AS t2_r22, `user_profiles`.`invitation` AS t2_r23, `user_profiles`.`notification` AS t2_r24, `user_profiles`.`notification_time` AS t2_r25, `user_profiles`.`wall_time` AS t2_r26, `user_profiles`.`wall_flag` AS t2_r27, `user_profiles`.`wanted_at` AS t2_r28, `user_profiles`.`wanted_message` AS t2_r29 FROM `codes` LEFT OUTER JOIN `users` ON `users`.`id` = `codes`.`user_id` LEFT OUTER JOIN `user_profiles` ON `user_profiles`.`user_id` = `users`.`id` WHERE `codes`.`community_id` IN (6, 2, 9, 1, 8, 16, 18, 19, 20, 21, 22, 23, 24, 30, 29, 67, 66, 5, 87) AND (`codes`.`deleted_at` IS NULL) ORDER BY users.last_active_at DESC LIMIT 10 OFFSET 0   
    

0 个答案:

没有答案