Clinton
Clinton

Reputation: 23135

How do XML Databases do deep queries?

I'm asking a question about how XML Databases work, but I'm going to give an example I understand on a standard relational database, in the hope people can explain how it works on an XML database.

Lets say our data has:

Countries
Businesses
Employees

In a relational database, you'd probably do the following:

COUNTRY_BUSINESS_TAB: COUNTRY_ID, BUSINESS_ID
BUSINESS_EMPLOYEES_TAB: BUSINESS_ID, EMPLOYEE_ID

Lets forget about giving meaningful names for the IDs and just work in terms of IDs directly.

Now, lets say if we want to search by employee, that's easy, we put an index EMPLOYEE_ID in BUSINESS_EMPLOYEES_TAB and we can quickly get the business ids of where employees works. Once the indexes are set up there is no need for a full table scan.

Now lets instead say the data is in XML format. At the top level there are a whole lot of country IDs in tags. As sub tags there are business IDs. And as subtags of them there are employee IDs.

Can an XML database quickly find all the places an employee works without scanning the whole document?

I ask because I've got a whole lot of XML data, which I was thinking of parsing and putting in an SQL database, but I'm now considering putting it directly in an XML database, like BaseX, and using XQuery instead of SQL. An explanation or simply a reference to read myself on how this issue is resolved in an XML database is all I need.

Upvotes: 1

Views: 90

Answers (1)

Christian Grün
Christian Grün

Reputation: 6229

XML Databases provide the XPath and XQuery languages to query your XML data. The following query could e.g. be used to return the places an employee works (to make it work, you’ll surely have to adapt it to your actual XML data):

//employee[name = 'john']/parent::business/parent::country/@name

In some XML DBMS, such as BaseX, the query above will automatically be rewritten to take advantage of index structures. This way, only those XML text nodes will be retrieved by the query processor that are important for your query result.

Upvotes: 1

Related Questions