繁体   English   中英

使用 SQLAlchemy 和 PostgreSQL 查询两个表

[英]Querying two tables using SQLAlchemy and PostgreSQL

我需要帮助改进我的 SQLAlchemy 查询。 我正在使用 Python 3.7、SQLAlchemy 1.3.15 和 PosgresSQL 9.4.3 作为数据库。 我正在尝试返回特定日期和时间段的约会计数。 但是,我的约会和约会空档表是分开的,我必须同时查询模型/表才能获得所需的结果。 这就是我所拥有的;

约会 Model

约会表有几列,其中包括约会时隙表的外键。

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')

预约时段表

约会时隙表包含约会的时隙。 Model 包含与约会表的关系。

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内返回,但在appointmentsappointment_slots槽表中只有 8 条记录。 这些表最终将拥有成百上千条记录。 我担心即使查询现在正在工作,但它最终会因记录的增加而苦苦挣扎。

如何改进或优化此查询以提高性能? 我正在查看 SQLAlchemy 子查询,使用约会插槽表上的appointment_slots关系,但无法让它工作并确认性能。 我认为必须有更好的方法来运行此查询,特别是使用约会_slots 表上的appointment_slots appointments relationship ,但我目前很难过。 有什么建议么?

我对查询加载时间不正确。 我实际上正在查看 380 毫秒的页面加载。 我还通过从appointments model 中删除slot_id并将appointment_id ID 外键添加到appointment_slots model 中来更改模型上的某些字段。 以下查询的页面加载;

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.4637ms

但是,我仍然尝试改进查询,并且能够通过使用 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的加载时间,这显示出比使用连接查询更好的性能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM