Hello folks!
As I have already mentioned in my earlier posts that my current assignment is based on a COTS product on top of major XQuery implementation, I am putting around this post to share some optimization tips for XQuery for better execution, readability, and other parameters.
XQuery Modularization
Modularization is a method which is used in any programming language like Java/C/C++ etc. and it also applies to XQuery as well. XQuery have its own main modules and sub modules. We can keep on creating sub modules based on our implementation. The point here to note is that XQuery engine creates a cache of all XQueries used before execution. So if you have lot of XQueries and sub XQuery files it will have impact on the execution engine to patch up XQueries. It is advisable to create Sub-Modules only if they will be shared by different main modules. If only one module is using a specific sub module function, it should be merged with the main XQuery module.
Loading XML Data
XQuery being a XML processing language, it require XML data source always to process. We read XML files in every single line of XQuery at least in my work! So practice here is not to use an external parser to parse an XML file or a source either the XML should be passed as an external object to the XQuery module or you should use “fn:doc” method to read XML files.
Streaming large XML Data
If your query’s results can be large, stream the results to an OutputStream or use SAX or StAX, so that retrieving the results does not introduce issues with memory or performance. Retrieving large query results as DOM can use large amounts of memory and significantly increase the time needed for processing queries. In XQuery if you use saxon:serialize, this will have impact on performance.
I will add more points soon. Please feel free to provide any of your observations.