消息查询语句优化,添加索引

This commit is contained in:
huang 2016-03-21 16:36:25 +08:00
parent 1b8680e385
commit e77a891bc6
11 changed files with 74 additions and 17 deletions

View File

@ -119,49 +119,46 @@ class UsersController < ApplicationController
# @new_message_count = forge_querys.count + forum_querys.count + course_querys.count + user_querys.count # @new_message_count = forge_querys.count + forum_querys.count + course_querys.count + user_querys.count
case params[:type] case params[:type]
when nil when nil
# 系统消息为管理员发送,我的消息中包含有系统消息
@message_alls = [] @message_alls = []
messages = MessageAll.where("(user_id =? and message_type !=?) or message_type =?" ,@user, "SystemMessage", "SystemMessage").order("created_at desc") messages = MessageAll.where("(user_id =? and message_type !=?) or message_type =?" ,@user.id, "SystemMessage", "SystemMessage").includes(:message).order("created_at desc")
messages.each do |message_all| messages.each do |message_all|
@message_alls << message_all.message @message_alls << message_all.message
end end
when 'unviewed' when 'unviewed'
@message_alls = [] @message_alls = []
messages = MessageAll.where("user_id =?", @user).order("created_at desc") messages = MessageAll.where("user_id =?", @user.id).includes(:message).order("created_at desc")
messages.each do |message_all| messages.each do |message_all|
# 在点击或者刷新消息列表后未读的消息存放在数组 # 在点击或者刷新消息列表后未读的消息存放在数组
if message_all.message_type != "SystemMessage"&& !message_all.message.nil? && message_all.message.viewed == 0 if message_all.message_type != "SystemMessage"&& !message_all.message.nil? && message_all.message.viewed == 0
@message_alls << message_all.message @message_alls << message_all.message
end end
end end
#课程相关消息 #课程相关消息
when 'homework' when 'homework'
@message_alls = CourseMessage.where("course_message_type in ('HomeworkCommon','StudentWorksScore','JournalsForMessage','StudentWork','Exercise') and user_id =?", @user).order("created_at desc") @message_alls = CourseMessage.where("course_message_type in ('HomeworkCommon','StudentWorksScore','JournalsForMessage','StudentWork','Exercise') and user_id =?", @user.id).order("created_at desc")
when 'course_message' when 'course_message'
@message_alls = CourseMessage.where("course_message_type =? and user_id =?", "Message", @user).order("created_at desc") @message_alls = CourseMessage.where("course_message_type =? and user_id =?", "Message", @user.id).order("created_at desc")
when 'course_news' when 'course_news'
# 课程通知包含发布的通知和回复的通知 # 课程通知包含发布的通知和回复的通知
@message_alls = CourseMessage.where("course_message_type in (?, ? ,?)", "News", "Comment", "Course").where("user_id =?", @user).order("created_at desc") @message_alls = CourseMessage.where("course_message_type in ('News', 'Comment') and user_id =?", @user.id).order("created_at desc")
when 'poll' when 'poll'
@message_alls = CourseMessage.where("course_message_type =? and user_id =?", "Poll", @user).order("created_at desc") @message_alls = CourseMessage.where("course_message_type =? and user_id =?", "Poll", @user.id).order("created_at desc")
#项目相关消息 #项目相关消息
when 'issue' when 'issue'
@message_alls = ForgeMessage.where("forge_message_type =? or forge_message_type =?" , "Issue", "Journal").where("user_id=?", @user).order("created_at desc") @message_alls = ForgeMessage.where("forge_message_type in ('Issue', 'Journal') and user_id =?" , @user.id).order("created_at desc")
when 'forge_message' when 'forge_message'
@message_alls = ForgeMessage.where("forge_message_type =? and user_id =?", "Message", @user).order("created_at desc") @message_alls = ForgeMessage.where("forge_message_type =? and user_id =?", "Message", @user.id).order("created_at desc")
when 'forge_news' when 'forge_news'
@message_alls = ForgeMessage.where("forge_message_type in (?,?) and user_id =?", "News", "Comment", @user).order("created_at desc") @message_alls = ForgeMessage.where("forge_message_type in (?,?) and user_id =?", "News", "Comment", @user.id).order("created_at desc")
when 'apply' when 'apply'
@message_alls = ForgeMessage.where("forge_message_type in ('ProjectInvite', 'AppliedProject', 'JoinProject', 'RemoveFromProject') and user_id =?", @user).order("created_at desc") @message_alls = ForgeMessage.where("forge_message_type in ('ProjectInvite', 'AppliedProject', 'JoinProject', 'RemoveFromProject') and user_id =?", @user.id).order("created_at desc")
#贴吧消息 #贴吧消息
when 'forum' when 'forum'
@message_alls = MemoMessage.where("memo_type =? and user_id =?", "Memo", @user).order("created_at desc") @message_alls = MemoMessage.where("memo_type =? and user_id =?", "Memo", @user.id).order("created_at desc")
#用户留言 #用户留言
when 'user_feedback' when 'user_feedback'
@message_alls = UserFeedbackMessage.where("journals_for_message_type =? and user_id =?", "JournalsForMessage", @user).order("created_at desc") @message_alls = UserFeedbackMessage.where("journals_for_message_type =? and user_id =?", "JournalsForMessage", @user.id).order("created_at desc")
else else
render_404 render_404
return return

