Create a database and populate it with the result of a query
Thursday, 22 July 2021 · 4 messages · last reply 30 Jul 2021
JR
Jonathan Robie22 Jul 2021 17:33 #
I would like to be able to create a database and populate it with the result of a query done on multiple databases.
How do I do that?
Jonathan
KK
Kristian Kankainen23 Jul 2021 07:52 #
Hello Jonathan,
There are very many answers to your question and you should be more clear in what you want (how is the old database organized? how do you want to organize the new database? what items do you want to carry over from the old to the new db? etc). Depending on this, it might be good to first create a copy of the old database and remove unnecessary things from it and augment it with populating from the other databases.
For creating and updating databases, the core thing to know (in my humble opinion) is the concept of the Pending Update List [1]. This means that a query does not make any updates on a db while the query is running, it instead creates a list of updates and these updates are carried out *after* the query ran *successfully*. This gives a lot of opportunities for optimisations behind the scenes.
Anyway, this implies that the database needs to be existing before you run the query for populating it. This means you need to do it in two runs, first create the database:
db:create($name-of-the-new-db)after that you need to run the query or queries that populates it, e.g something like:
let $new-item :=
<new-item>
{
for $old-item in db:open($old-db)/with/some/items
(: populate new-item with only 'this' and 'that' attributes from the old items :)
return ($old-item/@this, $old-item/@that)
}
</new-item>
return db:add($name-of-the-new-db, $new-item, $path-to-new-item)As your question is very open, I would say it is the organisation of the $path-to-new-item that gives most room for answers. Do you want to have everything in one file or many files?
Best rergards,
Kristian Kankainen
[1]: https://docs.basex.org/wiki/XQuery_Update#Pending_Update_List
Quoted text · Jonathan Robie · 7 lines
> On 22. Jul 2021, at 16:33, Jonathan Robie <jonathan.robie at gmail.com> wrote: > > I would like to be able to create a database and populate it with the result of a query done on multiple databases. > > How do I do that? > > Jonathan
CG
Christian Grün23 Jul 2021 10:30 #
…one more note: It’s also possible to pass on initial input to
db:create:let $cities := element cities {
for $i in 0 to 9
return db:open('countries' || $i)//city
}
return db:create('db', $cities, 'cities.xml')Best,
Christian
Quoted text · Kristian Kankainen · 42 lines
On Fri, Jul 23, 2021 at 7:53 AM Kristian Kankainen <kristian at keeleleek.ee> wrote: > > Hello Jonathan, > > There are very many answers to your question and you should be more clear in what you want (how is the old database organized? how do you want to organize the new database? what items do you want to carry over from the old to the new db? etc). Depending on this, it might be good to first create a copy of the old database and remove unnecessary things from it and augment it with populating from the other databases. > > For creating and updating databases, the core thing to know (in my humble opinion) is the concept of the Pending Update List [1]. This means that a query does not make any updates on a db while the query is running, it instead creates a list of updates and these updates are carried out *after* the query ran *successfully*. This gives a lot of opportunities for optimisations behind the scenes. > > Anyway, this implies that the database needs to be existing before you run the query for populating it. This means you need to do it in two runs, first create the database: > > db:create($name-of-the-new-db) > > after that you need to run the query or queries that populates it, e.g something like: > > let $new-item := > <new-item> > { > for $old-item in db:open($old-db)/with/some/items > (: populate new-item with only 'this' and 'that' attributes from the old items :) > return ($old-item/@this, $old-item/@that) > } > </new-item> > return db:add($name-of-the-new-db, $new-item, $path-to-new-item) > > As your question is very open, I would say it is the organisation of the $path-to-new-item that gives most room for answers. Do you want to have everything in one file or many files? > > Best rergards, > Kristian Kankainen > > [1]: https://docs.basex.org/wiki/XQuery_Update#Pending_Update_List > > > On 22. Jul 2021, at 16:33, Jonathan Robie <jonathan.robie at gmail.com> wrote: > > I would like to be able to create a database and populate it with the result of a query done on multiple databases. > > How do I do that? > > Jonathan > >
JR
Jonathan Robie30 Jul 2021 00:40 #
Thanks Kristian and Christian,
Very helpful answers. As usual.
Jonathan
Quoted text · Christian Grün · 76 lines
On Fri, Jul 23, 2021 at 4:30 AM Christian Grün <christian.gruen at gmail.com> wrote: > …one more note: It’s also possible to pass on initial input to db:create: > > let $cities := element cities { > for $i in 0 to 9 > return db:open('countries' || $i)//city > } > return db:create('db', $cities, 'cities.xml') > > Best, > Christian > > > > On Fri, Jul 23, 2021 at 7:53 AM Kristian Kankainen > <kristian at keeleleek.ee> wrote: > > > > Hello Jonathan, > > > > There are very many answers to your question and you should be more > clear in what you want (how is the old database organized? how do you want > to organize the new database? what items do you want to carry over from the > old to the new db? etc). Depending on this, it might be good to first > create a copy of the old database and remove unnecessary things from it and > augment it with populating from the other databases. > > > > For creating and updating databases, the core thing to know (in my > humble opinion) is the concept of the Pending Update List [1]. This means > that a query does not make any updates on a db while the query is running, > it instead creates a list of updates and these updates are carried out > *after* the query ran *successfully*. This gives a lot of opportunities for > optimisations behind the scenes. > > > > Anyway, this implies that the database needs to be existing before you > run the query for populating it. This means you need to do it in two runs, > first create the database: > > > > db:create($name-of-the-new-db) > > > > after that you need to run the query or queries that populates it, e.g > something like: > > > > let $new-item := > > <new-item> > > { > > for $old-item in db:open($old-db)/with/some/items > > (: populate new-item with only 'this' and 'that' attributes from the > old items :) > > return ($old-item/@this, $old-item/@that) > > } > > </new-item> > > return db:add($name-of-the-new-db, $new-item, $path-to-new-item) > > > > As your question is very open, I would say it is the organisation of the > $path-to-new-item that gives most room for answers. Do you want to have > everything in one file or many files? > > > > Best rergards, > > Kristian Kankainen > > > > [1]: https://docs.basex.org/wiki/XQuery_Update#Pending_Update_List > > > > > > On 22. Jul 2021, at 16:33, Jonathan Robie <jonathan.robie at gmail.com> > wrote: > > > > I would like to be able to create a database and populate it with the > result of a query done on multiple databases. > > > > How do I do that? > > > > Jonathan > > > > >
Written for BaseX 9.5. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery