PictoPy icon indicating copy to clipboard operation
PictoPy copied to clipboard

perf: avoid full table scan in toggle-favourite endpoint

Open ajithhraj opened this issue 8 months ago • 2 comments

What happened?

In backend/app/routes/images.py, the /toggle-favourite endpoint fetches all images from the database just to retrieve the updated favourite status of a single image:

image = next(
    (img for img in db_get_all_images() if img["id"] == image_id), None
)

This is a full table scan on every toggle action. If a user has thousands of images, this is extremely slow and wasteful.

Why this matters

  • Poor performance at scale
  • Unnecessary database load on every favourite toggle
  • db_get_all_images() is an expensive query with JOINs

Proposed fix

Add a db_get_image_by_id(image_id) function to database/images.py and use it instead of scanning all images.

Expected behavior

The endpoint should fetch only the single toggled image using its ID.

I'd like to fix this if maintainers agree.

ajithhraj avatar Mar 15 '26 07:03 ajithhraj

šŸ‘‹ Thanks for opening this issue! The maintainers will review it shortly. Till then, do not open any PR. This is a strict rule we like to follow around here :)

github-actions[bot] avatar Mar 15 '26 07:03 github-actions[bot]

Hello šŸ‘‹ I am interested in working on this issue. I have basic knowledge of Python and I’m eager to learn and contribute. I will try to provide a clean and well-documented solution. Please assign this issue to me if possible. Thank you 😊

JITENDRAGAHERWAR03 avatar Mar 30 '26 03:03 JITENDRAGAHERWAR03