为数据库添加索引,加快查询速度

This commit is contained in:
xaoyaoo 2024-08-03 14:01:12 +08:00
parent 32a702acc3
commit d5fd863cfa
5 changed files with 61 additions and 3 deletions

View File

@ -268,7 +268,7 @@ def get_video(videoPath):
def get_audio(savePath): def get_audio(savePath):
my_wxid = get_conf(g.caf, g.at, "last") my_wxid = get_conf(g.caf, g.at, "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required") if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = get_conf(g.caf, my_wxid, "merge_path") db_config = get_conf(g.caf, my_wxid, "db_config")
savePath = os.path.join(g.work_path, my_wxid, "audio", savePath) # 这个是从url中获取的 savePath = os.path.join(g.work_path, my_wxid, "audio", savePath) # 这个是从url中获取的
if os.path.exists(savePath): if os.path.exists(savePath):
@ -282,8 +282,8 @@ def get_audio(savePath):
if not os.path.exists(os.path.dirname(savePath)): if not os.path.exists(os.path.dirname(savePath)):
os.makedirs(os.path.dirname(savePath)) os.makedirs(os.path.dirname(savePath))
parsing_media_msg = MediaHandler(merge_path) db = DBHandler(db_config)
wave_data = parsing_media_msg.get_audio(MsgSvrID, is_play=False, is_wave=True, save_path=savePath, rate=24000) wave_data = db.get_audio(MsgSvrID, is_play=False, is_wave=True, save_path=savePath, rate=24000)
if not wave_data: if not wave_data:
return ReJson(1001, body="wave_data is required") return ReJson(1001, body="wave_data is required")

View File

@ -35,6 +35,13 @@ class DBHandler(MicroHandler, MediaHandler, OpenIMContactHandler, PublicMsgHandl
self.OpenIMMedia_exist = self.OpenIMMedia_tables_exist() self.OpenIMMedia_exist = self.OpenIMMedia_tables_exist()
self.Favorite_exist = self.Favorite_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, # print(self.MSG_exist, self.Micro_exist, self.Media_exist, self.OpenIMContact_exist, self.PublicMsg_exist,
# self.OpenIMMedia_exist, self.Favorite_exist) # self.OpenIMMedia_exist, self.Favorite_exist)

View File

@ -29,6 +29,18 @@ class MsgHandler(DatabaseBase):
""" """
return self.check_tables_exist(self.MSG_required_tables) 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 @db_error
def get_msg_count(self, wxids: list = ""): def get_msg_count(self, wxids: list = ""):
""" """

View File

@ -24,6 +24,33 @@ class MicroHandler(DatabaseBase):
""" """
return self.check_tables_exist(self.Micro_required_tables) 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 @db_error
def get_labels(self, id_is_key=True): def get_labels(self, id_is_key=True):
""" """

View File

@ -39,6 +39,18 @@ class PublicMsgHandler(MsgHandler):
""" """
return self.check_tables_exist(self.PublicMSG_required_tables) 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 @db_error
def get_plc_msg_count(self, wxids: list = ""): def get_plc_msg_count(self, wxids: list = ""):
""" """