Answer the question
In order to leave comments, you need to log in
How to merge multiple rows of a separate table into one in Postgresql 9.3?
Good afternoon!
There is a branched database in POSTGRESQL 9.3 with a total volume of about 10 GB (7K-2M records in each table). The task was to combine several rows of a separate table into one. I'll try to explain with an abstract example. Suppose there is a films table with several attributes (movie id and title). There is also an actors table where a list of actors is stored. The table data is also linked through an intermediate one (to provide many-to-many relationships).
films
id | title film |
one | matrix |
2 | Johnny mnemonic |
3 | Bikers |
id_film | id_actor |
one | one |
one | 2 |
2 | one |
3 | 2 |
id | name |
one | Keanu Reeves |
2 | Laurence Fishburne |
id | title film | actors |
one | matrix | 1- Keanu Reeves | 2- Laurence Fishburne |
2 | Johnny mnemonic | 1- Keanu Reeves |
3 | Bikers | 2- Laurence Fishburne |
Answer the question
In order to leave comments, you need to log in
Such a task arose to form an intermediate table on the basis of which the web application displays the necessary information. Initially, the selection was made from 4 related tables. but such a request is executed for several seconds, which, in principle, is too much for an application. tests with an intermediate table gave a result of 0.8-0.9 seconds, which is becoming more or less acceptable. in the future, it is planned to combine it with the nosql solution, but translating data and rewriting the code for joint use of postgresql and nosql will require significant time costs. Therefore, in fact, the creation of an intermediate solution arose.
the study of manuals and the poke method has so far led to this option.
UPDATE films SET acters = (SELECT json_agg (ALL(actors.id, actors.name))
FROM actor_in_films INNER JOIN actors ON actors.id=actor_in_films.id_actor WHERE actor_in_films.id_film= films.id GROUP BY actor_in_films.id_film);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question