From: Joe W. <jo...@gm...> - 2011-01-17 18:16:01
|
Hi Gary, > I replaced : > > for $art in collection('/db/laws')//ARTICLE > return $art/META/META_COMMUN[fn:contains(ID,'3449')] > > with : > > collection('/db/laws')//ARTICLE/META/META_COMMUN[ngram:contains(ID,'3449')] > > and the execution time is much better ! (140 ms) Let's take a look at what you did that led to this speed up: 1. You replaced a FLWOR expression with an XPath expression, and in so doing you processed the most selective expression first. These two steps are actually both recommended in the "Query Tuning" article (http://exist-db.org/tuning.html). "Process the most selective expression first" is in section 4.2, and "Prefer XPath predicates over where expressions" is section 4.4 (although to be precise, you didn't use a "where" expression, but you did put the restrictive expression at the end of the expression). 2. You replaced the fn:contains() function with ngram:contains(). You didn't tell us if you actually applied an NGram index to the ID element or not. Didn't you only apply an xs:string range index? If so, you should continue to use fn:contains() instead of ngram:contains(). I believe that for your query, range and ngram indexes should perform be equally well. There may even be some additional opportunities for optimizing your query. If you read through the Query Tuning article, you'll notice that section 4.1 is "Prefer short paths". Taking this suggestion into account (and assuming you are using NGram indexes here), you could try: collection('/db/laws')//META_COMMUN[ngram:contains(ID,'3449')] or even: collection('/db/laws')//ID[ngram:contains(.,'3449')] If it is critical for your query that you only match ID elements that have META_COMMUN parent elements or ARTICLE ancestor elements, you can specify this in a further predicate: collection('/db/laws')//ID[ngram:contains(.,'3449')][parent::META_COMMUN] collection('/db/laws')//ID[ngram:contains(.,'3449')][ancestor::ARTICLE] Of course, which of these queries work best for you depend on your data. But if you can follow the advice in the "Query Tuning" article, you can really improve your query speeds. My suggestions reflect the advice in Section 4.7 ("Use the ancestor or parent axis instead of a top-down approach") and Section 4.2 ("Always process the most selective filter/expression first"). I hope these suggestions are helpful. Let us know if you have any questions. Cheers, Joe |