- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的表中有一个 JsonB 列,其中包含分层信息。
MyTable (id uuid, indexes jsonb, content bytea)
现在如果我创建一个 CTE 说
WITH RECURSIVE hierarchy(pid, id, content) AS (
--load first parents
SELECT t.indexes ->> 'parentId' as pId, t.id, t.content FROM MyTable c
JOIN MyTable t ON t.indexes ->> 'Id' = c.indexes ->> 'parentId'
WHERE c.Id = ANY('{..Some UUIDS}')
UNION
SELECT t.indexes ->> 'parentId' as pId, t.id, t.content
FROM hierarchy h, MyTable t
WHERE t.indexes ->> 'Id' = h.pid
) SELECT id, content from hierarchy
现在,从 30 万条记录的表中的 2 个节点构建父树的示例运行大约需要 10 秒。
现在如果我创建一个索引
CREATE INDEX MyIndex ON MyTable
USING btree
((indexes ->> 'Id')
这将时间减少到 4.5 秒。这产生了一个分析
-> Recursive Union (cost=23.81..4528423.71 rows=80794929 width=1219) (actual time=0.188..1802.636 rows=5 loops=1)
-> Nested Loop (cost=23.81..3150.15 rows=899 width=1219) (actual time=0.132..0.133 rows=1 loops=1)
Output: (t.indexes ->> 'parentId'::text), t.id, t.content
-> Index Scan using "MyTable_pkey" on "TEST"."MyTable" c (cost=0.42..8.44 rows=1 width=123) (actual time=0.053..0.053 rows=1 loops=1)
Output: c.id, c.content, c.indexes
Index Cond: (c.id = ANY ('{1c725f08-0324-41e9-b417-5ec885fb1cc9}'::uuid[]))
-> Bitmap Heap Scan on "TEST"."MyTable" t (cost=23.39..3130.48 rows=899 width=1219) (actual time=0.066..0.066 rows=1 loops=1)
Output: t.id, t.content, t.indexes
Recheck Cond: (((t.indexes ->> 'Id'::text) = (c.indexes ->> 'parentId'::text)))
Heap Blocks: exact=1
-> Bitmap Index Scan on "MyIndex" (cost=0.00..23.17 rows=899 width=0) (actual time=0.055..0.055 rows=1 loops=1)
Index Cond: ((t.indexes ->> 'Id'::text) = (c.indexes ->> 'parentId'::text))
//UNION PART
-> Merge Join (cost=770.60..290937.50 rows=8079403 width=1219) (actual time=360.467..360.476 rows=1 loops=5)
Output: (t_1.indexes ->> 'parentId'::text), t_1.id, t_1.content
Merge Cond: ((t_1.indexes ->> 'Id'::text) = h.pid)
-> Index Scan using "MyIndex" on "TEST"."MyTable" t_1 (cost=0.42..127680.55 rows=179742 width=1219) (actual time=0.019..288.168 rows=60478 loops=5)
Output: t_1.id, t_1.sourceid, t_1.content, t_1.indexes
-> Sort (cost=770.18..792.65 rows=8990 width=32) (actual time=0.010..0.011 rows=1 loops=5)
Output: h.pid
Sort Key: h.pid
Sort Method: quicksort Memory: 25kB
-> WorkTable Scan on hierarchy h (cost=0.00..179.80 rows=8990 width=32) (actual time=0.001..0.001 rows=1 loops=5)
Output: h.pid
现在我可以通过用 cte 中的函数替换索引 ->> 'parentId' 并在该函数上创建索引来获得巨大的速度改进。
CREATE FUNCTION "TEST"."MyFunction"(idarg uuid)
RETURNS text AS
$BODY$
SELECT t.indexes ->> 'Id' as result FROM "TEST"."MyTable" t
WHERE t.id = idarg
$BODY$
LANGUAGE sql IMMUTABLE;
有索引
CREATE INDEX MyFunctionIndex ON MyTable
USING btree
(MyFunction(id))
现在执行查询需要 0.01 秒通过分析
-> Recursive Union (cost=23.81..5333205.06 rows=80794929 width=1219) (actual time=0.163..0.291 rows=5 loops=1)
-> Nested Loop (cost=23.81..3372.65 rows=899 width=1219) (actual time=0.082..0.084 rows=1 loops=1)
Output: (t.indexes ->> 'parentId'::text), t.id, t.content, t.modified
-> Index Scan using "MyTable_pkey" on "TEST"."MyTable" c (cost=0.42..8.44 rows=1 width=123) (actual time=0.019..0.019 rows=1 loops=1)
Output: c.id, c.sourceid, c.viewid, c.content, c.indexes, c.statekey, c.modified
Index Cond: (c.id = ANY ('{1c725f08-0324-41e9-b417-5ec885fb1cc9}'::uuid[]))
-> Bitmap Heap Scan on "TEST"."MyTable" t (cost=23.39..3352.98 rows=899 width=1219) (actual time=0.037..0.037 rows=1 loops=1)
Output: t.id, t.content, t.indexes
Recheck Cond: (("TEST"."MyFunction"(t.id) = (c.indexes ->> 'parentId'::text)))
Heap Blocks: exact=1
-> Bitmap Index Scan on "MyFunctionIndex" (cost=0.00..23.17 rows=899 width=0) (actual time=0.025..0.025 rows=1 loops=1)
Index Cond: ("TEST"."MyFunction"(t.id) = (c.indexes ->> 'parentId'::text))
//UNION PART
-> Nested Loop (cost=0.42..371393.38 rows=8079403 width=1219) (actual time=0.012..0.013 rows=1 loops=5)
Output: (t_1.indexes ->> 'parentId'::text), t_1.id, t_1.content
-> WorkTable Scan on hierarchy h (cost=0.00..179.80 rows=8990 width=32) (actual time=0.000..0.000 rows=1 loops=5)
Output: h.pid, h.id, h.content
-> Index Scan using "MyFunctionIndex" on "TEST"."MyTable" t_1 (cost=0.42..30.06 rows=899 width=1219) (actual time=0.010..0.010 rows=1 loops=5)
Output: t_1.id, t_1.content, t_1.indexes
Index Cond: ("TEST"."MyFunction"(t_1.id) = h.pid)
那么为什么索引的运行速度不如函数索引呢?
那里似乎有一种多余的东西。我不想只使用函数索引的原因是它是 IMMUTABLE,因此索引不会在 INSERT/UPDATE/DELETE 后自动更新。
PS 我不是在寻找架构更改建议。
最佳答案
看起来 Gin 索引表现良好。如果我在 indexes 列上创建一个 Gin 索引,然后将 Join 更改为
ON t.indexes @> jsonb_build_object('Id', c.indexes -> 'parentId')
去哪里
WHERE t.indexes @> jsonb_build_object('Id', h.pid)
它不如纯函数索引快,但至少它会动态更新,并且执行计划没有那种不必要的排序
可以通过添加 gin 索引标志 jsonb_path_ops 进一步提高性能
关于postgresql - Postgres 分层 (jsonb) CTE 不必要地慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34658709/
在 Django 中如何处理分层 URL?有什么最佳做法吗?例如。如果我有一个像 /blog/category1/category2/myblogentry 这样的 URL(使用例如 django-m
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
有没有办法在 R 中创建这样的图表? 以下是图表中显示的数据的摘录: df % group_by(Animal) %>% unite(col=Type, Animal:Name, sep =
我一直在努力处理一些时间戳数据(直到现在才需要处理日期,并且它表明)。希望您能帮忙。 我正在处理来自网站的数据,该数据显示每个客户 (ID) 各自的访问以及这些访问的时间戳。它的分组是指一个客户可能有
我一直在努力处理一些时间戳数据(直到现在才需要处理日期,并且它表明)。希望您能帮忙。 我正在处理来自网站的数据,该数据显示每个客户 (ID) 各自的访问以及这些访问的时间戳。它的分组是指一个客户可能有
我正在尝试完成这段代码: ORDER BY IF(j.groups IS NULL OR j.groups = '', IF(j.title IS NULL, i.title), j.groups)
我有一个非常抽象的问题,因为我不确定如何提出它。我的其中一个 View 上有一个 UIImageView。我想让 ImageView 看起来“压入 super View ”。我不确定技术术语是什么,但
我希望 100% 宽的包含图像的 div 位于我的页面下方。在这些 div 之上,我想要一个 1210 像素宽的 div,我可以在其中放置我的内容。 例子: http://mudchallenger.
我目前正在做一个类似于 http://www.beoplay.com/Products/BeoplayA9#under-the-hood 的元素使用 Javascript、HTML5 和 CSS3。我
我想像上面那样创建图像缩略图..为此,我在下面创建了 XML activity_main.xml
我想知道是否可以定义一个分层 MapReduce 作业?。换句话说,我想要一个 map-reduce 作业,在 mapper 阶段将调用不同的 MapReduce 作业。可能吗?您对如何操作有什么建议
程序设计: A 类,实现较低级别的数据处理 类 B-E,为 A 提供更高级别的接口(interface)以执行各种功能 F 类,它是根据用户输入与 B-E 交互的 UI 对象 在任何给定时间只能有一个
CTE 对我来说有点新,所以我希望有人可以帮助我编写的以下内容将采用类别表并从中构建层次结构以进行显示。我知道这种事情一直被问到,但我认为我的排序情况使它有点独特。 我希望有一些使用 Hierarch
我有关于 的问题群 在聚类分析(层次聚类)中。例如,这是 的完全链式的树状图。虹膜数据集 . 我使用后 > table(cutree(hc, 3), iris$Species) 这是输出 : se
数据 我有以下(简化的)数据集,我们称之为 df从现在开始: species rank value 1
Delphi 2009 中的分层窗口和系统菜单存在问题。也就是说,我们的分层窗口(没有边框)没有系统菜单。当我说系统菜单时,我指的是单击应用程序的图标、右键单击其标题栏或(在 Windows 7 中,
我正在制作一个 pototype HMTL5 Canvas 动画,该动画将导出到 Quicktime。 我有一个动态生成的背景,上面有动态屏蔽的元素。 我可以获取要制作的背景,并将其作为逐帧动画(pn
好吧,我有一个打印棋盘的类和另一个打印国际象棋的类 如何使用 LayeredPane 将它们合并在一起,如上面的示例图片所示?我一整天都在尝试,但似乎没有任何效果。我正在使用 JFrame 打印图片。
这是我的场景。我有两个类(class) ClassA 和 ClassB。 B类继承A类。 我在它们两个上使用@Component注释来使它们成为Spring bean。 @Component publ
这不是一道问题题,而是一道使用工具——leiningen——的题。 在一个主项目下创建分层的 lein 项目是否有优势,如果有,优势是什么? 如果我使用 lein new bene-cmp 创建一个项
我是一名优秀的程序员,十分优秀!