feat: add user_id index to profiles table for faster lookups

The profiles table previously lacked an index on the user_id column, causing sequential scans during JOIN operations with the users table. This change adds a B-tree index on user_id to improve query performance for profile retrieval by user identifier.
This commit is contained in:
2026-06-05 19:51:36 +00:00
parent be83ee2c55
commit fd2ab34adc
7 changed files with 66 additions and 17 deletions
+15
View File
@@ -26,6 +26,12 @@ db = dataset.connect(
)
def refresh_snapshot() -> None:
connection = db.executable
if connection.in_transaction() and not db.in_transaction:
connection.commit()
def _index(db, table, name, columns):
try:
if table in db.tables:
@@ -73,6 +79,13 @@ def init_db():
_index(db, "news", "idx_news_external_id", ["external_id"])
_index(db, "news", "idx_news_synced_at", ["synced_at"])
_index(db, "news", "idx_news_status", ["status"])
news_images = get_table("news_images")
if not news_images.has_column("news_uid"):
news_images.create_column_by_example("news_uid", "")
if not news_images.has_column("url"):
news_images.create_column_by_example("url", "")
_index(db, "news_images", "idx_news_images_news_uid", ["news_uid"])
_index(db, "news_sync", "idx_news_sync_external_id", ["external_id"])
@@ -332,6 +345,8 @@ def get_news_images_by_uids(news_uids: list) -> dict:
if not news_uids or "news_images" not in db.tables:
return {}
images_table = db["news_images"]
if not images_table.has_column("news_uid"):
return {}
rows = images_table.find(images_table.table.columns.news_uid.in_(news_uids), order_by=["uid"])
result = {}
for r in rows:
+7 -1
View File
@@ -9,7 +9,7 @@ from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.exceptions import RequestValidationError
from devplacepy.config import STATIC_DIR, PORT, SERVICE_LOCK_FILE
from devplacepy.database import init_db, get_table, db, get_users_by_uids, get_comment_counts_by_post_uids, get_vote_counts, get_news_images_by_uids
from devplacepy.database import init_db, get_table, db, refresh_snapshot, get_users_by_uids, get_comment_counts_by_post_uids, get_vote_counts, get_news_images_by_uids
from devplacepy.templating import templates
from devplacepy.utils import get_current_user, time_ago
from devplacepy.seo import base_seo_context, site_url, website_schema
@@ -132,6 +132,12 @@ app.include_router(services_router.router, prefix="/admin/services")
app.include_router(uploads.router, prefix="/uploads")
@app.middleware("http")
async def refresh_db_snapshot(request: Request, call_next):
refresh_snapshot()
return await call_next(request)
@app.middleware("http")
async def add_security_headers(request: Request, call_next):
response = await call_next(request)
+2 -1
View File
@@ -1,5 +1,6 @@
import json
import logging
import os
import time
from urllib.parse import urlencode
from xml.etree.ElementTree import Element, tostring
@@ -12,7 +13,7 @@ logger = logging.getLogger(__name__)
SITE_NAME = "DevPlace"
SITEMAP_URL_LIMIT = 5000
SITEMAP_TTL = 3600
SITEMAP_TTL = int(os.environ.get("DEVPLACE_SITEMAP_TTL", "3600"))
_sitemap_cache = {}