为数据库添加索引,加快查询速度
This commit is contained in:
1 parent
32a702acc3
commit
d5fd863cfa
5 files changed
+61
-3
No files matched your search
@@ -35,6 +35,13 @@ class DBHandler(MicroHandler, MediaHandler, OpenIMContactHandler, PublicMsgHandl
|
||||
self.OpenIMMedia_exist = self.OpenIMMedia_tables_exist()
|
||||
self.Favorite_exist = self.Favorite_tables_exist()
|
||||
|
||||
if self.MSG_exist: # 添加索引 StrTalker
|
||||
self.Msg_add_index()
|
||||
if self.PublicMsg_exist: # 添加索引 StrTalker
|
||||
self.PublicMsg_add_index()
|
||||
if self.Micro_exist: # 添加索引 StrTalker
|
||||
self.Micro_add_index()
|
||||
|
||||
# print(self.MSG_exist, self.Micro_exist, self.Media_exist, self.OpenIMContact_exist, self.PublicMsg_exist,
|
||||
# self.OpenIMMedia_exist, self.Favorite_exist)
|
||||
|
||||
|
||||
@@ -29,6 +29,18 @@ class MsgHandler(DatabaseBase):
|
||||
"""
|
||||
return self.check_tables_exist(self.MSG_required_tables)
|
||||
|
||||
def Msg_add_index(self):
|
||||
"""
|
||||
添加索引,加快查询速度
|
||||
"""
|
||||
# 检查是否存在索引
|
||||
sql = "CREATE INDEX IF NOT EXISTS idx_MSG_StrTalker ON MSG(StrTalker);"
|
||||
self.execute(sql)
|
||||
sql = "CREATE INDEX IF NOT EXISTS idx_MSG_CreateTime ON MSG(CreateTime);"
|
||||
self.execute(sql)
|
||||
sql = "CREATE INDEX IF NOT EXISTS idx_MSG_StrTalker_CreateTime ON MSG(StrTalker, CreateTime);"
|
||||
self.execute(sql)
|
||||
|
||||
@db_error
|
||||
def get_msg_count(self, wxids: list = ""):
|
||||
"""
|
||||
|
||||
@@ -24,6 +24,33 @@ class MicroHandler(DatabaseBase):
|
||||
"""
|
||||
return self.check_tables_exist(self.Micro_required_tables)
|
||||
|
||||
def Micro_add_index(self):
|
||||
"""
|
||||
添加索引, 加快查询速度
|
||||
"""
|
||||
# 为 Session 表添加索引
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_Session_strUsrName_nTime ON Session(strUsrName, nTime);")
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_Session_nOrder ON Session(nOrder);")
|
||||
|
||||
# 为 Contact 表添加索引
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_Contact_UserName ON Contact(UserName);")
|
||||
|
||||
# 为 ContactHeadImgUrl 表添加索引
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_ContactHeadImgUrl_usrName ON ContactHeadImgUrl(usrName);")
|
||||
|
||||
# 为 ChatInfo 表添加索引
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_ChatInfo_Username_LastReadedCreateTime "
|
||||
"ON ChatInfo(Username, LastReadedCreateTime);")
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_ChatInfo_LastReadedCreateTime ON ChatInfo(LastReadedCreateTime);")
|
||||
|
||||
# 为 Contact 表添加复合索引
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_Contact_search "
|
||||
"ON Contact(UserName, NickName, Remark, Alias, QuanPin, PYInitial, RemarkQuanPin, RemarkPYInitial);")
|
||||
|
||||
# 为 ChatRoom 和 ChatRoomInfo 表添加索引
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_ChatRoom_ChatRoomName ON ChatRoom(ChatRoomName);")
|
||||
self.execute("CREATE INDEX IF NOT EXISTS idx_ChatRoomInfo_ChatRoomName ON ChatRoomInfo(ChatRoomName);")
|
||||
|
||||
@db_error
|
||||
def get_labels(self, id_is_key=True):
|
||||
"""
|
||||
|
||||
@@ -39,6 +39,18 @@ class PublicMsgHandler(MsgHandler):
|
||||
"""
|
||||
return self.check_tables_exist(self.PublicMSG_required_tables)
|
||||
|
||||
def PublicMsg_add_index(self):
|
||||
"""
|
||||
添加索引,加快查询速度
|
||||
"""
|
||||
# 检查是否存在索引
|
||||
sql = "CREATE INDEX IF NOT EXISTS idx_PublicMsg_StrTalker ON MSG(StrTalker);"
|
||||
self.execute(sql)
|
||||
sql = "CREATE INDEX IF NOT EXISTS idx_PublicMsg_CreateTime ON MSG(CreateTime);"
|
||||
self.execute(sql)
|
||||
sql = "CREATE INDEX IF NOT EXISTS idx_PublicMsg_StrTalker_CreateTime ON MSG(StrTalker, CreateTime);"
|
||||
self.execute(sql)
|
||||
|
||||
@db_error
|
||||
def get_plc_msg_count(self, wxids: list = ""):
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user