SQL:输出顺序
我正在检查 postgresql 的文档以了解递归查询,其中有一个示例。
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t
上面的语句和100条SELECT语句一样吗?来自文档:
递归查询通常用于处理分层或树结构数据。
如果我想根据某些条件对分层结构进行排序,建议使用递归查询。例如。 SQL 查询:从表中获取有序行 - II 以及接受的答案。是否应该从数据库检索数据,然后在内存中排序。或者RECURSIVE查询会更加高效!!
I was checking the docs of postgresql for Recursive queries where I got an example.
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t
Is the above statement same as 100 SELECT statements. From the docs:
Recursive queries are typically used to deal with hierarchical or tree-structured data.
If I want to sort the hierarchical structure based on some criteria will it be advisable to recursive query. eg. SQL Query: Fetch ordered rows from a table - II and the accepted answer. Should the data be retrieved from the DB and then sorted in memory. Or RECURSIVE query will be more effcient !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案取决于您的架构设计、硬件/操作系统、配置和加载的数据量。使用解释和解释分析两种方式运行它,并从几个典型查询中选择最快的。
即使我有足够的信息来猜测您的模式和示例数据,任何对我有利的答案可能对您不利。
The answer depends on your schema design, hardware/OS, configuration, and volume of data loaded. Run it both ways with explain and explain analyze and pick the fastest over several typical queries.
Even if I had enough information to guess your schema and exemplar data, any answer good for me may not the good for yo.