Querying with arrays
От | Tim Dudgeon |
---|---|
Тема | Querying with arrays |
Дата | |
Msg-id | 547722A7.4040702@gmail.com обсуждение исходный текст |
Ответы |
Re: Querying with arrays
|
Список | pgsql-sql |
I'm considering using arrays to handle managing "lists" of rows (I know this may not be the best approach, but bear withme).<br /><br /> I create a table for my lists like this:<b id="docs-internal-guid-b52591dd-f159-1b1d-dee1-e0c932294de6"style="font-weight:normal;"><span style="font-size:15px;font-family:Arial;color:#000000;background-color:transparent;font-weight:normal;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;"></span></b><br /><br/> create table lists (<br /> id SERIAL PRIMARY KEY,<br /> hits INTEGER[] NOT NULL<br /> );<br /><br /> Then I caninsert the results of a query into that table as a new list of hits<br /><br /> INSERT INTO lists (hits)<br /> SELECTarray_agg(id)<br /> FROM some_table<br /> WHERE ...;<br /><br /> Now the problem part. How to best use that array ofprimary key values to restore the data at a later stage. Conceptually I'm wanting this:<br /><br /> SELECT * from some_table<br /> WHERE id <is in the list of ids in the array in the lists table>;<br /><br /> These both work by arereally slow:<br /><br /> SELECT t1.*<br /> FROM some_table t1<br /> WHERE t1.id IN (SELECT unnest(hits) from lists WHEREid = 2);<br /><br /> SELECT t1.*<br /> FROM some_table t1<br /> JOIN lists l ON t1.id = any(l.hits)<br /> WHERE l.id= 2;<br /><br /> Is there an efficient way to do this, or is this a dead end?<br /><br /> Thanks<br /> Tim<br /><br /><br/><br /><br /><br /><br />
В списке pgsql-sql по дате отправления: