I
I
Ivan2019-01-04 07:51:08
Python
Ivan, 2019-01-04 07:51:08

Do I need to combine SQL queries into one?

Hello.
there is a table in a DB in it it is necessary to enter three fields, depending on results of certain actions.
I wrote three separate queries to the db.
In this case, performance does not matter, however, is it correct to do this in some production code/projects?
Those. questions:
1. what is better three separate requests, or to push all in one? (despite the fact that it is possible (but not sure) the parse_data field will be updated sometime separately).
2. Perhaps there are some comments on the code. (I'm probably not doing string concatenation properly?).

# статус выгрузки TRUE
    def set_parse_status_true(self,id_num):
        cursor = self.conn.cursor()
        id_num = str(id_num)
        sql_update = "UPDATE customers SET is_parse = TRUE WHERE id = '" + id_num + "';"
        cursor.execute(sql_update)
        self.conn.commit()
        cursor.close()
    
    # устанавливаем дату выгрузки
    def set_parse_data(self,id_num,parse_data):
        cursor = self.conn.cursor()
        id_num = str(id_num)
        parse_data = str(parse_data)
        sql_update = "UPDATE customers SET parse_data = '" + parse_data + "' WHERE id = '" + id_num + "';"
        cursor.execute(sql_update)
        self.conn.commit()
        cursor.close()

    # прописываем путь до выгрузки
    def set_path_link(self,in_num,path):
        cursor = self.conn.cursor()
        id_num = str(id_num)
        sql_update = "UPDATE customers SET path_link = '" + path + "' WHERE id = '" + id_num + "';"
        cursor.execute(sql_update)
        self.conn.commit()
        cursor.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2019-01-04
@Iv_and_S

in general:
- the combat code is different: one processes 100 requests per hour, the other - 1000 requests per minute / second.
- requests are also different, and are performed differently; sometimes combining into one can give a profit, sometimes not.
specifically: you update the same table (in one thread, if I understand correctly), it makes sense to do this in one request. data preparation can be scattered over different functions, but it is more optimal to make a single query. if later you need to separately update some field, well, update it, it will already be a different functionality.
better not in terms of performance (partially with it), but in terms of code cohesion: imagine that you need to change the name of the table from customers toclients - is it easier to do this in one function or in several?
premature optimization is evil. refactoring is an inevitable companion of a programmer (if he is not "herak, hereak - and in production").

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question