Skip to content

Conversation

bsekachev
Copy link
Member

@bsekachev bsekachev commented Jul 7, 2025

Motivation and context

Current database schema leads to inefficient query planning on postgres side.
There are some half-working workarounds, however it seems the most reliable solution is to add new ForeignKeys(Job) to all attribute tables.

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

@bsekachev bsekachev requested a review from SpecLad as a code owner July 7, 2025 09:37
@bsekachev bsekachev requested a review from zhiltsov-max as a code owner July 7, 2025 09:51
@zhiltsov-max
Copy link
Contributor

zhiltsov-max commented Jul 7, 2025

To update test db, you can start the test containers without the new migration (pytest --rebuid --start-services tests/python), then return the migration file, run the migration (python manage.py migrate), then dump the test db (docker exec test_cvat_server_1 python manage.py dumpdata --indent 2 --natural-foreign --exclude=admin --exclude=auth.permission --exclude=authtoken --exclude=contenttypes --exclude=django_rq --exclude=sessions --traceback > tests/python/shared/assets/cvat_db/data.json).

@bsekachev bsekachev requested a review from nmanovic as a code owner July 7, 2025 11:53
@bsekachev bsekachev changed the title Added extra Job indexes to all AttributeVal models Added ForeignKey(Job) to all AttributeVal models Jul 7, 2025
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 71.86%. Comparing base (7485d1a) to head (ae8fbbb).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9612      +/-   ##
===========================================
+ Coverage    71.76%   71.86%   +0.09%     
===========================================
  Files          441      441              
  Lines        46286    46365      +79     
  Branches      3949     3960      +11     
