我们正在研究使用 Oracle 分层查询来建模可能非常大的树结构(可能无限宽,深度为 30+)。我的理解是,分层查询提供了一种编写递归连接 SQL 的方法,但与手动编写等效查询相比,它们并没有提供任何真正的性能增强……是这样吗?在性能方面,人们在使用 oracle 分层查询方面有过什么样的体验?
4 回答
简短的回答是,如果没有分层扩展(连接方式),您将无法编写递归查询。您可以以编程方式发出许多递归链接的查询。
所有数据库的经验法则,尤其是 oracle,是如果您可以在单个查询中发出结果,它几乎总是比以编程方式执行要快。
我的经验是使用小得多的集合,所以我不能说层次查询对大集合的性能有多好。
在进行这些树检索时,您通常有这些选项
- 查询所有内容并在客户端组装树。
- 对树的每一级执行一个查询,建立在您从先前查询结果中知道您需要的内容的基础上
- 使用 Oracle 提供的内置内容(START WITH,CONNECT BY PRIOR)。
在数据库中完成这一切将减少不必要的往返或提取过多数据的浪费查询。
Try partitioning the data within you hierarchical table and then limiting the partition included in the query.
CREATE TABLE
loopy
(key NUMBER, key_hier number, info VARCHAR2, part NUMBER)
PARTITION BY
RANGE (part)
(
PARTITION low VALUES LESS THAN (1000),
PARTITION mid VALUES LESS THAN (10000),
PARTITION high VALUES LESS THAN (MAXVALUE)
);
SELECT
info
FROM
loopy PARTITION(mid)
CONNECT BY
key = key_hier
START WITH
key = <some value>;
The interesting problem now becomes your partitioning strategy. Oracle provides several options.
I've seen that using connect by can be slow but compared to what? There isn't really another option except building a result set using recursive PL/SQL calls (slower) or doing it on your client side.
You could try separating your data into a mapping (hierarchy definition) and lookup tables (the display data) and then joining them back together. I guess I wouldn't expect much of a gain assuming you are getting the hierarchy data from indexed fields but its worth a try.
Have you tried it using the connect by yet? I'm a big fan of trying different variations.