0

I'm trying to perform over than 10k queries to mysql server from Python and here is my prepared query statement

UPDATE `rates` 
SET `in`=%s, `out`=%s, `amount`=%s, `minamount`=%s, `maxamount`=%s 
WHERE `from`='EXMRUB' AND `to`='YAMRUB' AND `id_site`= 1

The problem is that it runs really slow, I mean it takes about a 5 minute to perform 10k updates, how can I modify, my query to make it faster?

The Impaler
  • 45,731
  • 9
  • 39
  • 76
DispooL
  • 37
  • 1
  • 6

2 Answers2

1

Assuming that id_site is your table's primary key, you could use a little MySQL trick in combination with executemany to speed up the update:

INSERT INTO rates (id_site, `in`, `out`, amount, minamount, maxamount)
    VALUES (%s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
    `in`=VALUES(`in`)
,   `out`=VALUES(`out`)
,   amount=VALUES(amount)
,   minamount=VALUES(minamount)
,   maxamount=VALUES(maxamount);

executemany is very important, because it makes sure that the loop is not done on the client side.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You can speed up each individual query with an index on (``from``, ``to``, ``id_site``).

However, you are probably better off loading the data into a separate table in the database and then doing the update in a single statement, using join.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786