网站建设及政务公开工作,icp备案 网站,做网站交付标准,政务建设网站得必要性postgresql 的递归查询功能很强大#xff0c;可以实现传统 sql 无法实现的事情。那递归查询的执行逻辑是什么呢#xff1f;在递归查询中#xff0c;我们一般会用到 union 或者 union all#xff0c;他们两者之间的区别是什么呢#xff1f;
递归查询的执行逻辑
递归查询的…postgresql 的递归查询功能很强大可以实现传统 sql 无法实现的事情。那递归查询的执行逻辑是什么呢在递归查询中我们一般会用到 union 或者 union all他们两者之间的区别是什么呢
递归查询的执行逻辑
递归查询的基本语法如下
WITH RECURSIVE ctename AS (SELECT /* non-recursive branch, cannot reference ctename */UNION [ALL]SELECT /* recursive branch referencing ctename */
)
SELECT ...
FROM ctename ...
其本身也是一个CTE可以将复杂的查询逻辑进行分离让整个查询的逻辑更加清晰。对于递归查询而言分为两部分
非递归部分。即例子中的 UNION [ALL] 的上半部分递归部分。即例子中的 UNION [ALL] 的下半部分
递归查询的逻辑如下
计算非递归部分其结果将作为递归查询的数据集也是初始数据集在第一步计算出来的数据上执行递归部分新查询出的数据将作为下次递归执行的数据集。也就是说每次递归使用的数据集都是上次递归的结果直到没有新的数据产生后递归结束将每一次递归的数据进行聚合就拿到了最终的数据集
UNION 和 UNION ALL
UNION: 会将本次递归查询到的数据进行内部去重也会和之前递归查询出的数据进行去重UNION ALL: 不会对数据进行去重
举个例子
// 创建表
create table document_directories
(id bigserial not null,name text not null,created_at timestamp with time zone default CURRENT_TIMESTAMP not null,updated_at timestamp with time zone default CURRENT_TIMESTAMP not null,parent_id bigint default 0 not null
);// 插入示例数据有两条数据是一样的
INSERT INTO public.document_directories (id, name, created_at, updated_at, parent_id) VALUES (1, 中国, 2020-03-28 15:55:27.137439, 2020-03-28 15:55:27.137439, 0);
INSERT INTO public.document_directories (id, name, created_at, updated_at, parent_id) VALUES (2, 上海, 2020-03-28 15:55:40.894773, 2020-03-28 15:55:40.894773, 1);
INSERT INTO public.document_directories (id, name, created_at, updated_at, parent_id) VALUES (3, 北京, 2020-03-28 15:55:53.631493, 2020-03-28 15:55:53.631493, 1);
INSERT INTO public.document_directories (id, name, created_at, updated_at, parent_id) VALUES (4, 南京, 2020-03-28 15:56:05.496985, 2020-03-28 15:56:05.496985, 1);
INSERT INTO public.document_directories (id, name, created_at, updated_at, parent_id) VALUES (5, 浦东新区, 2020-03-28 15:56:24.824672, 2020-03-28 15:56:24.824672, 2);
INSERT INTO public.document_directories (id, name, created_at, updated_at, parent_id) VALUES (6, 徐汇区, 2020-03-28 15:56:39.664924, 2020-03-28 15:56:39.664924, 2);
INSERT INTO public.document_directories (id, name, created_at, updated_at, parent_id) VALUES (6, 徐汇区, 2020-03-28 15:56:39.664924, 2020-03-28 15:56:39.664924, 2);
使用 UNION ALL 进行数据查询
with recursive sub_shanghai as (select id, name, parent_idfrom document_directorieswhere id2union allselect dd.id, dd.name, dd.parent_idfrom document_directories ddjoin sub_shanghai on dd.parent_idsub_shanghai.id)select * from sub_shanghai;
结果如下 使用 UNION 进行查询
with recursive sub_shanghai as (select id, name, parent_idfrom document_directorieswhere id2unionselect dd.id, dd.name, dd.parent_idfrom document_directories ddjoin sub_shanghai on dd.parent_idsub_shanghai.id)select * from sub_shanghai;
得到结果如下 我们修改下原始数据再看下去重逻辑的区别
update document_directories set parent_id 2 where id2;
当我们使用 UNION 进行递归查询时结果并没有发生变化。但是当我们使用 UNION ALL 进行查询时会一直执行。这是因为 UNION ALL 不会将数据进行去重而每次递归查询的时候总归能查询到 {id: 5, name:上海, parent_id: 2} 这条数据所以递归就没有终止条件。
从而也验证了UNION 不但会将本次递归查询的数据进行内部去重也会和之前的递归结果进行去重。
文章转载自: http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.yhwxn.cn.gov.cn.yhwxn.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.httzf.cn.gov.cn.httzf.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.nfyc.cn.gov.cn.nfyc.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.ztqj.cn.gov.cn.ztqj.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.ggnjq.cn.gov.cn.ggnjq.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.hclplus.com.gov.cn.hclplus.com http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.mrlkr.cn.gov.cn.mrlkr.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.rhph.cn.gov.cn.rhph.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.lzttq.cn.gov.cn.lzttq.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.qklff.cn.gov.cn.qklff.cn