I have data in a table that look like this:
src_id, dst_id, params
int , int , array
I'm querying the data to extract some values from the array with the following Django query
dataset = query_set.values_list('src_id', 'dst_id', *[e.field for e in settings])
I need to output the data like this:
[
[1,2,[a,b,c,d]],
[3,4,[a,c,d,e]],
...
]
but the values_list
returns a list of tuples like
(1,2,a,b,c,d)(3,4,a,c,d,e)...
So I've been doing the following to recombine the tuple values into an array:
[[d[0], d[1], d[2:]] for d in dataset]
It works fine, but I have 1M+ rows, and it is slow. It is also using a lot of memory. Ideally I want to format the data properly in the SQL (through Django) and push the output directly to the JSON serializer without having to generate an intermediate array.
Is there a way to optimize this code?
I looked at using a loop, or lambda but that doesn't make much of a difference. I looked at using array
but it only takes primitive types, so no much luck with array of arrays.
I am looking for a way to query the DB and output the data directly in the right format, if possible in Django:
I see Django can do slice array lookup like
dataset = query_set.values_list('src_id', 'dst_id', 'data__3_10')
to get the values between indices 3 and 10 for example, but I need to retrieve specific indices (like 1,4,5,6,8,11 for example), not slices.
Any hint on how to format the output of a query like this?
is it possible, or just too esoteric for Django?
Do I need raw
SQL for this?
Thanks