If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Thursday, June 14, 2012

Regular xquery versus xquery using index [Sedna XMLDB]

This article will show you 2 different ways of querying for data. Mainly accessing the collection directly versus using an index to fetch the data.
declare function local:getBasicTypes($productIds as xs:string*) as element(Product)* {
    (: remark: @identifier = $productIds acts like a SQL @identifier in $productIds :)
    collection("basicTypes/released")/Product[@identifier = $productIds]
};

let $ids := ('PH3330L', 'PH3030CL')
return 

  {
    for $bt in local:getBasicTypes($ids)
    return $bt/ProductInformation/Description
  }


Now let us create an index for the @identifier and retrieve the data using this index
create index "basictype_id"
  on fn:collection("basicTypes/released")/Product
  by @identifier
  as xs:string

declare function local:getBasicTypesByIndex($productIds as xs:string*) as element(Product)* {
    for $id in $productIds return index-scan('basictype_id', $id, 'EQ')
};

let $ids := ('PH3330L', 'PH3030CL')
return 

  {
    for $bt in local:getBasicTypesByIndex($ids)
    return $bt/ProductInformation/Description
  }


The result is the same for both:
<result>
  <Description>N-channel TrenchMOS logic level FET</Description>
  <Description>9657 Trench 7 (IMPULSE)</Description>
</result>

Here is an example of creating an index using namespaces:
declare namespace cat = "urn:iso:std:iso:ts:29002:-10:ed-1:tech:xml-schema:catalogue";
create index "legacy_id"
  on fn:collection("legacyBasicTypes")/cat:catalogue
  by cat:item/cat:reference/@reference_number
  as xs:string

No comments:

Post a Comment