View File

@ -0,0 +1,5 @@
class AddIndexToMessageAlls < ActiveRecord::Migration
def change
add_index :message_alls, [:user_id, :message_id, :message_type, :created_at]
end
end

View File

@ -0,0 +1,5 @@
class AddIndexToCourseMessages < ActiveRecord::Migration
def change
add_index :course_messages, [:user_id, :course_id, :created_at]
end
end

View File

@ -0,0 +1,5 @@
class AddIndexToMemoMessages < ActiveRecord::Migration
def change
add_index :memo_messages, [:user_id, :forum_id, :created_at]
end
end

View File

@ -0,0 +1,5 @@
class AddIndexToUserFeedbackMessages < ActiveRecord::Migration
def change
add_index :user_feedback_messages, [:user_id, :created_at]
end
end

View File

@ -0,0 +1,5 @@
class AddIndexToForgeMessages < ActiveRecord::Migration
def change
add_index :forge_messages, [:user_id, :project_id, :created_at]
end
end

View File

@ -0,0 +1,5 @@
class AddForgetypeToForgeMessages < ActiveRecord::Migration
def change
add_index :forge_messages, [:forge_message_id, :forge_message_type]
end
end

View File

@ -0,0 +1,5 @@
class AddUsertypeToUserFeedbackMessages < ActiveRecord::Migration
def change
add_index :user_feedback_messages, [:journals_for_message_id]
end
end

View File

@ -0,0 +1,5 @@
class AddMemotypeToMemoMessages < ActiveRecord::Migration
def change
add_index :memo_messages, [:memo_id, :memo_type]
end
end

View File

@ -0,0 +1,5 @@
class AddFeedToUserFeedbackMessages < ActiveRecord::Migration
def change
add_index :course_messages, [:course_message_type]
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20160317090350) do ActiveRecord::Schema.define(:version => 20160321080825) do
create_table "activities", :force => true do |t| create_table "activities", :force => true do |t|
t.integer "act_id", :null => false t.integer "act_id", :null => false
@ -465,6 +465,10 @@ ActiveRecord::Schema.define(:version => 20160317090350) do
t.integer "status" t.integer "status"
end end
add_index "course_messages", ["course_message_id"], :name => "index_course_messages_on_course_message_id"
add_index "course_messages", ["course_message_type"], :name => "index_course_messages_on_course_message_type"
add_index "course_messages", ["user_id", "course_id", "created_at"], :name => "index_course_messages_on_user_id_and_course_id_and_created_at"
create_table "course_statuses", :force => true do |t| create_table "course_statuses", :force => true do |t|
t.integer "changesets_count" t.integer "changesets_count"
t.integer "watchers_count" t.integer "watchers_count"
@ -741,6 +745,9 @@ ActiveRecord::Schema.define(:version => 20160317090350) do
t.integer "status" t.integer "status"
end end
add_index "forge_messages", ["forge_message_id", "forge_message_type"], :name => "index_forge_messages_on_forge_message_id_and_forge_message_type"
add_index "forge_messages", ["user_id", "project_id", "created_at"], :name => "index_forge_messages_on_user_id_and_project_id_and_created_at"
create_table "forums", :force => true do |t| create_table "forums", :force => true do |t|
t.string "name", :null => false t.string "name", :null => false
t.text "description" t.text "description"
@ -1055,6 +1062,9 @@ ActiveRecord::Schema.define(:version => 20160317090350) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
add_index "memo_messages", ["memo_id", "memo_type"], :name => "index_memo_messages_on_memo_id_and_memo_type"
add_index "memo_messages", ["user_id", "forum_id", "created_at"], :name => "index_memo_messages_on_user_id_and_forum_id_and_created_at"
create_table "memos", :force => true do |t| create_table "memos", :force => true do |t|
t.integer "forum_id", :null => false t.integer "forum_id", :null => false
t.integer "parent_id" t.integer "parent_id"
@ -1078,6 +1088,8 @@ ActiveRecord::Schema.define(:version => 20160317090350) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
add_index "message_alls", ["message_id", "message_type", "created_at"], :name => "index_message_alls_on_message_id_and_message_type_and_created_at"
create_table "messages", :force => true do |t| create_table "messages", :force => true do |t|
t.integer "board_id", :null => false t.integer "board_id", :null => false
t.integer "parent_id" t.integer "parent_id"
@ -1830,6 +1842,9 @@ ActiveRecord::Schema.define(:version => 20160317090350) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
add_index "user_feedback_messages", ["journals_for_message_id"], :name => "index_user_feedback_messages_on_journals_for_message_id"
add_index "user_feedback_messages", ["user_id", "created_at"], :name => "index_user_feedback_messages_on_user_id_and_created_at"
create_table "user_grades", :force => true do |t| create_table "user_grades", :force => true do |t|
t.integer "user_id", :null => false t.integer "user_id", :null => false
t.integer "project_id", :null => false t.integer "project_id", :null => false