From 6f24b08f02faf8bdb924b576208d0d0a53a51236 Mon Sep 17 00:00:00 2001 From: zhuilang <490240398@qq.com> Date: Fri, 2 Feb 2024 15:58:57 +0800 Subject: [PATCH] =?UTF-8?q?mongo=20=E6=94=AF=E6=8C=81=20hint=20(#1865)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mongo 支持 hint mongodb查询有时不会命中最优索引,所以支持下 hint 使用 --- lualib/skynet/db/mongo.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 325a68c9..30f441b9 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -511,9 +511,10 @@ function mongo_collection:find(query, projection) __data = nil, __cursor = nil, __document = {}, + __sort = empty_bson, __skip = 0, __limit = 0, - __sort = empty_bson, + __hint = nil } , cursor_meta) end @@ -548,6 +549,11 @@ function mongo_cursor:limit(amount) return self end +function mongo_cursor:hint(indexName) + self.__hint = indexName + return self +end + function mongo_cursor:count(with_limit_and_skip) local ret if with_limit_and_skip then @@ -690,6 +696,13 @@ function mongo_collection:aggregate(pipeline, options) } , aggregate_cursor_meta) end +local function add_hint(self) + local h = self.__hint + if h then + return "hint", h + end +end + function mongo_cursor:hasNext() if self.__ptr == nil then if self.__document == nil then @@ -701,7 +714,7 @@ function mongo_cursor:hasNext() if self.__data == nil then local name = self.__collection.name response = database:runCommand("find", name, "filter", self.__query, "sort", self.__sort, - "skip", self.__skip, "limit", self.__limit, "projection", self.__projection) + "skip", self.__skip, "limit", self.__limit, "projection", self.__projection, add_hint(self)) else if self.__cursor and self.__cursor > 0 then local name = self.__collection.name