Python 使用SQLAlchemy和PostgreSQL查询两个表

Python 使用SQLAlchemy和PostgreSQL查询两个表,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我需要帮助改进我的SQLAlchemy查询。我使用Python3.7、SQLAlchemy 1.3.15和PosgresSQL 9.4.3作为数据库。我正在尝试返回特定日期和时间段的约会计数。但是,我的约会和约会时段表是分开的,我必须查询这两个模型/表以获得所需的结果。这是我所拥有的 预约模式 约会表有几列,其中包括约会槽表的外键 class Appointment(ResourceMixin, db.Model): __tablename__ = 'appointments'

我需要帮助改进我的SQLAlchemy查询。我使用Python3.7、SQLAlchemy 1.3.15和PosgresSQL 9.4.3作为数据库。我正在尝试返回特定日期和时间段的约会计数。但是,我的约会和约会时段表是分开的,我必须查询这两个模型/表以获得所需的结果。这是我所拥有的

预约模式

约会表有几列,其中包括约会槽表的外键

class Appointment(ResourceMixin, db.Model): 
    __tablename__ = 'appointments'          

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE'), index=True, nullable=True)
    slot_id = db.Column(db.Integer, db.ForeignKey('appointment_slots.id', onupdate='CASCADE', ondelete='CASCADE'), index=True, nullable=False)
    appointment_date = db.Column(db.DateTime(), nullable=False)
    appointment_type = db.Column(db.String(128), nullable=False, default='general')
预约时段表

“约会时段”表包含约会的时段。该模型由返回到约会表的关系组成

class AppointmentSlot(ResourceMixin, db.Model):                                                   
    __tablename__ = 'appointment_slots'                                                           
    id = db.Column(db.Integer, primary_key=True)                                                                         
    # Relationships.                                                                              
    appointments = db.relationship('Appointment', uselist=False,                                  
                                   backref='appointments', lazy='joined', passive_deletes=True)   
    start_time = db.Column(db.String(5), nullable=False, server_default='08:00')                                                                                             
    end_time = db.Column(db.String(5), nullable=False, server_default='17:00')                                      
SQLAlchemy查询

目前,我正在运行以下SQLAlchemy查询,以获取特定日期和时间段的约会计数

appointment_count = db.session.query(func.count(Appointment.id)).join(AppointmentSlot)\
        .filter(and_(Appointment.appointment_date == date, AppointmentSlot.id == Appointment.id,
                     AppointmentSlot.start_time == time)).scalar()
上面的查询返回正确的结果,这是一个位数的值,但我担心查询没有得到优化。当前查询返回的是
380ms
,但
约会
约会时段
表中只有8条记录。这些表格最终将有上百条记录。我担心的是,即使查询现在正在运行,它最终还是会与记录的增加作斗争


如何改进或优化此查询以提高性能?我正在使用
appointment\u slots
表上的约会关系查看SQLAlchemy子查询,但无法使其工作并确认性能。我想一定有更好的方法来运行这个查询,尤其是使用
约会\u slots
表上的
约会关系
,但我现在很为难。有什么建议吗?

我对查询加载时间的看法不正确。我实际上看到的是380ms的页面加载。我还通过从
约会
模型中删除
插槽id
,并向
约会插槽
模型添加
约会id
外键,来更改模型上的一些字段。以下查询的页面加载

appointment_count = db.session.query(func.count(Appointment.id)).join(AppointmentSlot)\
        .filter(and_(Appointment.appointment_date == date, 
AppointmentSlot.appointment_id == Appointment.id, AppointmentSlot.start_time == time)).scalar()
结果是,<代码>0.4637毫秒

但是,我仍然试图改进查询,并且能够通过使用SQLAlchemy子查询来做到这一点。下面的子查询

subquery = db.session.query(Appointment.id).filter(Appointment.appointment_date == date).subquery()
query = db.session.query(func.count(AppointmentSlot.id))\
        .filter(and_(AppointmentSlot.appointment_id.in_(subquery),
 AppointmentSlot.start_time == time)).scalar()

返回一个加载时间
0.3700ms
,它显示了比使用联接查询更好的性能。

如果启用调试日志记录或在调用
scalar()
之前打印(…)对象,它将显示它正在调用的查询。然后,您可以检查它,以确定是否正在执行优化查询,然后检查哪些列已编制索引或未编制索引,然后修复该问题。您从何处获得时间-数据库本身还是在连接到数据库并运行python脚本的其他计算机上?