XQueries Flashcards
Xqueries
A basic definition is to say they are SQL for XML files, and is an extension of XPaths.
FLWR
More general XQuery.
F -> for
L -> let
W -> where
R -> return
FLWR Example
let $doc := doc(“mydoc.xml)
for $s in $doc/university/student
where $s/module = “Computer Science”
return $s/name
let $doc := doc(“mydoc.xml)
The let clause, where we define the document we are going into.
for $s in $doc/university/student
The for clause, where we define the for loop.
where $s/module = “Computer Science”
The where clause, specifying conditions for the return.
return $s/name
The return clause, aka what is returned.
Return clause pairs
<pair>{variableone},{variabletwo}</pair>
What is returned
If we were doing this example:
let $doc := doc(“mydoc.xml)
for $s in $doc/university/student
where $s/module = “Computer Science”
return $s/name
an example for a return could be:
<name>Anna</name>
Order by
let $doc := doc(“mydoc.xml)
for $s in $doc/university/student
where $s/module = “Computer Science”
order by $s/name
return $s/name
Group by
let $doc := doc(“mydoc.xml”)
for $m in $doc/university/student/module
group by $mod:=$m
return <pair>{$mod},{count($m)}</pair>
Distinct values
distinct-values(let $doc := doc(“mydoc.xml)
for $m in $doc/university/student/module
return $m)
Three ways of XML to SQL
- Store XML documents as entries of a table.
- Store XML documents in schema-independent form.
- Store XML documents in shredded form across a number of attributes and relations.
Storing XML as an attribute
Raw XML is stored in serialised form, which makes it efficient to insert document into database and retrieve them in their original form since it is all serial.
Storing XML as a schema-independent representation
In simple terms, this means storing it as a tree structure in our database.
Recursive nature of schema-independent representation
Can cause problems, since if we had to retrieve something three layers deep, that’s three recursions, aka three queries.
Denormalised index
Overcomes recursive problem by containing combinations of path expressions and a link to the node and parent node.
Essentially, its a table which gives us the index of what we want instead of having to traverse the tree itself.
Storing XML in shredded form
In other words, putting extracted information from XML into database.
XML is decomposed into its constituent elements and data distributed over number of attributes in one or more relations.
All of this makes it easier for indexing values of some elements, and can also provide hierarchy.
NoSQL
“not only SQL” or “not relational”.
NoSQL Advantages
- Faster access to data.
- Fault-tolerance.
- Flexibility in data storage.
- Full ACID compliance can sometimes be relaxed.
Web Service database split
NoSQL Database:
Used for slightly less important things, like user profiles or shopping carts. This data doesn’t always need to be 100% correct; if something is not updated properly, it isn’t devastating. As we can see, the way it is store is slightly less strict, and contains very simply queries.
Relational Database:
Very important data like credit card transactions; requires compliancy and security.
NoSQL database setup
Typically distributed with nodes running on commodity hardware with a standard network.
NoSQL database setup advantages
- availability
- consistency (not the same as acid)
- scalability
- high performance
- partition tolerance
Availability
Every non-failing node always execute queries.