craftcms / craftcms/cms

[5.x]: Missing index on the priority attribute in the 'queue' table, causing slow performance in job retrieval (15+ seconds).

Open
#16,202 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

What happened?
Summary

This issue was reported to me by a colleague last week. We are working on a Craft Commerce website that requires heavy use of small, incremental processing. During certain processes, especially when external synchronizations are running overnight, we can end up with 10,000 to 30,000 queue jobs being created.

The 'queue' table in the database lacks an index on the priority attribute, which causes performance issues when retrieving the next job. Specifically, querying the next job was taking over 15 seconds because MySQL had to load and sort all results in memory based on CPU power. After adding an index on the priority column manually, the query time was reduced to around 0.005 seconds, which resolved the issue.

Details
  • Problem: Missing index on the priority attribute in the 'queue' table, causing slow performance in job retrieval (15+ seconds).

  • Impact: In a system with high volumes of queue jobs (10-30k jobs overnight), this slow performance significantly affects cron job processing and overall system efficiency.

  • Solution: Manually added an index to the priority column, resulting in a drastic performance improvement (query now takes ~0.005 seconds).

  • Observation: We have checked some other Craft based projects, and neither Craft 4 nor Craft 5 projects had this index, suggesting a potential design flaw in Craft.

Suggestion

Recommend adding an index to the priority column in the 'queue' table by default to improve performance in high-volume job-processing scenarios.

Craft CMS version

5.4.9

PHP version

8.2.25

Operating system and version

Debian 5.10.223-1 (2024-08-10) x86_64 GNU/Linux

Database type and version

MySQL 8.0.30

Image driver and version

No response

Installed plugins and versions
"algolia/algoliasearch-client-php": "^3.4",
"craftcms/ckeditor": "4.2.0",
"craftcms/cms": "5.4.9",
"craftcms/commerce": "5.1.1",
"mmikkel/incognito-field": "2.0.0",
"nystudio107/craft-vite": "5.0.1",
"presseddigital/linkit": "5.0.0",
"putyourlightson/craft-sendgrid": "3.0.0",
"setasign/fpdi": "^2.6",
"studioespresso/craft-scout": "5.0.1",
"tecnickcom/tcpdf": "^6.7",
"verbb/formie": "3.0.7",
"verbb/navigation": "3.0.4",
"verbb/wishlist": "3.0.3",
"vlucas/phpdotenv": "^5.4.0",
"yiisoft/yii2-redis": "^2.0"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by locating the queue table definition and the job-retrieval query, then examine how database indexes are added; done means the default schema includes the priority index and high-volume MySQL job retrieval no longer performs the reported slow sort.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
backend, databases, performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.