Postgres 9.5
Given a table with one field of type json
, about 700 rows, and where each row has about 4,000 elements in a single array...
my_db_field:
[0.44577, 0.4855, 0.45429, 0.54437,...]
[0.45012, 0.48698, 0.45715, 0.55337,...]
[0.47347, 0.49156, 0.46079, 0.56818,...]
[0.4936, 0.49835, 0.46086, 0.58195,...]
[0.51068, 0.50511, 0.46228, 0.59482,...]
The PostgreSQL docs show how you can query for a single element inside an array.
SELECT my_db_field->2 AS test FROM my_db_table
results:
test (of type json)
--------------------
0.4855
0.48698
0.49156
etc.
What I would like to do, though, is select multiple elements in the array and return that as an array in the same format the rows are. By multiple, I mean around 300 elements in the array; e.g., from element 0 to element 300. Does Postgres have a nice syntax for such a query?
test_id int, idx int, elem text
. prune the json-array out of the json object and move it to the new table. Then you could just doSELECT * FROM test JOIN test_array USING ( test_id ) WHERE idx < 300
. JSON is fine for storing, but I'm questioning whether or not you've outgrown its utility at this point.