Speeding up fn:id()
Thursday, 12 January 2017 · 2 messages
SZ
Sebastian Zimmer12 Jan 2017 17:40 #
Hi all,
I am surprised that
fn:id() does take quite a long time in contrast to (almost) equivalent XPaths.
An example:Quoted text · 1 line
> XQUERY collection('zk/')//id('zkI_108-5_R')
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="zkI_108-5_R"> ...
</div>
Query executed in 5956.43 ms.
Quoted text · 1 line
> XQUERY collection('zk/')//*[@xml:id='zkI_108-5_R']
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="zkI_108-5_R"> ...
</div>
Query executed in 9.43 ms.
Couldn't the first XQuery be optimized into the second?
If not, maybe an xml:id index would be a good feature. eXist-db does have one, particularly dedicated to speed up
fn:id() [1].
Best regards,
Sebastian Zimmer
[1] http://exist-db.org/exist/apps/doc/indexing.xml--
<----------
Sebastian Zimmer
sebastian.zimmer at uni-koeln.de
Cologne Center for eHumanities (CCeH)
- Universität zu Köln -
http://www.cceh.uni-koeln.de
------------------------------------>
CG
Christian Grün12 Jan 2017 18:57 #
Hi Sebastian,
Quoted text · Sebastian Zimmer · 1 line
>> collection('zk/')//id('zkI_108-5_R')
…yields the same results as…
collection('zk/')/id('zkI_108-5_R')…but the latter version should be much faster. Your first query is identical to:
collection('zk/')/descendant-or-self::node()/id('zkI_108-5_R')This means that the
fn:id function will be called for each node of the document (but as duplicates in a path expression will be removed, you will not get any duplicate nodes).
In BaseX, similar to eXist-db, the index is used to speed up evaluation, but eXist-db seems to better at optimizing the query in a way such that fn:id will only be evaluated once. Maybe I can introduce a similar optimization in BaseX, but for now, using a single slash should already help you out.
Cheers,
ChristianQuoted text · Sebastian Zimmer · 34 lines
> <div xmlns="http://www.tei-c.org/ns/1.0" xml:id="zkI_108-5_R"> > ... > </div> > Query executed in 5956.43 ms. > >> XQUERY collection('zk/')//*[@xml:id='zkI_108-5_R'] > > <div xmlns="http://www.tei-c.org/ns/1.0" xml:id="zkI_108-5_R"> > ... > </div> > Query executed in 9.43 ms. > > Couldn't the first XQuery be optimized into the second? > If not, maybe an xml:id index would be a good feature. eXist-db does have > one, particularly dedicated to speed up fn:id() [1]. > > Best regards, > Sebastian Zimmer > > [1] http://exist-db.org/exist/apps/doc/indexing.xml > > > -- > <---------- > Sebastian Zimmer > sebastian.zimmer at uni-koeln.de > > Cologne Center for eHumanities (CCeH) > - Universität zu Köln - > > http://www.cceh.uni-koeln.de > > ------------------------------------> >
Written for BaseX 8.5. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery