Skip to content

fix: prefetch video_image when getting a video#596

Open
viadanna wants to merge 1 commit intoopenedx:masterfrom
open-craft:viadanna/image-queries
Open

fix: prefetch video_image when getting a video#596
viadanna wants to merge 1 commit intoopenedx:masterfrom
open-craft:viadanna/image-queries

Conversation

@viadanna
Copy link
Contributor

@viadanna viadanna commented Feb 26, 2026

Description

This pull request updates the way related course and image data are fetched for videos in the edxval/api.py module, optimizing the database query for retrieving a Video by including video image data.

This prevents the number of database queries during video serialization from increasing with the number of CourseVideo objects pointing to the Video object.

Testing instructions

  1. Ensure a devstack or sandbox with a course containing a video referenced in edx-val's CourseVideo.
  2. Ensure at least two CourseVideo objects are pointing to the same Video. Export the course and import to a new one to get another CourseVideo reference.
  3. Enable SQL logging by adding the following settings:
# Enable database logging
LOGGING["formatters"]["db"] = {"format": "[%(name)s] %(filename)s:%(lineno)d - %(message)s"}
LOGGING["loggers"]["django.db.backends"] = {
    "handlers": ["local"],
    "level": "DEBUG",
    "formatter": "db"
}
  1. Open a django shell and try serializing a video
In [2]: from edxval import api

In [3]: api.CourseVideo.objects.last()
Out[3]: <CourseVideo: course-v1:edx+demo+a>

In [4]: api.CourseVideo.objects.last().__dict__
Out[4]: 
{'_state': <django.db.models.base.ModelState at 0x72276ed650d0>,
 'id': 24,
 'course_id': 'course-v1:edx+demo+a',
 'video_id': 31,
 'is_hidden': False}

In [5]: api.Video.objects.get(id=31)
Out[5]: <Video: 88723157-d381-46ff-8f3c-324970fcea3e>

In [6]: api.get_video_info("88723157-d381-46ff-8f3c-324970fcea3e")

Out[6]: {'encoded_videos': [OrderedDict([('created', datetime.datetime(2026, 2, 19, 19, 5, 59, 251724, tzinfo=datetime.timezone.utc)), ('modified', datetime.datetime(2026, 2, 19, 19, 5, 59, 251820, tzinfo=datetime.timezone.utc)), ('url', 'https://example.com/file.mp4'), ('file_size', 32619768), ('bitrate', 261), ('profile', 'mobile_low')]), OrderedDict([('created', datetime.datetime(2026, 2, 19, 19, 5, 59, 251934, tzinfo=datetime.timezone.utc)), ('modified', datetime.datetime(2026, 2, 19, 19, 5, 59, 252129, tzinfo=datetime.timezone.utc)), ('url', 'https://d2f1egay8yehza.cloudfront.net/ASUASM24/ASUASM24T315-V005300_MB1.mp4'), ('file_size', 61681723), ('bitrate', 493), ('profile', 'mobile_high')]), OrderedDict([('created', datetime.datetime(2026, 2, 19, 19, 5, 59, 252219, tzinfo=datetime.timezone.utc)), ('modified', datetime.datetime(2026, 2, 19, 19, 5, 59, 252256, tzinfo=datetime.timezone.utc)), ('url', 'https://example.com/file.mp4'), ('file_size', 123890767), ('bitrate', 991), ('profile', 'desktop_mp4')]), OrderedDict([('created', datetime.datetime(2026, 2, 19, 19, 5, 59, 252341, tzinfo=datetime.timezone.utc)), ('modified', datetime.datetime(2026, 2, 19, 19, 5, 59, 252374, tzinfo=datetime.timezone.utc)), ('url', 'hpZW8xlVbBE'), ('file_size', 0), ('bitrate', 0), ('profile', 'youtube')])], 'courses': [{'course-v1:edx+demo+paulo': None}, {'course-v1:edx+demo+a': None}], 'url': '/api/val/v0/videos/88723157-d381-46ff-8f3c-324970fcea3e', 'created': datetime.datetime(2026, 2, 19, 19, 5, 59, 232659, tzinfo=datetime.timezone.utc), 'edx_video_id': '88723157-d381-46ff-8f3c-324970fcea3e', 'client_video_id': 'file.mp4', 'duration': 999.53, 'status': 'imported', 'error_description': None}
  1. Verify that multiple queries are made for the video image:
