Commit ·
ded0d16
1
Parent(s): eef0aea
Performance: fix N+1 queries, share DB conn per request, add gzip compression
Browse files- get_all_employees: 77 queries → 3 (batch load patterns/unavail)
- Flask g: reuse one DB connection per request instead of per-call
- flask-compress: gzip HTML/JSON responses
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- db/database.py +14 -1
- db/repositories.py +52 -1
- requirements-web.txt +1 -0
- web_app.py +10 -1
db/database.py
CHANGED
|
@@ -120,13 +120,26 @@ class Connection:
|
|
| 120 |
def commit(self): self._conn.commit()
|
| 121 |
|
| 122 |
def close(self):
|
|
|
|
|
|
|
| 123 |
if self.backend == "postgres" and self._pool:
|
| 124 |
-
self._pool.putconn(self._conn)
|
| 125 |
else:
|
| 126 |
self._conn.close()
|
| 127 |
|
| 128 |
|
| 129 |
def get_connection() -> Connection:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
return Connection()
|
| 131 |
|
| 132 |
|
|
|
|
| 120 |
def commit(self): self._conn.commit()
|
| 121 |
|
| 122 |
def close(self):
|
| 123 |
+
if getattr(self, "_flask_managed", False):
|
| 124 |
+
return # Flask teardown が責任を持つ
|
| 125 |
if self.backend == "postgres" and self._pool:
|
| 126 |
+
self._pool.putconn(self._conn)
|
| 127 |
else:
|
| 128 |
self._conn.close()
|
| 129 |
|
| 130 |
|
| 131 |
def get_connection() -> Connection:
|
| 132 |
+
"""Flask リクエスト中は接続を g で共有し、プール貸し借りを削減する"""
|
| 133 |
+
try:
|
| 134 |
+
from flask import g, has_request_context
|
| 135 |
+
if has_request_context():
|
| 136 |
+
if not hasattr(g, "_db_conn"):
|
| 137 |
+
conn = Connection()
|
| 138 |
+
conn._flask_managed = True
|
| 139 |
+
g._db_conn = conn
|
| 140 |
+
return g._db_conn
|
| 141 |
+
except (ImportError, RuntimeError):
|
| 142 |
+
pass
|
| 143 |
return Connection()
|
| 144 |
|
| 145 |
|
db/repositories.py
CHANGED
|
@@ -14,7 +14,32 @@ def get_all_employees(active_only: bool = True) -> list[Employee]:
|
|
| 14 |
rows = conn.execute(
|
| 15 |
"SELECT * FROM employees" + (" WHERE is_active=1" if active_only else "") + " ORDER BY id"
|
| 16 |
).fetchall()
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
conn.close()
|
| 19 |
return employees
|
| 20 |
|
|
@@ -70,6 +95,32 @@ def restore_employee(employee_id: int):
|
|
| 70 |
conn.close()
|
| 71 |
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def _row_to_employee(row, conn) -> Employee:
|
| 74 |
patterns = conn.execute(
|
| 75 |
"SELECT * FROM fixed_patterns WHERE employee_id=? ORDER BY day_of_week",
|
|
|
|
| 14 |
rows = conn.execute(
|
| 15 |
"SELECT * FROM employees" + (" WHERE is_active=1" if active_only else "") + " ORDER BY id"
|
| 16 |
).fetchall()
|
| 17 |
+
if not rows:
|
| 18 |
+
conn.close()
|
| 19 |
+
return []
|
| 20 |
+
|
| 21 |
+
emp_ids = [r["id"] for r in rows]
|
| 22 |
+
ph = ",".join(["?" ] * len(emp_ids))
|
| 23 |
+
|
| 24 |
+
# fixed_patterns を一括取得(N+1解消)
|
| 25 |
+
pat_rows = conn.execute(
|
| 26 |
+
f"SELECT * FROM fixed_patterns WHERE employee_id IN ({ph}) ORDER BY employee_id, day_of_week",
|
| 27 |
+
emp_ids
|
| 28 |
+
).fetchall()
|
| 29 |
+
pat_map: dict[int, list] = {}
|
| 30 |
+
for p in pat_rows:
|
| 31 |
+
pat_map.setdefault(p["employee_id"], []).append(p)
|
| 32 |
+
|
| 33 |
+
# fixed_unavailable_dates を一括取得(N+1解消)
|
| 34 |
+
unavail_rows = conn.execute(
|
| 35 |
+
f"SELECT employee_id, date FROM fixed_unavailable_dates WHERE employee_id IN ({ph}) ORDER BY employee_id, date",
|
| 36 |
+
emp_ids
|
| 37 |
+
).fetchall()
|
| 38 |
+
unavail_map: dict[int, list] = {}
|
| 39 |
+
for u in unavail_rows:
|
| 40 |
+
unavail_map.setdefault(u["employee_id"], []).append(u["date"])
|
| 41 |
+
|
| 42 |
+
employees = [_row_to_employee_preloaded(row, pat_map, unavail_map) for row in rows]
|
| 43 |
conn.close()
|
| 44 |
return employees
|
| 45 |
|
|
|
|
| 95 |
conn.close()
|
| 96 |
|
| 97 |
|
| 98 |
+
def _row_to_employee_preloaded(row, pat_map: dict, unavail_map: dict) -> Employee:
|
| 99 |
+
eid = row["id"]
|
| 100 |
+
keys = row.keys()
|
| 101 |
+
pp_val = row["primary_position"] if "primary_position" in keys and row["primary_position"] else None
|
| 102 |
+
pt_val = row["primary_timeslot"] if "primary_timeslot" in keys and row["primary_timeslot"] else None
|
| 103 |
+
return Employee(
|
| 104 |
+
id=eid, name=row["name"],
|
| 105 |
+
employment_type=EmploymentType(row["employment_type"]),
|
| 106 |
+
hall_skill=SkillLevel(row["hall_skill"]),
|
| 107 |
+
kitchen_skill=SkillLevel(row["kitchen_skill"]),
|
| 108 |
+
primary_position=PrimaryPosition(pp_val) if pp_val else None,
|
| 109 |
+
primary_timeslot=TimeSlot(pt_val) if pt_val else None,
|
| 110 |
+
can_work_both_positions=bool(row["can_work_both_positions"]) if "can_work_both_positions" in keys else False,
|
| 111 |
+
can_open=bool(row["can_open"]) if "can_open" in keys else False,
|
| 112 |
+
can_cleanup=bool(row["can_cleanup"]) if "can_cleanup" in keys else False,
|
| 113 |
+
always_available_breakfast=bool(row["always_available_breakfast"]) if "always_available_breakfast" in keys else False,
|
| 114 |
+
always_available_dinner=bool(row["always_available_dinner"]) if "always_available_dinner" in keys else False,
|
| 115 |
+
is_active=bool(row["is_active"]),
|
| 116 |
+
fixed_patterns=[
|
| 117 |
+
FixedPattern(p["day_of_week"], bool(p["breakfast"]), bool(p["dinner"]))
|
| 118 |
+
for p in pat_map.get(eid, [])
|
| 119 |
+
],
|
| 120 |
+
fixed_unavailable_dates=unavail_map.get(eid, []),
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
def _row_to_employee(row, conn) -> Employee:
|
| 125 |
patterns = conn.execute(
|
| 126 |
"SELECT * FROM fixed_patterns WHERE employee_id=? ORDER BY day_of_week",
|
requirements-web.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
flask>=3.0.0
|
|
|
|
| 2 |
gunicorn>=21.0.0
|
| 3 |
psycopg2-binary>=2.9.0
|
| 4 |
ortools>=9.7.0
|
|
|
|
| 1 |
flask>=3.0.0
|
| 2 |
+
flask-compress>=1.14
|
| 3 |
gunicorn>=21.0.0
|
| 4 |
psycopg2-binary>=2.9.0
|
| 5 |
ortools>=9.7.0
|
web_app.py
CHANGED
|
@@ -3,13 +3,15 @@ import sys
|
|
| 3 |
import os
|
| 4 |
sys.path.insert(0, os.path.dirname(__file__))
|
| 5 |
|
| 6 |
-
from flask import Flask
|
|
|
|
| 7 |
from db.database import initialize_db
|
| 8 |
|
| 9 |
|
| 10 |
def create_app():
|
| 11 |
app = Flask(__name__, template_folder="templates", static_folder="static")
|
| 12 |
app.secret_key = os.environ.get("SECRET_KEY", "dev-secret-key-change-in-prod")
|
|
|
|
| 13 |
|
| 14 |
initialize_db()
|
| 15 |
|
|
@@ -29,6 +31,13 @@ def create_app():
|
|
| 29 |
app.register_blueprint(settings_bp)
|
| 30 |
app.register_blueprint(export_bp)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@app.get("/")
|
| 33 |
def index():
|
| 34 |
from flask import redirect, url_for
|
|
|
|
| 3 |
import os
|
| 4 |
sys.path.insert(0, os.path.dirname(__file__))
|
| 5 |
|
| 6 |
+
from flask import Flask, g
|
| 7 |
+
from flask_compress import Compress
|
| 8 |
from db.database import initialize_db
|
| 9 |
|
| 10 |
|
| 11 |
def create_app():
|
| 12 |
app = Flask(__name__, template_folder="templates", static_folder="static")
|
| 13 |
app.secret_key = os.environ.get("SECRET_KEY", "dev-secret-key-change-in-prod")
|
| 14 |
+
Compress(app)
|
| 15 |
|
| 16 |
initialize_db()
|
| 17 |
|
|
|
|
| 31 |
app.register_blueprint(settings_bp)
|
| 32 |
app.register_blueprint(export_bp)
|
| 33 |
|
| 34 |
+
@app.teardown_appcontext
|
| 35 |
+
def close_db(error):
|
| 36 |
+
conn = g.pop("_db_conn", None)
|
| 37 |
+
if conn is not None:
|
| 38 |
+
conn._flask_managed = False
|
| 39 |
+
conn.close()
|
| 40 |
+
|
| 41 |
@app.get("/")
|
| 42 |
def index():
|
| 43 |
from flask import redirect, url_for
|