CLP和SQL之间有什么关系?

发布于 2024-08-23 14:49:24 字数 151 浏览 3 评论 0原文

在阅读约束逻辑编程时,我不禁注意到与SQL 编程。 SQL 是“约束逻辑编程”的实际例子吗?

In reading up on constraint-logic programming, I can't help but notice an apparent relationship with SQL programming. Is SQL an example of "constraint logic programming" in action?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮筵 2024-08-30 14:49:25

从高层次上看,它们非常相似。两者都是声明性的或基于集合的,而不是迭代的(意味着您要求您想要的东西 - 您不会逐项循环和处理单个项目)。

CLP 可以用 SQL 建模,但它是寻找更好的问题解决方案的情况。 SQL 适合在已明确定义的给定数据集中查找答案。 CLP 适合在松散甚至有时甚至没有完全定义的领域中寻找答案。

举个例子。如果我想使用 SQL 返回 1 到 1000 万之间的所有偶数,我需要一个包含所有列出的数字(现有数据集)的表。使用 CLP,我只需要一个界限 (10M),但不必显式创建所有记录。

内部 CLP 引擎(CSP 问题)还可以推断约束以使其更快。 SQL 你必须弄清楚这些规则并明确地说明它们。例如,如果您的 SQL 是 A=B 且 B=C,则 CLP 引擎可能会计算出 A=C 并使用它来运行 SQL 无法运行的更快速度。它们也可以类似地推断域以优化运行时间(如果我知道偶数永远不会返回,CLP 会将它们排除在考虑之外 - SQL 仍会考虑它们,但不会作为“解决方案”返回)。

希望这会有所帮助 - 如果您需要的话,我可以获取更多技术信息。要记住的关键点是两者相似,但您使用哪一个取决于问题模型。如果您可以说“这些是我的变量,这些是如何相互关联的,请给我任何有效的解决方案”,那么 CLP 是一个很好的候选者。如果您可以说“有我的变量,这些是具体细节,请给我返回适合的现有数据的子集”,那么 SQL 是更好的选择。

They are very similar from a high level. Both are declarative or set based rather than iterative (meaning you ask for what you want - you don't loop through and process individual items one by one).

CLP can be modeled in SQL but it is a case of finding a better solution to a problem. SQL is good for finding answers in a given data set that is already explicitly defined. CLP is good for finding answers in domains that are loosely or sometimes even not even completely defined.

As an example. If I wanted to to return all the even numbers between 1 and 10 million using SQL I would need a table with all the numbers listed to select from (an existing data set). Using CLP I would just need a bound (10M) but wouldn't have to explicit create all the records.

Internally CLP engines (CSP problems) can also infer constraints to make them faster. SQL you would have to figure out those rules and explicitly state them. For example if your SQL is where A=B and B=C, a CLP engine might figure out that A=C and use that to run faster where SQL would not. They can similarly infer domains as well to optimize run time (if I know that the even numbers are never returned a CLP will throw them out from consideration - SQL will still consider them but not return as a "solution").

Hopefully that helps - I can get more technical if you need it. The key point to remember is the two are similar but which you use depends on the problem model. If you can say "these are my variables, and these are how the interrelate, give me any valid solution" then CLP is a good candidate. If you can say "there are my variables, and these are the specifics, give me back a subset of my existing data that fits" then SQL is a better candidate.

池木 2024-08-30 14:49:25

我没有完整的答案,但您可能会发现查看 Datalog 和 DLV 很有趣。它们可能是逻辑编程(但不是 CLP)和 SQL 之间的“缺失的环节”,可以帮助您更清楚地理解事物。

因为这更多关于 LP 而不是 CLP 我可能错过了一些东西 - 如果是这样,抱歉。

i don't have a full answer, but you might find it interesting to look at Datalog and DLV. They are perhaps the "missing link" between Logic Programming (but not CLP) and SQL that will help you understand things more clearly.

Since this is more about LP than CLP I may have missed something - if so, sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文