Improve performance of commerce_license_exists()

Created on 13 September 2018, almost 7 years ago
Updated 19 August 2025, 4 days ago

Problem/Motivation

In our experience, any queries that rely on a countQuery (or in EFQ's case, count()) on a large database table results in longer-than-expected query execution time. On production, the EFQ described by commerce_license_exists() actually took 1,080 ms (over 1 second) to check for some products on a user:

This issue is described on a few pages I've found where it is explained how COUNT queries on large InnoDB tables are expensive: 1, 2, and 3.

Our {commerce_license} table has 6.7 million rows, and increasing daily. This means we need to squeeze as much throughput as we can from expensive queries and functions such as this.

In addition, the function itself stores values in the static cache. However, if the statically cached result is 0, the query is called again in all subsequent calls during the same request because of a check for empty(), thereby negating the performance benefit of a static cache.

Proposed resolution

Remove the call to count() and replace it with both a ranged query, and assign a boolean to the results array. Also, check for whether a result for this query !isset() before performing the same query a second time for the same product_id.

Remaining tasks

Review and test.

User interface changes

None.

API changes

None. While the function itself was previously (in its current form) returning and storing the results of count(), the function's comments note that the expected return value of commerce_license_exists() is a Boolean.

Data model changes

None, unless modules are erroneously using the actual count value of returned results.

πŸ“Œ Task
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States torgosPizza Portland, OR

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024