Optimization in XSLT and XQuery Michael Kay. 2 Challenges XSLT/XQuery are high-level declarative languages: performance depends on good optimization Performance.

Slides:



Advertisements
Similar presentations
Inside an XSLT Processor Michael Kay, ICL 19 May 2000.
Advertisements

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Symbol Table.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
CSE 6331 © Leonidas Fegaras XML and Relational Databases 1 XML and Relational Databases Leonidas Fegaras.
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
Chapter 7: User-Defined Functions II
1 XQuery Web and Database Management System. 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery?
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Compiler Construction
Paper by: A. Balmin, T. Eliaz, J. Hornibrook, L. Lim, G. M. Lohman, D. Simmen, M. Wang, C. Zhang Slides and Presentation By: Justin Weaver.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Tutorial 6 & 7 Symbol Table
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
1 Indexing and Querying XML Data for Regular Path Expressions A Paper by Quanzhong Li and Bongki Moon Presented by Amnon Shochot.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Environments and Evaluation
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Types Type = Why? a set of values
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Access Path Selection in a Relation Database Management System (summarized in section 2)
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Query Processing Presented by Aung S. Win.
10/06/041 XSLT: crash course or Programming Language Design Principle XSLT-intro.ppt 10, Jun, 2004.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
XML Processing Moves Forward XSLT 2.0 and XQuery 1.0 Michael Kay Prague 2005.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
Comparing XSLT and XQuery Michael Kay XTech 2005.
Efficient Evaluation of XQuery over Streaming Data Xiaogang Li Gagan Agrawal The Ohio State University.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Access Path Selection in a Relational Database Management System Selinger et al.
Database Management 9. course. Execution of queries.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Query Optimization (CB Chapter ) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems: An Application Oriented.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
1 Typing XQuery WANG Zhen (Selina) Something about the Internship Group Name: PROTHEO, Inria, France Research: Rewriting and strategies, Constraints,
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
1 Minggu 6, Pertemuan 12 Query Processing Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Efficient Evaluation of XQuery over Streaming Data
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Prepared by : Ankit Patel (226)
Optimization Code Optimization ©SoftMoore Consulting.
Database Performance Tuning and Query Optimization
Semantic Analysis Chapter 6.
Semantic Analysis Chapter 6.
Chapter 11 Database Performance Tuning and Query Optimization
Compiler Construction
Course Overview PART I: overview material PART II: inside a compiler
Compiler Construction
Presentation transcript:

Optimization in XSLT and XQuery Michael Kay

2 Challenges XSLT/XQuery are high-level declarative languages: performance depends on good optimization Performance also depends on good programming! How can users write good programs if they don’t know what the optimizer is doing?

3 What is optimization? Widest sense: –Everything that’s done to make your query go fast Narrower sense: –Expression rewriting: replacing the code that you write with equivalent, faster code that has the same effect

4 Main performance contributors Efficient internal coding Tree model for documents Streamed execution (pipelining) + lazy evaluation Rewrite optimizations –Including join optimization Tail recursion XSLT template rule matching

5 Databases vs. in-memory processors Databases –90% of optimization is about finding and using indexes –You can spend more time building the data to reduce query costs –Indexes are long-lived –Queries may be repeatable or one-off In-memory processors –Loading the data is a significant part of the overall cost –Memory utilization needs to be minimized –Indexes, if used, are transient –Queries/Stylesheets may be repeatable or one-off

6 The Saxon TinyTree Model Requirements: –Low memory footprint –Fast construction –Fast access paths –Support for document order Non-requirement: –In-situ update

7 TinyTree example 12 Prague assume whitespace is stripped nrdepthkindnamenext/ owner typedata 00 DOC ELEM 3028 (root) ELEM 3164 (a) 4INT 33 TEXT -2-►12 42 ELEM 3165 (b) 1STR 53 TEXT -4-► ”Prague” 60 STOP ----

8 TinyTree: key points No “object-per-node” overhead Names held as integer codes Fast child navigation Fast document order comparison Extra information added dynamically if needed: –preceding-sibling pointers –Base-uri, line numbers etc –Indexes

9 Streaming (Pipelining) Common practice in set-based languages –Functional programming languages –SQL Each node in the expression tree can deliver its results incrementally to the parent node Can be implemented as pull or push (Saxon uses both)

10 Example: filter expressions Class FilterExpressionIterator { public Item next() { while (true) { Item item = base.next(); if (item == EOS) return EOS; if (matches(item, predicate)) return item; } $nodes[x=1] filter $nodes = x1

11 Example: Many-to-One Comparisons Class ManyToOneComparisonEvaluator { public boolean evaluate () { while (true) { Item item = lhs.next(); if (item = rhs) return true; } return false; } x=1 = x1

12 Benefits of Streaming Saves memory –No memory for intermediate results –Allocating and de-allocating memory takes time Early exit, for example in –(a/b/c/d)[1] –book[author = ‘Smith’]

13 Lazy Evaluation Closely associated with streaming Variables and function arguments are not evaluated until the value is needed Benefits: –The value might never be needed –Only part of the value might be needed (early exit) –Memory is used for the minimum time

14 Compile-time Expression Rewrites General approach: 1.Parse the source code into an expression tree 2.Resolve references (variables, functions) 3.Decorate the tree with attributes –Type of an expression –Dependencies of an expression –Other properties, e.g. whether a node-set is sorted 4.Scan the tree repeatedly to identify expressions that can be replaced by faster equivalents

15 Two kinds of rewrites Rewrites that could have been done by the programmer –count(A) > 3 ►exists(A[4]) Rewrites that use constructs not available to the programmer –A[position()=last()] ► A[isLast()]

16 Some important rewrites Sort removal –Not sorting path expressions where the result is already sorted Constant subexpressions –Evaluated at compile time where possible Extracting subexpressions from loops Distributing WHERE conditions + many ad-hoc rewrites

17 Some rewrites that Saxon doesn’t yet do Inline expansion of variable references Inline expansion of function calls Detecting common subexpressions Creating new global variables

18 Type Checking and its effect on performance XQuery and XSLT 2.0 allow you to declare types of variables and functions –But it’s not mandatory Main benefit is better error detection Type information can also be used by the optimizer –With Saxon, this rarely makes a big difference

19 “Optimistic static type checking” The static type of an expression S is compared with the required type R Possible outcomes: –S is a subtype of R: no action needed –S overlaps with R: run-time type-checking code is generated –S and R are disjoint: static error reported Special case: –integer* and string* overlap (both allow the empty sequence)

20 Join Optimization Less important in XQuery than in SQL –Except that some people write XQuery as if it were SQL General strategy in Saxon-SA: –Distribute the join predicates (turn WHERE clauses into filter expressions) –Use indexed lookup for predicates where appropriate

21 Indexes in Saxon-SA Explicit user-defined indexes –xsl:key Implicit document-level indexes Implicit sequence-level indexes = $param] Hash tables for many-to-many “=“ –book[keyword = $keywords]

22 Some tips for effective indexing Declare your types Avoid untypedAtomic –use a schema Use “eq” rather than “=“

23 Tail Recursion See the printed paper

24 Conclusions Optimization techniques are similar for XSLT and XQuery –But vary between database products and in-memory processors Compile-time techniques –Type analysis –Expression rewriting Run-time techniques –Streaming/pipelining –etc