O'Reilly logo

XQuery, 2nd Edition by Priscilla Walmsley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 16. Writing Better Queries

Well-designed, robust queries have the advantages of running faster and with fewer errors, as well as being easier to debug and maintain. This chapter describes some of the goals of query design, with particular attention to handling errors and tuning for performance.

Query Design Goals

Some of the elements of good query design include:

Clarity

Queries that clearly convey their meaning are much easier to understand and therefore to maintain.

Modularity

Expressions should be reusable in many parts of a query and across multiple queries.

Robustness

Queries should be able to handle all possible combinations of values of input data.

Error handling

Queries should handle dynamic errors gracefully, with useful messages.

Performance

Queries should be tuned for performance.

The rest of this chapter takes a closer look at these design goals.

Clarity

You can increase the clarity of your queries by improving the layout of the query, making appropriate use of names, and using comments liberally. In addition to the recommendations in this chapter, you can go to http://xqdoc.org/xquery-style.html for some more detailed XQuery style conventions.

Improving the Layout

To make the structure of a query more obvious, you should make appropriate use of whitespace and parentheses. Whitespace (line breaks, spaces, and tabs) is allowed anywhere between keywords to make it more readable.

It is helpful to split longer FLWOR and conditional expressions into multiple lines and indent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required