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.

This commit is contained in:
chaz@pokeit.co 2010-12-31 15:30:29 -05:00
parent 4285237a48
commit 838c626bba
3 changed files with 33 additions and 36 deletions

View File

@ -73,7 +73,7 @@ except ImportError:
use_numpy = False use_numpy = False
DB_VERSION = 147 DB_VERSION = 148
# Variance created as sqlite has a bunch of undefined aggregate functions. # Variance created as sqlite has a bunch of undefined aggregate functions.
@ -125,7 +125,8 @@ class Database:
{'tab':'Gametypes', 'col':'siteId', 'drop':0} {'tab':'Gametypes', 'col':'siteId', 'drop':0}
, {'tab':'Hands', 'col':'gametypeId', 'drop':0} # mct 22/3/09 , {'tab':'Hands', 'col':'gametypeId', 'drop':0} # mct 22/3/09
#, {'tab':'Hands', 'col':'siteHandNo', 'drop':0} unique indexes not dropped #, {'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':'HandsActions', 'col':'actionId', 'drop':1}
, {'tab':'HandsPlayers', 'col':'handId', 'drop':1} , {'tab':'HandsPlayers', 'col':'handId', 'drop':1}
, {'tab':'HandsPlayers', 'col':'playerId', 'drop':1} , {'tab':'HandsPlayers', 'col':'playerId', 'drop':1}
@ -150,7 +151,8 @@ class Database:
, {'tab':'HandsPlayers', 'col':'handId', 'drop':0} , {'tab':'HandsPlayers', 'col':'handId', 'drop':0}
, {'tab':'HandsPlayers', 'col':'playerId', 'drop':0} , {'tab':'HandsPlayers', 'col':'playerId', 'drop':0}
, {'tab':'HandsPlayers', 'col':'tourneysPlayersId', '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':'HandsActions', 'col':'actionId', 'drop':1}
, {'tab':'HudCache', 'col':'gametypeId', 'drop':1} , {'tab':'HudCache', 'col':'gametypeId', 'drop':1}
, {'tab':'HudCache', 'col':'playerId', 'drop':0} , {'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':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1}
, {'fktab':'HandsPlayers', 'fkcol':'playerId', 'rtab':'Players', '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':'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':'HandsActions', 'fkcol':'actionId', 'rtab':'Actions', 'rcol':'id', 'drop':1}
, {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1}
, {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} , {'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':'Hands', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1}
, {'fktab':'HandsPlayers', 'fkcol':'handId', 'rtab':'Hands', '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':'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':'HandsActions', 'fkcol':'actionId', 'rtab':'Actions', 'rcol':'id', 'drop':1}
, {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1}
, {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} , {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0}
@ -1755,7 +1759,6 @@ class Database:
pp.pprint(pdata) pp.pprint(pdata)
inserts = [] inserts = []
hpid = {}
for p in pdata: for p in pdata:
inserts.append( (hid, inserts.append( (hid,
pids[p], pids[p],
@ -1860,17 +1863,9 @@ class Database:
#print "DEBUG: inserts: %s" %inserts #print "DEBUG: inserts: %s" %inserts
#print "DEBUG: q: %s" % q #print "DEBUG: q: %s" % q
c = self.get_cursor() c = self.get_cursor()
c.executemany(q, inserts)
if self.import_options['saveActions']: def storeHandsActions(self, hid, pids, adata, printdata = False):
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):
#print "DEBUG: %s %s %s" %(hid, pids, adata) #print "DEBUG: %s %s %s" %(hid, pids, adata)
# This can be used to generate test data. Currently unused # This can be used to generate test data. Currently unused
@ -1881,8 +1876,8 @@ class Database:
inserts = [] inserts = []
for a in adata: for a in adata:
inserts.append( (hpid[(hid, pids[adata[a]['player']])], inserts.append( (hid,
#self.getHandsPlayerId(self.hid, pids[adata[a]['player']]), pids[adata[a]['player']],
adata[a]['street'], adata[a]['street'],
adata[a]['actionNo'], adata[a]['actionNo'],
adata[a]['streetActionNo'], adata[a]['streetActionNo'],

View File

@ -266,11 +266,11 @@ db: a connected Database object"""
hh['seats'] = len(self.dbid_pids) hh['seats'] = len(self.dbid_pids)
self.dbid_hands = db.storeHand(hh, printdata = printtest) self.dbid_hands = db.storeHand(hh, printdata = printtest)
self.dbid_hpid = db.storeHandsPlayers(self.dbid_hands, self.dbid_pids, db.storeHandsPlayers(self.dbid_hands, self.dbid_pids, self.stats.getHandsPlayers(),
self.stats.getHandsPlayers(), printdata = printtest) printdata = printtest)
if self.saveActions: if self.saveActions:
db.storeHandsActions(self.dbid_hands, self.dbid_pids, self.dbid_hpid, db.storeHandsActions(self.dbid_hands, self.dbid_pids, self.stats.getHandsActions(),
self.stats.getHandsActions(), printdata = printtest) printdata = printtest)
else: else:
log.info(_("Hand.insert(): hid #: %s is a duplicate") % hh['siteHandNo']) log.info(_("Hand.insert(): hid #: %s is a duplicate") % hh['siteHandNo'])
self.is_duplicate = True # i.e. don't update hudcache self.is_duplicate = True # i.e. don't update hudcache

View File

@ -1007,7 +1007,8 @@ class Sql:
if db_server == 'mysql': if db_server == 'mysql':
self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions (
id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), 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, street SMALLINT NOT NULL,
actionNo SMALLINT NOT NULL, actionNo SMALLINT NOT NULL,
streetActionNo SMALLINT NOT NULL, streetActionNo SMALLINT NOT NULL,
@ -1022,7 +1023,8 @@ class Sql:
elif db_server == 'postgresql': elif db_server == 'postgresql':
self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions (
id BIGSERIAL, PRIMARY KEY (id), 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, street SMALLINT,
actionNo SMALLINT, actionNo SMALLINT,
streetActionNo SMALLINT, streetActionNo SMALLINT,
@ -1036,7 +1038,8 @@ class Sql:
elif db_server == 'sqlite': elif db_server == 'sqlite':
self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
handsPlayerId BIGINT, handId INT NOT NULL,
playerId INT NOT NULL,
street SMALLINT, street SMALLINT,
actionNo SMALLINT, actionNo SMALLINT,
streetActionNo SMALLINT, streetActionNo SMALLINT,
@ -1046,9 +1049,7 @@ class Sql:
amountCalled INT, amountCalled INT,
numDiscarded SMALLINT, numDiscarded SMALLINT,
cardsDiscarded TEXT, cardsDiscarded TEXT,
allIn BOOLEAN, allIn BOOLEAN
FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id),
FOREIGN KEY (actionId) REFERENCES Actions(id) ON DELETE CASCADE
)""" )"""
@ -4323,7 +4324,7 @@ class Sql:
self.query['handsPlayersTTypeId_joiner'] = " OR TourneysPlayersId+0=" self.query['handsPlayersTTypeId_joiner'] = " OR TourneysPlayersId+0="
self.query['handsPlayersTTypeId_joiner_id'] = " OR id=" self.query['handsPlayersTTypeId_joiner_id'] = " OR id="
self.query['store_hand'] = """INSERT INTO Hands ( self.query['store_hand'] = """insert into Hands (
tablename, tablename,
gametypeid, gametypeid,
sitehandno, sitehandno,
@ -4355,13 +4356,13 @@ class Sql:
street4Pot, street4Pot,
showdownPot 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, %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, handId,
playerId, playerId,
startCash, startCash,
@ -4458,7 +4459,7 @@ class Sql:
street3Raises, street3Raises,
street4Raises street4Raises
) )
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,
@ -4480,8 +4481,9 @@ class Sql:
%s, %s, %s, %s, %s %s, %s, %s, %s, %s
)""" )"""
self.query['store_hands_actions'] = """INSERT INTO HandsActions ( self.query['store_hands_actions'] = """insert into HandsActions (
handsPlayerId, handId,
playerId,
street, street,
actionNo, actionNo,
streetActionNo, streetActionNo,
@ -4493,10 +4495,10 @@ class Sql:
cardsDiscarded, cardsDiscarded,
allIn allIn
) )
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
)""" )"""
################################ ################################