我有4个表,分别称为文件元数据,播放器排名,统计数据功能和演示功能。一个文件元数据可以具有多个播放器等级(每个播放器等级代表一个播放器),并且一个播放器等级可以具有统计功能或演示功能(如果is_demo为true,则两者都可以)。我需要有效地将播放器状态(即统计功能或演示功能)(如果文件元数据中的is_demo标志为true,请获取演示功能,否则为统计功能)读取到熊猫数据框。
现在是我的查询:
session = self.session()
query = session.query(self.db_model.file_metadata)
query = query.options(
joinedload(self.db_model.file_metadata.player_rank)
.joinedload(self.db_model.player_rank.stat_features))\
.options(joinedload(self.db_model.file_metadata.player_rank)
.joinedload(self.db_model.player_rank.demo_features))
query = query.filter(case(
[
(self.db_model.file_metadata.is_demo is True,
self.db_model.player_rank.demo_features),
],
else_=self.db_model.player_rank.stat_features
))
chunks = pd.read_sql(query.statement, con=query.session.bind,
chunksize=100)
df = pd.DataFrame()
for chunk in tqdm(chunks):
df = pd.concat([df, chunk])
session.close()
数据库设计:
class file_metadata(base):
__tablename__ = 'file_metadata'
unique_identifier = Column(BigInteger, primary_key=True)
filename = Column(String, unique=True)
date = Column(DateTime)
is_demo = Column(Boolean)
version = Column(String) # version of analyzer
server_type = Column(String)
tie_situation = Column(Boolean)
player_ranks = relationship("player_rank", lazy='subquery',
back_populates="file_metadata", uselist=True,
cascade="delete")
class player_rank(base):
__tablename__ = 'player_rank'
# id = Column(Integer, primary_key=True)
Date = Column(BigInteger, primary_key=True)
Name = Column(String, primary_key=True)
mu = Column(Float(precision=3))
sigma = Column(Float(precision=3))
Won = Column(Integer)
file_metadata_id = Column(BigInteger, ForeignKey('file_metadata.unique_identifier'))
file_metadata = relationship("file_metadata", lazy='subquery',
back_populates="player_ranks", uselist=False)
stat_features = relationship("stat_features", lazy='subquery', uselist=False,
back_populates="player_ranking")
demo_features = relationship("demo_features", lazy='subquery',
uselist=False, back_populates="player_ranking")
class stat_features(base, CommonEqualityMixin):
__tablename__ = 'stat_features'
id = Column(Integer, primary_key=True)
Name = Column(String)
Date = Column(BigInteger)
Pistol_Rounds_Won_Percentage = Column(Float(precision=3))
HS_Percentage = Column(Float(precision=3))
Clutches_Won = Column(Integer)
ADR = Column(Float(precision=3))
FPR = Column(Float(precision=3))
APR = Column(Float(precision=3))
K_D_Diff_Round = Column(Float(precision=3))
Flash_Assists_Round = Column(Float(precision=3))
Blind_Players_Killed_Round = Column(Float(precision=3))
Blind_Kills = Column(Float(precision=3))
Grenade_Damage_Round = Column(Float(precision=3))
Fire_Damage_Round = Column(Float(precision=3))
Time_Flashing_Opponents_Round = Column(Float(precision=3))
Accuracy = Column(Float(precision=3))
Num_Times_Trader = Column(Float(precision=3))
Num_Times_Tradee = Column(Float(precision=3))
KAST = Column(Float(precision=3))
Ratings = Column(Float(precision=3), default=-1)
Won = Column(Integer)
player_ranking = relationship("player_rank", foreign_keys=[Name, Date], lazy='subquery',
uselist=False, back_populates="stat_features")
__table_args__ = (ForeignKeyConstraint([Name, Date], [player_rank.Name, player_rank.Date]),)
class demo_features(base, CommonEqualityMixin):
__tablename__ = 'demo_features'
id = Column(Integer, primary_key=True)
Name = Column(String)
Date = Column(BigInteger)
Pistol_Rounds_Won_Percentage = Column(Float(precision=3))
HS_Percentage = Column(Float(precision=3))
Clutches_Won = Column(Integer)
ADR = Column(Float(precision=3))
FPR = Column(Float(precision=3))
APR = Column(Float(precision=3))
K_D_Diff_Round = Column(Float(precision=3))
Flash_Assists_Round = Column(Float(precision=3))
Blind_Players_Killed_Round = Column(Float(precision=3))
Blind_Kills = Column(Float(precision=3))
Grenade_Damage_Round = Column(Float(precision=3))
Fire_Damage_Round = Column(Float(precision=3))
Time_Flashing_Opponents_Round = Column(Float(precision=3))
Accuracy = Column(Float(precision=3))
Num_Times_Trader = Column(Float(precision=3))
Num_Times_Tradee = Column(Float(precision=3))
KAST = Column(Float(precision=3))
Ratings = Column(Float(precision=3), default=-1)
Won = Column(Integer)
player_ranking = relationship("player_rank", foreign_keys=[Name, Date], lazy='subquery',
uselist=False, back_populates="demo_features")
__table_args__ = (ForeignKeyConstraint([Name, Date], [player_rank.Name, player_rank.Date]),)