2026-02-26 14:57:54,376 DEBUG 72 [django.db.backends] utils.py:131 - (0.001) SELECT `edxval_video`.`id`, `edxval_video`.`created`, `edxval_video`.`edx_video_id`, `edxval_video`.`client_video_id`, `edxval_video`.`duration`, `edxval_video`.`status`, `edxval_video`.`error_description` FROM `edxval_video` WHERE `edxval_video`.`edx_video_id` = '88723157-d381-46ff-8f3c-324970fcea3e' LIMIT 21; args=('88723157-d381-46ff-8f3c-324970fcea3e',); alias=default
2026-02-26 14:57:54,378 DEBUG 72 [django.db.backends] utils.py:131 - (0.001) SELECT `edxval_encodedvideo`.`id`, `edxval_encodedvideo`.`created`, `edxval_encodedvideo`.`modified`, `edxval_encodedvideo`.`url`, `edxval_encodedvideo`.`file_size`, `edxval_encodedvideo`.`bitrate`, `edxval_encodedvideo`.`profile_id`, `edxval_encodedvideo`.`video_id`, `edxval_profile`.`id`, `edxval_profile`.`profile_name` FROM `edxval_encodedvideo` INNER JOIN `edxval_profile` ON (`edxval_encodedvideo`.`profile_id` = `edxval_profile`.`id`) WHERE `edxval_encodedvideo`.`video_id` IN (31); args=(31,); alias=default
2026-02-26 14:57:54,380 DEBUG 72 [django.db.backends] utils.py:131 - (0.001) SELECT `edxval_coursevideo`.`id`, `edxval_coursevideo`.`course_id`, `edxval_coursevideo`.`video_id`, `edxval_coursevideo`.`is_hidden` FROM `edxval_coursevideo` WHERE `edxval_coursevideo`.`video_id` IN (31); args=(31,); alias=default
2026-02-26 14:57:54,385 DEBUG 72 [django.db.backends] utils.py:131 - (0.001) SELECT `edxval_videoimage`.`id`, `edxval_videoimage`.`created`, `edxval_videoimage`.`modified`, `edxval_videoimage`.`course_video_id`, `edxval_videoimage`.`image`, `edxval_videoimage`.`generated_images` FROM `edxval_videoimage` WHERE `edxval_videoimage`.`course_video_id` = 12 LIMIT 21; args=(12,); alias=default
2026-02-26 14:57:54,388 DEBUG 72 [django.db.backends] utils.py:131 - (0.001) SELECT `edxval_videoimage`.`id`, `edxval_videoimage`.`created`, `edxval_videoimage`.`modified`, `edxval_videoimage`.`course_video_id`, `edxval_videoimage`.`image`, `edxval_videoimage`.`generated_images` FROM `edxval_videoimage` WHERE `edxval_videoimage`.`course_video_id` = 24 LIMIT 21; args=(24,); alias=default
  1. Now deploy the changes from this PR, repeat the last two steps and verify the videoimage queries changed to a single outer join.
2026-02-26 15:04:09,333 DEBUG 184 [django.db.backends] utils.py:131 - (0.002) SELECT `edxval_coursevideo`.`id`, `edxval_coursevideo`.`course_id`, `edxval_coursevideo`.`video_id`, `edxval_coursevideo`.`is_hidden`, `edxval_videoimage`.`id`, `edxval_videoimage`.`created`, `edxval_videoimage`.`modified`, `edxval_videoimage`.`course_video_id`, `edxval_videoimage`.`image`, `edxval_videoimage`.`generated_images` FROM `edxval_coursevideo` LEFT OUTER JOIN `edxval_videoimage` ON (`edxval_coursevideo`.`id` = `edxval_videoimage`.`course_video_id`) WHERE `edxval_coursevideo`.`video_id` IN (31); args=(31,); alias=default

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Feb 26, 2026
@openedx-webhooks
Copy link

Thanks for the pull request, @viadanna!

This repository is currently maintained by @openedx/2u-aurora.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@github-project-automation github-project-automation bot moved this to Needs Triage in Contributions Feb 26, 2026
@viadanna viadanna self-assigned this Feb 26, 2026
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Waiting on Author in Contributions Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Waiting on Author

Development

Successfully merging this pull request may close these issues.

2 participants