1
Next, my doubt is more about logic anyway..
I have a python script in which I use psycopg2 to perform database searches. But I need to do this within a for.
I have full notion that doing a search within a for can affect and greatly in the performance of the code, besides getting something 'ugly''.
I’ve thought of other solutions such as bringing a general query and working with 1 loop within another to replace select but I think this would give me a high processing in the same way....
When I come across a situation like this the best way out to avoid a select to each for loop?
for busca_filas in rows_sip:
if busca_filas[1] == 'QUEUE':
dict_filas.update({"fisico": busca_filas[2]})
dict_filas.update({"virtual": busca_filas[4]})
cursor.execute("SELECT * FROM queue_members where queue_name = '" + busca_filas[2] + "';")
busca_ramais_filas = cursor.fetchall()
for item_ramais_filas in busca_ramais_filas:
dict_detalhe_filas.update({'nome_fila': item_ramais_filas[0]})
dict_detalhe_filas.update({'ramal': item_ramais_filas[3]})
dict_detalhe_filas.update({'pausado': item_ramais_filas[4]})
list_detalhe_filas.append(dict_detalhe_filas)
dict_detalhe_filas = {}
dict_filas.update({'ramais': list_detalhe_filas})
list_filas.append(dict_filas)
list_detalhe_filas = []
dict_filas = {}
I created this example code just so you understand how my code is.
What you could do, is let SQL work in a relational way as it was optimized to do, that is, bring everything you need the query at once, ie, make a Join with this queue_members, and bring the columns you need, instead of using this asterisk, order the query so that the extensions are always below the right names, and do a single one, using conditional to know if it has been renamed. I think it gets more efficient.
– Zorkind
Wow, something so basic.. kkkkkkkk. I get your idea and actually it’s to improve even more, to the point where I can get the select out of the for. I’m gonna redo this code and post it here to people who have a similar question
– Matheus Suffi
That’s right, that’s the idea, take a look at my answer :-)
– Zorkind