From 838c626bba1ffcf8da1d31e2d1735ed9e90ae327 Mon Sep 17 00:00:00 2001 From: "chaz@pokeit.co" Date: Fri, 31 Dec 2010 15:30:29 -0500 Subject: [PATCH] Changed the HandsActions table join index from 'handsPlayersId' to 'handId' and 'playerId'. This allows us to use executemany(), which is faster, in storeHandsPlayers when storing actions. --- pyfpdb/Database.py | 31 +++++++++++++------------------ pyfpdb/Hand.py | 8 ++++---- pyfpdb/SQL.py | 30 ++++++++++++++++-------------- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 8ed0e3bc..e1b097fd 100644 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -73,7 +73,7 @@ except ImportError: use_numpy = False -DB_VERSION = 147 +DB_VERSION = 148 # Variance created as sqlite has a bunch of undefined aggregate functions. @@ -125,7 +125,8 @@ class Database: {'tab':'Gametypes', 'col':'siteId', 'drop':0} , {'tab':'Hands', 'col':'gametypeId', 'drop':0} # mct 22/3/09 #, {'tab':'Hands', 'col':'siteHandNo', 'drop':0} unique indexes not dropped - , {'tab':'HandsActions', 'col':'handsPlayerId', 'drop':0} + , {'tab':'HandsActions', 'col':'handId', 'drop':1} + , {'tab':'HandsActions', 'col':'playerId', 'drop':1} , {'tab':'HandsActions', 'col':'actionId', 'drop':1} , {'tab':'HandsPlayers', 'col':'handId', 'drop':1} , {'tab':'HandsPlayers', 'col':'playerId', 'drop':1} @@ -150,7 +151,8 @@ class Database: , {'tab':'HandsPlayers', 'col':'handId', 'drop':0} , {'tab':'HandsPlayers', 'col':'playerId', 'drop':0} , {'tab':'HandsPlayers', 'col':'tourneysPlayersId', 'drop':0} - , {'tab':'HandsActions', 'col':'handsPlayerId', 'drop':0} + , {'tab':'HandsActions', 'col':'handId', 'drop':0} + , {'tab':'HandsActions', 'col':'playerId', 'drop':0} , {'tab':'HandsActions', 'col':'actionId', 'drop':1} , {'tab':'HudCache', 'col':'gametypeId', 'drop':1} , {'tab':'HudCache', 'col':'playerId', 'drop':0} @@ -174,7 +176,8 @@ class Database: , {'fktab':'HandsPlayers', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} , {'fktab':'HandsPlayers', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} , {'fktab':'HandsPlayers', 'fkcol':'tourneysPlayersId','rtab':'TourneysPlayers','rcol':'id', 'drop':1} - , {'fktab':'HandsActions', 'fkcol':'handsPlayerId', 'rtab':'HandsPlayers', 'rcol':'id', 'drop':1} + , {'fktab':'HandsActions', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} + , {'fktab':'HandsActions', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} , {'fktab':'HandsActions', 'fkcol':'actionId', 'rtab':'Actions', 'rcol':'id', 'drop':1} , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} , {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} @@ -184,7 +187,8 @@ class Database: {'fktab':'Hands', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} , {'fktab':'HandsPlayers', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} , {'fktab':'HandsPlayers', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} - , {'fktab':'HandsActions', 'fkcol':'handsPlayerId', 'rtab':'HandsPlayers', 'rcol':'id', 'drop':1} + , {'fktab':'HandsActions', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} + , {'fktab':'HandsActions', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} , {'fktab':'HandsActions', 'fkcol':'actionId', 'rtab':'Actions', 'rcol':'id', 'drop':1} , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} , {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} @@ -1755,7 +1759,6 @@ class Database: pp.pprint(pdata) inserts = [] - hpid = {} for p in pdata: inserts.append( (hid, pids[p], @@ -1860,17 +1863,9 @@ class Database: #print "DEBUG: inserts: %s" %inserts #print "DEBUG: q: %s" % q c = self.get_cursor() + c.executemany(q, inserts) - if self.import_options['saveActions']: - for r in inserts: - c.execute(q, r) - hpid[(r[0], r[1])] = self.get_last_insert_id(c) - else: - c.executemany(q, inserts) - - return hpid - - def storeHandsActions(self, hid, pids, hpid, adata, printdata = False): + def storeHandsActions(self, hid, pids, adata, printdata = False): #print "DEBUG: %s %s %s" %(hid, pids, adata) # This can be used to generate test data. Currently unused @@ -1881,8 +1876,8 @@ class Database: inserts = [] for a in adata: - inserts.append( (hpid[(hid, pids[adata[a]['player']])], - #self.getHandsPlayerId(self.hid, pids[adata[a]['player']]), + inserts.append( (hid, + pids[adata[a]['player']], adata[a]['street'], adata[a]['actionNo'], adata[a]['streetActionNo'], diff --git a/pyfpdb/Hand.py b/pyfpdb/Hand.py index 405a56e5..a3a1059f 100644 --- a/pyfpdb/Hand.py +++ b/pyfpdb/Hand.py @@ -266,11 +266,11 @@ db: a connected Database object""" hh['seats'] = len(self.dbid_pids) self.dbid_hands = db.storeHand(hh, printdata = printtest) - self.dbid_hpid = db.storeHandsPlayers(self.dbid_hands, self.dbid_pids, - self.stats.getHandsPlayers(), printdata = printtest) + db.storeHandsPlayers(self.dbid_hands, self.dbid_pids, self.stats.getHandsPlayers(), + printdata = printtest) if self.saveActions: - db.storeHandsActions(self.dbid_hands, self.dbid_pids, self.dbid_hpid, - self.stats.getHandsActions(), printdata = printtest) + db.storeHandsActions(self.dbid_hands, self.dbid_pids, self.stats.getHandsActions(), + printdata = printtest) else: log.info(_("Hand.insert(): hid #: %s is a duplicate") % hh['siteHandNo']) self.is_duplicate = True # i.e. don't update hudcache diff --git a/pyfpdb/SQL.py b/pyfpdb/SQL.py index 6a87b433..d396b43c 100644 --- a/pyfpdb/SQL.py +++ b/pyfpdb/SQL.py @@ -1007,7 +1007,8 @@ class Sql: if db_server == 'mysql': self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - handsPlayerId BIGINT UNSIGNED NOT NULL, FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id), + handId BIGINT UNSIGNED NOT NULL, FOREIGN KEY (handId) REFERENCES Hands(id), + playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), street SMALLINT NOT NULL, actionNo SMALLINT NOT NULL, streetActionNo SMALLINT NOT NULL, @@ -1022,7 +1023,8 @@ class Sql: elif db_server == 'postgresql': self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( id BIGSERIAL, PRIMARY KEY (id), - handsPlayerId BIGINT, FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id), + handId BIGINT NOT NULL, FOREIGN KEY (handId) REFERENCES Hands(id), + playerId INT NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), street SMALLINT, actionNo SMALLINT, streetActionNo SMALLINT, @@ -1036,7 +1038,8 @@ class Sql: elif db_server == 'sqlite': self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( id INTEGER PRIMARY KEY, - handsPlayerId BIGINT, + handId INT NOT NULL, + playerId INT NOT NULL, street SMALLINT, actionNo SMALLINT, streetActionNo SMALLINT, @@ -1046,9 +1049,7 @@ class Sql: amountCalled INT, numDiscarded SMALLINT, cardsDiscarded TEXT, - allIn BOOLEAN, - FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id), - FOREIGN KEY (actionId) REFERENCES Actions(id) ON DELETE CASCADE + allIn BOOLEAN )""" @@ -4323,7 +4324,7 @@ class Sql: self.query['handsPlayersTTypeId_joiner'] = " OR TourneysPlayersId+0=" self.query['handsPlayersTTypeId_joiner_id'] = " OR id=" - self.query['store_hand'] = """INSERT INTO Hands ( + self.query['store_hand'] = """insert into Hands ( tablename, gametypeid, sitehandno, @@ -4355,13 +4356,13 @@ class Sql: street4Pot, showdownPot ) - VALUES + values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" - self.query['store_hands_players'] = """INSERT INTO HandsPlayers ( + self.query['store_hands_players'] = """insert into HandsPlayers ( handId, playerId, startCash, @@ -4458,7 +4459,7 @@ class Sql: street3Raises, street4Raises ) - VALUES ( + values ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, @@ -4480,8 +4481,9 @@ class Sql: %s, %s, %s, %s, %s )""" - self.query['store_hands_actions'] = """INSERT INTO HandsActions ( - handsPlayerId, + self.query['store_hands_actions'] = """insert into HandsActions ( + handId, + playerId, street, actionNo, streetActionNo, @@ -4493,10 +4495,10 @@ class Sql: cardsDiscarded, allIn ) - VALUES ( + values ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s + %s, %s )""" ################################