簡體   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