Performance problem with function declaration
Monday, 21 November 2011 · 4 messages · last reply 22 Nov 2011
AB
Andy Bunce21 Nov 2011 23:39 #
Hi
Using Basex 7.0.2 and the factbook.xml database, I observe the following:
declare variable $data:=db:open("factbook");Quoted text · 9 lines
> > declare function local:myid1($id){ > $data//*[@id=$id] > }; > (: get a 1000 ids to test :) > let $ids:=subsequence($data//@province/string(.),1,1000) > > for $s in $ids return local:myid1($s) >
Takes 30 seconds!
Replacing the final line with
Quoted text · 2 lines
> for $s in $ids return $data//*[@id=$s] >
The time is 123ms.
/Andy
CG
Christian Grün21 Nov 2011 23:50 #
Hi Andy,
you can speed up the function by adding the data type of the $id argument:
declare function local:myid1($id as xs:string*) {
...
};Otherwise, the query compiler won't know if the incoming ids will be strings, numbers, or of any other type.
Hope this helps,
Christian
Quoted text · Andy Bunce · 31 lines
On Mon, Nov 21, 2011 at 11:39 PM, Andy Bunce <bunce.andy at gmail.com> wrote: > Hi > Using Basex 7.0.2 and the factbook.xml database, I observe the following: > >> declare variable $data:=db:open("factbook"); >> >> declare function local:myid1($id){ >> $data//*[@id=$id] >> }; >> (: get a 1000 ids to test :) >> let $ids:=subsequence($data//@province/string(.),1,1000) >> >> for $s in $ids return local:myid1($s) > > > Takes 30 seconds! > > Replacing the final line with >> >> for $s in $ids return $data//*[@id=$s] > > The time is 123ms. > > /Andy > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > >
AB
Andy Bunce22 Nov 2011 00:08 #
Hi Christian,
Thanks, that reduces the difference in times to ~30ms.
I was not expecting the lack of typing to have such a bad effect. I MUST remember to always declare types from now on :-)
/Andy
On Mon, Nov 21, 2011 at 10:50 PM, Christian Grün
<christian.gruen at gmail.com>wrote:
Quoted text · Christian Grün · 47 lines
> Hi Andy, > > you can speed up the function by adding the data type of the $id argument: > > declare function local:myid1($id as xs:string*) { > ... > }; > > Otherwise, the query compiler won't know if the incoming ids will be > strings, numbers, or of any other type. > > Hope this helps, > Christian > ___________________________ > > On Mon, Nov 21, 2011 at 11:39 PM, Andy Bunce <bunce.andy at gmail.com> wrote: > > Hi > > Using Basex 7.0.2 and the factbook.xml database, I observe the following: > > > >> declare variable $data:=db:open("factbook"); > >> > >> declare function local:myid1($id){ > >> $data//*[@id=$id] > >> }; > >> (: get a 1000 ids to test :) > >> let $ids:=subsequence($data//@province/string(.),1,1000) > >> > >> for $s in $ids return local:myid1($s) > > > > > > Takes 30 seconds! > > > > Replacing the final line with > >> > >> for $s in $ids return $data//*[@id=$s] > > > > The time is 123ms. > > > > /Andy > > > > _______________________________________________ > > BaseX-Talk mailing list > > BaseX-Talk at mailman.uni-konstanz.de > > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > > > > >
CG
Christian Grün22 Nov 2011 00:17 #
Quoted text · Andy Bunce · 3 lines
> Thanks, that reduces the difference in times to ~30ms. > I was not expecting the lack of typing to have such a bad effect. > I MUST remember to always declare types from now on :-)
I agree it's not really obvious that typed variables are so much faster; indeed there are still some cases in which you might get slightly better performance with untyped values. In future, queries such as yours will be optimized at runtime (by generating more than one execution plan and choosing the cheapest at runtime) , but that's still on our todo list..
Your questions are welcome,
Christian
Written for BaseX 7.0. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery