Update many rows in a postgres table using psycopg2 and python
<p>I have a table T with two columns: Tag (text) and Score (real). The Tags remain constant (and there can be several rows with the same Tag), while the Scores change often. The new values are presented in a python object like this (possibly with millions of items):</p>

<pre><code>recs = [{'tag': t1, 'score': s1}, {'tag': t2, 'score': s2}]
</code></pre>

<p>I am currently using the following python code to update the scores in the table:</p>

<pre><code>db_conn = psycopg2.connect(connect_string)
cursor = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
q = "UPDATE T SET Score = %(score)s WHERE Tag = %(tag)s;"
cursor.executemany(q, recs)
</code></pre>

<p>While this works, it is quite slow. So, I am looking for a solution that improves the speed of the code above. Any recommendation is welcome.</p>