How could I reuse sub queries?
Tuesday, 8 August 2017 · 3 messages
D
donaldjohn8 Aug 2017 03:24 #
Hi, everyone:
Here is my query that finds the distinct value and counts them, you can see that I used the same sub query twice, first for finding the distinct values, second for count the distinct values, is there a way that I can cache the sub query result and reuse it somewhere else? I think it will run faster in that way.
import module namespace fx = "http://www.functx.com" at "functx-1.0-doc-2007-01.xq";
declare namespace business="http://www.sipo.gov.cn/XMLSchema/business";
declare namespace base="http://www.sipo.gov.cn/XMLSchema/base";
(:Iterate through the distributed values:)
for $dis_val in
(
(
(:open multi database, then query the doc status field:)
for $result in doc("50PatentDividedCreatingClause.xml")/results/result
return db:open($result/dbName)/business:PatentDocumentAndRelated/@status
)
=>distinct-values()
)
return
"value : " || $dis_val || ", count: "||
count(
for $status in
(
for $result in doc("50PatentDividedCreatingClause.xml")/results/result
return db:open($result/dbName)/business:PatentDocumentAndRelated/@status
)
where $status = $dis_val
return $status||"")D
It looks much cleaner! thanks a lot!
------------------ 原始邮件 ------------------
发件人: "Liam R. E. Quin"<liam at w3.org>;
发送时间: 2017年8月8日(星期二) 上午9:58
收件人: "donaldjohn"<donaldjohn at qq.com>; "basex-talk"<basex-talk at mailman.uni-konstanz.de>; 主题: Re: [basex-talk] How could I reuse sub queries?
Quoted text · donaldjohn · 3 lines
On Tue, 2017-08-08 at 09:24 +0800, donaldjohn wrote: > is there a way that I can cache the sub query result and > reuse it somewhere else? I think it will run faster in that way.
You may find it runs at the same speed - BaseX may have noticed hte common query.
But you can try using,
let $statuslist := for $result in
doc("50PatentDividedCreatingClause.xml")/results/result
return
db:open($result/dbName)/business:PatentDocumentAndRelated/@status
return (
. . . do stuff with $statuslist . . . do more stuff with $statuslist
)
Quoted text · Liam R. E. Quin · 1 line
>
Liam
--
Liam Quin, W3C, http://www.w3.org/People/Quin/
Staff contact for Verifiable Claims WG, XQuery WG
Web slave for http://www.fromoldbooks.org/
LQ
Quoted text · donaldjohn · 3 lines
On Tue, 2017-08-08 at 09:24 +0800, donaldjohn wrote: > is there a way that I can cache the sub query result and > reuse it somewhere else? I think it will run faster in that way.
You may find it runs at the same speed - BaseX may have noticed hte common query.
But you can try using,
let $statuslist := for $result in
doc("50PatentDividedCreatingClause.xml")/results/result
return
db:open($result/dbName)/business:PatentDocumentAndRelated/@status
return (
. . . do stuff with $statuslist . . . do more stuff with $statuslist
)
Quoted text · donaldjohn · 1 line
>
Liam
--
Liam Quin, W3C, http://www.w3.org/People/Quin/
Staff contact for Verifiable Claims WG, XQuery WG
Web slave for http://www.fromoldbooks.org/
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery