logseq / docs

Logseq documentation
https://docs.logseq.com
MIT License
137 stars 88 forks source link

Add documentation for "Clause order of advanced query" #60

Open cnrpman opened 1 year ago

cnrpman commented 1 year ago

A main blame of query issue is actually about clause order of queries. We should setup a FAQ on the forum https://discuss.logseq.com/c/faq/6 to help saving the communication cost of the process. Then make it to the document.

References: https://docs.logseq.com/#/page/advanced%20queries https://docs.datomic.com/on-prem/query/query-executing.html#clause-order https://github.com/logseq/logseq/blob/d0af76d513407f660946d9c957cf2786f430e2a2/deps/db/src/logseq/db/rules.cljc#L26-L62

The clauses are executed sequentially. If you mention a binding in a clause that is not mentioned at all in the previous section, it will do a full-text search, which will be very slow So you should try to use up the input in the first order, and keep the conditions small To narrow the scope step by step

For example, if the input is ?cn:

[?b :block/page ?p] ;; This clause has no constraints, so don't put it here [?cd :block/name ?cn] [?b :block/path-refs ?cd] (not [?b :block/page ?cd])

should be changed to [?cd :block/name ?cn] [?b :block/path-refs ?cd] (not [?b :block/page ?cd]) [?b :block/page ?p] ;; wait for the previous clause to narrow down, then execute the unqualified clause