From dd96155bcefe1459eba59272fe06e60255760105 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Thu, 28 Jun 2012 13:57:04 -0700 Subject: [PATCH] tdb_sql: Optimistically do UPDATES and fall back to INSERTS. Doing a SELECT on the data table to determine which keys already exist was adding too much extra load to the Account master. This attempts to alleviate that by optimistically trying an UPDATE and if no rows were actually changed by the UPDATE, doing an INSERT instead. --- r2/r2/lib/db/tdb_sql.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/r2/r2/lib/db/tdb_sql.py b/r2/r2/lib/db/tdb_sql.py index ba7b431b9..64cf83d82 100644 --- a/r2/r2/lib/db/tdb_sql.py +++ b/r2/r2/lib/db/tdb_sql.py @@ -491,12 +491,8 @@ def db2py(val, kind): #TODO i don't need type_id def set_data(table, type_id, thing_id, **vals): - s = sa.select([table.c.key], sa.and_(table.c.thing_id == thing_id)) - transactions.add_engine(table.bind) - keys = [x.key for x in s.execute().fetchall()] - i = table.insert(values = dict(thing_id = thing_id)) u = table.update(sa.and_(table.c.thing_id == thing_id, table.c.key == sa.bindparam('_key'))) @@ -504,14 +500,13 @@ def set_data(table, type_id, thing_id, **vals): for key, val in vals.iteritems(): val, kind = py2db(val, return_kind=True) - #TODO one update? - if key in keys: - u.execute(_key = key, value = val, kind = kind) - else: + uresult = u.execute(_key = key, value = val, kind = kind) + if not uresult.rowcount: inserts.append({'key':key, 'value':val, 'kind': kind}) #do one insert if inserts: + i = table.insert(values = dict(thing_id = thing_id)) i.execute(*inserts) def incr_data_prop(table, type_id, thing_id, prop, amount):