下面的 sql 查询非常慢。。。
select count(distinct "public"."tb_groupchat"."chat_id"), count("public"."tb_groupchat"."id"), count(distinct "public"."tb_groupchat"."sender_id") from "public"."tb_groupchat" where ("public"."tb_groupchat"."timestamp" >= $1 and "public"."tb_groupchat"."timestamp" < $2)
表结构:
"id" serial8 PRIMARY KEY NOT NULL,
"chat_id" int8 NOT NULL,
"sender_id" int8 NOT NULL,
"timestamp" int8 NOT NULL
explain 的结果
[
{
"Plan": {
"Node Type": "Aggregate",
"Strategy": "Plain",
"Partial Mode": "Simple",
"Parallel Aware": false,
"Async Capable": false,
"Startup Cost": 181904.15,
"Total Cost": 181904.16,
"Plan Rows": 1,
"Plan Width": 24,
"Output": ["count(DISTINCT chat_id)", "count(id)", "count(DISTINCT sender_id)"],
"Plans": [
{
"Node Type": "Index Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Scan Direction": "Forward",
"Index Name": "idx_timestamp",
"Relation Name": "tb_groupchat",
"Schema": "public",
"Alias": "tb_groupchat",
"Startup Cost": 0.43,
"Total Cost": 172205.39,
"Plan Rows": 1293168,
"Plan Width": 24,
"Output": ["id", "chat_id", "sender_id", "content", "\"timestamp\""],
"Index Cond": "((tb_groupchat.\"timestamp\" >= '1684944000000'::bigint) AND (tb_groupchat.\"timestamp\" < '1685030400000'::bigint))"
}
]
},
"Query Identifier": 6892608323288585066,
"JIT": {
"Functions": 5,
"Options": {
"Inlining": false,
"Optimization": false,
"Expressions": true,
"Deforming": true
}
}
}
]
不知道各位同学有没有啥高招。。。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.