===========================================
+ Hits         33218    33319     +101     
+ Misses       13068    13046      -22     
Components Coverage Δ
cvat-ui 77.70% <68.35%> (-0.04%) ⬇️
cvat-server 67.22% <90.00%> (+0.18%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bsekachev
Copy link
Member Author

bsekachev commented Jul 7, 2025

FYI: migration queries

BEGIN;
--
-- Add field job to labeledimageattributeval
--
ALTER TABLE "engine_labeledimageattributeval" ADD COLUMN "job_id" integer NULL CONSTRAINT "engine_labeledimagea_job_id_24236328_fk_engine_jo" REFERENCES "engine_job"("id") DEFERRABLE INITIALLY DEFERRED; SET CONSTRAINTS "engine_labeledimagea_job_id_24236328_fk_engine_jo" IMMEDIATE;
--
-- Add field job to labeledshapeattributeval
--
ALTER TABLE "engine_labeledshapeattributeval" ADD COLUMN "job_id" integer NULL CONSTRAINT "engine_labeledshapea_job_id_76b20fd6_fk_engine_jo" REFERENCES "engine_job"("id") DEFERRABLE INITIALLY DEFERRED; SET CONSTRAINTS "engine_labeledshapea_job_id_76b20fd6_fk_engine_jo" IMMEDIATE;
--
-- Add field job to labeledtrackattributeval
--
ALTER TABLE "engine_labeledtrackattributeval" ADD COLUMN "job_id" integer NULL CONSTRAINT "engine_labeledtracka_job_id_d7147e99_fk_engine_jo" REFERENCES "engine_job"("id") DEFERRABLE INITIALLY DEFERRED; SET CONSTRAINTS "engine_labeledtracka_job_id_d7147e99_fk_engine_jo" IMMEDIATE;
--
-- Add field job to trackedshapeattributeval
--
ALTER TABLE "engine_trackedshapeattributeval" ADD COLUMN "job_id" integer NULL CONSTRAINT "engine_trackedshapea_job_id_3e34bfad_fk_engine_jo" REFERENCES "engine_job"("id") DEFERRABLE INITIALLY DEFERRED; SET CONSTRAINTS "engine_trackedshapea_job_id_3e34bfad_fk_engine_jo" IMMEDIATE;
--
-- Raw Python operation
--


[{'sql': 'UPDATE "engine_labeledimageattributeval" SET "job_id" = (SELECT '
         'U0."job_id" FROM "engine_labeledimage" U0 WHERE U0."id" = '
         '("engine_labeledimageattributeval"."image_id") LIMIT 1)',
  'time': '0.003'},
 {'sql': 'UPDATE "engine_labeledshapeattributeval" SET "job_id" = (SELECT '
         'U0."job_id" FROM "engine_labeledshape" U0 WHERE U0."id" = '
         '("engine_labeledshapeattributeval"."shape_id") LIMIT 1)',
  'time': '7.053'},
 {'sql': 'UPDATE "engine_labeledtrackattributeval" SET "job_id" = (SELECT '
         'U0."job_id" FROM "engine_labeledtrack" U0 WHERE U0."id" = '
         '("engine_labeledtrackattributeval"."track_id") LIMIT 1)',
  'time': '0.002'},
 {'sql': 'UPDATE "engine_trackedshapeattributeval" SET "job_id" = (SELECT '
         'U1."job_id" FROM "engine_trackedshape" U0 INNER JOIN '
         '"engine_labeledtrack" U1 ON (U0."track_id" = U1."id") WHERE U0."id" '
         '= ("engine_trackedshapeattributeval"."shape_id") LIMIT 1)',
  'time': '0.002'}]




--
-- Alter field job on labeledimageattributeval
--
SET CONSTRAINTS "engine_labeledimagea_job_id_24236328_fk_engine_jo" IMMEDIATE; ALTER TABLE "engine_labeledimageattributeval" DROP CONSTRAINT "engine_labeledimagea_job_id_24236328_fk_engine_jo";
ALTER TABLE "engine_labeledimageattributeval" ALTER COLUMN "job_id" SET NOT NULL;
ALTER TABLE "engine_labeledimageattributeval" ADD CONSTRAINT "engine_labeledimagea_job_id_24236328_fk_engine_jo" FOREIGN KEY ("job_id") REFERENCES "engine_job" ("id") DEFERRABLE INITIALLY DEFERRED;
--
-- Alter field job on labeledshapeattributeval
--
SET CONSTRAINTS "engine_labeledshapea_job_id_76b20fd6_fk_engine_jo" IMMEDIATE; ALTER TABLE "engine_labeledshapeattributeval" DROP CONSTRAINT "engine_labeledshapea_job_id_76b20fd6_fk_engine_jo";
ALTER TABLE "engine_labeledshapeattributeval" ALTER COLUMN "job_id" SET NOT NULL;
ALTER TABLE "engine_labeledshapeattributeval" ADD CONSTRAINT "engine_labeledshapea_job_id_76b20fd6_fk_engine_jo" FOREIGN KEY ("job_id") REFERENCES "engine_job" ("id") DEFERRABLE INITIALLY DEFERRED;
--
-- Alter field job on labeledtrackattributeval
--
SET CONSTRAINTS "engine_labeledtracka_job_id_d7147e99_fk_engine_jo" IMMEDIATE; ALTER TABLE "engine_labeledtrackattributeval" DROP CONSTRAINT "engine_labeledtracka_job_id_d7147e99_fk_engine_jo";
ALTER TABLE "engine_labeledtrackattributeval" ALTER COLUMN "job_id" SET NOT NULL;
ALTER TABLE "engine_labeledtrackattributeval" ADD CONSTRAINT "engine_labeledtracka_job_id_d7147e99_fk_engine_jo" FOREIGN KEY ("job_id") REFERENCES "engine_job" ("id") DEFERRABLE INITIALLY DEFERRED;
--
-- Alter field job on trackedshapeattributeval
--
SET CONSTRAINTS "engine_trackedshapea_job_id_3e34bfad_fk_engine_jo" IMMEDIATE; ALTER TABLE "engine_trackedshapeattributeval" DROP CONSTRAINT "engine_trackedshapea_job_id_3e34bfad_fk_engine_jo";
ALTER TABLE "engine_trackedshapeattributeval" ALTER COLUMN "job_id" SET NOT NULL;
ALTER TABLE "engine_trackedshapeattributeval" ADD CONSTRAINT "engine_trackedshapea_job_id_3e34bfad_fk_engine_jo" FOREIGN KEY ("job_id") REFERENCES "engine_job" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "engine_labeledimageattributeval_job_id_24236328" ON "engine_labeledimageattributeval" ("job_id");
CREATE INDEX "engine_labeledshapeattributeval_job_id_76b20fd6" ON "engine_labeledshapeattributeval" ("job_id");
CREATE INDEX "engine_labeledtrackattributeval_job_id_d7147e99" ON "engine_labeledtrackattributeval" ("job_id");
CREATE INDEX "engine_trackedshapeattributeval_job_id_3e34bfad" ON "engine_trackedshapeattributeval" ("job_id");
COMMIT;

@bsekachev bsekachev changed the title Added ForeignKey(Job) to all AttributeVal models [do not merge] Added ForeignKey(Job) to all AttributeVal models Jul 7, 2025
@cvat-bot cvat-bot bot mentioned this pull request Jul 7, 2025
Copy link

sonarqubecloud bot commented Jul 7, 2025

@azhavoro azhavoro merged commit 89f1a28 into develop Jul 7, 2025
15 checks passed
@azhavoro azhavoro deleted the bs/added_index branch July 7, 2025 20:46
@azhavoro azhavoro changed the title [do not merge] Added ForeignKey(Job) to all AttributeVal models Added ForeignKey(Job) to all AttributeVal models Jul 7, 2025
@opencv-alalek
Copy link

BTW, avoid "merge" commits in PR to avoid empty diff on GitHub: https://github.com/cvat-ai/cvat/pull/9612/files
Prefer using "git rebase" (or "git pull --rebase") instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants