PostgreSQL Oracle 相容性 - connect by 2
標籤
PostgreSQL , Oracle , 樹形查詢 , 遞迴 , connect by , tablefunc , connectby
背景
Oracle connect by語法經常用於有樹形關係的記錄查詢,PostgreSQL使用CTE遞迴語法,可以實現同樣的功能。
《PostgreSQL Oracle 相容性之 - connect by》
本文通過一個更加簡單的方法,同樣可以實現一樣的功能,用到tablefunc外掛中的connectby函式。
介面如下
https://www.postgresql.org/docs/devel/static/tablefunc.html
connectby(text relname, text keyid_fld, text parent_keyid_fld
[, text orderby_fld ], text start_with, int max_depth
[, text branch_delim ])
setof record
Produces a representation of a hierarchical tree structure
Parameter | Description |
---|---|
relname | Name of the source relation |
keyid_fld | Name of the key field |
parent_keyid_fld | Name of the parent-key field |
orderby_fld | Name of the field to order siblings by (optional) |
start_with | Key value of the row to start at |
max_depth | Maximum depth to descend to, or zero for unlimited depth |
branch_delim | String to separate keys with in branch output (optional) |
例子
create extension tablefunc;
CREATE TABLE connectby_tree(keyid text, parent_keyid text, pos int);
INSERT INTO connectby_tree VALUES('row1',NULL, 0);
INSERT INTO connectby_tree VALUES('row2','row1', 0);
INSERT INTO connectby_tree VALUES('row3','row1', 0);
INSERT INTO connectby_tree VALUES('row4','row2', 1);
INSERT INTO connectby_tree VALUES('row5','row2', 0);
INSERT INTO connectby_tree VALUES('row6','row4', 0);
INSERT INTO connectby_tree VALUES('row7','row3', 0);
INSERT INTO connectby_tree VALUES('row8','row6', 0);
INSERT INTO connectby_tree VALUES('row9','row5', 0);
-- with branch, without orderby_fld (order of results is not guaranteed)
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~')
AS t(keyid text, parent_keyid text, level int, branch text);
keyid | parent_keyid | level | branch
-------+--------------+-------+---------------------
row2 | | 0 | row2
row4 | row2 | 1 | row2~row4
row6 | row4 | 2 | row2~row4~row6
row8 | row6 | 3 | row2~row4~row6~row8
row5 | row2 | 1 | row2~row5
row9 | row5 | 2 | row2~row5~row9
(6 rows)
-- without branch, without orderby_fld (order of results is not guaranteed)
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
AS t(keyid text, parent_keyid text, level int);
keyid | parent_keyid | level
-------+--------------+-------
row2 | | 0
row4 | row2 | 1
row6 | row4 | 2
row8 | row6 | 3
row5 | row2 | 1
row9 | row5 | 2
(6 rows)
-- with branch, with orderby_fld (notice that row5 comes before row4)
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~')
AS t(keyid text, parent_keyid text, level int, branch text, pos int);
keyid | parent_keyid | level | branch | pos
-------+--------------+-------+---------------------+-----
row2 | | 0 | row2 | 1
row5 | row2 | 1 | row2~row5 | 2
row9 | row5 | 2 | row2~row5~row9 | 3
row4 | row2 | 1 | row2~row4 | 4
row6 | row4 | 2 | row2~row4~row6 | 5
row8 | row6 | 3 | row2~row4~row6~row8 | 6
(6 rows)
-- without branch, with orderby_fld (notice that row5 comes before row4)
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0)
AS t(keyid text, parent_keyid text, level int, pos int);
keyid | parent_keyid | level | pos
-------+--------------+-------+-----
row2 | | 0 | 1
row5 | row2 | 1 | 2
row9 | row5 | 2 | 3
row4 | row2 | 1 | 4
row6 | row4 | 2 | 5
row8 | row6 | 3 | 6
(6 rows)
參考
《PostgreSQL 家譜、族譜類應用實踐 - 圖式關係儲存與搜尋》
《PostgreSQL 遞迴妙用案例 - 分組資料去重與打散》
《PostgreSQL Oracle 相容性之 - INDEX SKIP SCAN (遞迴查詢變態優化) 非驅動列索引掃描優化》
《PostgreSQL 圖式搜尋(graph search)實踐 - 百億級圖譜,毫秒響應》
《[未完待續] AgensGraph 圖資料庫介紹以及 on ECS部署》
《PostgreSQL 實踐 - 內容社群(如論壇)圖式搜尋應用》
《小微貸款、天使投資(風控助手)業務資料庫設計(圖式搜尋\圖譜分析) - 阿里雲RDS PostgreSQL, HybridDB for PostgreSQL最佳實踐》
《PostgrSQL 遞迴SQL的幾個應用 - 極客與正常人的思維》
《PostgreSQL 遞迴查詢CASE - 樹型路徑分組輸出》
《金融風控、公安刑偵、社會關係、人脈分析等需求分析與資料庫實現 - PostgreSQL圖資料庫場景應用》
《用PostgreSQL找回618秒逝去的青春 - 遞迴收斂優化》
《distinct xx和count(distinct xx)的變態遞迴優化方法 - 索引收斂(skip scan)掃描》
《時序資料合併場景加速分析和實現 - 複合索引,視窗分組查詢加速,變態遞迴加速》
《facebook linkbench 測試PostgreSQL社交關係圖譜場景效能》
《PostgreSQL 使用遞迴SQL 找出資料庫物件之間的依賴關係》
《PostgreSQL Oracle 相容性之 - WITH 遞迴 ( connect by )》
《遞迴優化CASE - group by & distinct tuning case : use WITH RECURSIVE and min() function》
《PostgreSQL 樹狀資料儲存與查詢(非遞迴) - Use ltree extension deal tree-like data type》
相關文章
- Oracle start with connect by PostgreSQL recursive cteOracleSQL
- Laravel connect oracleLaravelOracle
- Oracle vs PostgreSQL DBA(22)- Oracle VPD#2OracleSQL
- Linux Use ODBC Connect OracleLinuxOracle
- Oracle的SYS_CONNECT_BY_PATH函式Oracle函式
- Oracle vs PostgreSQL DBA(21)- Oracle VPDOracleSQL
- [轉載] Oracle:start with...connect by子句的用法Oracle
- Ora2Pg:將Oracle遷移到PostgreSQL的免費工具OracleSQL
- Oracle vs PostgreSQL,研發注意事項(10)- PostgreSQL資料型別轉換規則#2OracleSQL資料型別
- PostgreSQLOracle相容性-connectby2SQLOracle
- Oracle vs PostgreSQL Develop(17) - ARRAYOracleSQLdev
- Oracle vs PostgreSQL Develop(15) - DISTINCT ONOracleSQLdev
- Oracle vs PostgreSQL Develop(18) - BooleanOracleSQLdevBoolean
- PostgreSQL/Oracle 時間改成秒SQLOracle
- Oracle vs PostgreSQL,研發注意事項(2)-DDL語句與事務OracleSQL
- Python Uses ibm_db connect to DB2PythonIBMDB2
- Oracle案例09——ORA-12154: TNS:could not resolve the connect identifier specifiedOracleIDE
- Oracle vs PostgreSQL Develop(16) - Prepared StatementOracleSQLdev
- Oracle vs PostgreSQL Develop(20) - Materialized ViewOracleSQLdevZedView
- Oracle vs PostgreSQL Develop(19) - PIPE ROWOracleSQLdev
- PostgreSQL與Oracle的sql差異SQLOracle
- Oracle vs PostgreSQL Develop(28) - function overloadOracleSQLdevFunction
- Oracle vs PostgreSQL Develop(26) - ASCII 0OracleSQLdevASCII
- 多表關聯更新(mysql,oracle,postgreSQL)MySqlOracle
- Oracle到PostgreSQL等價改寫OracleSQL
- Oracle vs PostgreSQL Develop(29) - COMMIT in PLSQLOracleSQLdevMIT
- Jersey2.x分支JavaSE相容性Java
- Oracle與PostgreSQL比較:PostgreSQL至少在安裝和大小方面擊敗了Oracle - foersterOracleSQL
- Oracle vs PostgreSQL Develop(24) - subquery with agg functionOracleSQLdevFunction
- LightDB/PostgreSQL 相容Oracle儲存過程SQLOracle儲存過程
- Oracle vs PostgreSQL Develop(31) - Index Only ScanOracleSQLdevIndex
- 使用GoldenGate 遷移Oracle到PostgreSQL/LightDBGoOracleSQL
- PostgreSQL DBA(143) - pgAdmin(Monitoring PostgreSQL VACUUM processes#2)SQL
- Oracle 到 PostgreSQL參考分割槽實現OracleSQL
- Oracle vs PostgreSQL DBA(13)- 拆分(split)分割槽OracleSQL
- oracle Mysql PostgreSQL 資料庫的對比OracleMySql資料庫
- Oracle & MySQL & PostgreSQL資料庫恢復支援OracleMySql資料庫
- Oracle/MySQL透過odbc訪問PostgreSQL for LightDBOracleMySql