Skip to content

Add index on catalog_product_entity.type_id (#37431) - #41223

Open
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/37431-product-entity-type-id-index
Open

Add index on catalog_product_entity.type_id (#37431)#41223
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/37431-product-entity-type-id-index

Conversation

@lbajsarowicz

Copy link
Copy Markdown
Contributor

Description (*)

catalog_product_entity.type_id has no index. Core filters on it in the configurable and downloadable export row customizers, the admin widget product chooser, the low-stock report collection and the product frontend action synchronizer, and every one of those is a full table scan.

This adds a btree index on type_id and whitelists it. Declarative schema has no prefix-length support (index.xsd only knows a column name), so this is a full-column index.

Measured on a copy of the table grown to 307,200 rows (4,096 configurable, 303,104 simple), MySQL 8.0, medians of 3 from SHOW PROFILES:

Query before after plan before plan after
COUNT(*) WHERE type_id='configurable' 13.9 ms 0.42 ms ALL, 304,569 rows ref, 4,096 rows, Using index
entity_id, sku WHERE type_id='configurable' ORDER BY entity_id LIMIT 200 0.99 ms 0.19 ms index PRIMARY ref, Using index condition
COUNT(*) WHERE type_id='simple' 15.6 ms 20.7 ms ALL ref, 305,991 rows, Using index

The last row is the trade-off: for the dominant value the optimizer prefers the 130-byte key over the clustered scan and gets slower. Core never filters on type_id='simple' alone, and a prefix index that would shrink that cost is not expressible in declarative schema.

Line wrapping elsewhere in db_schema.xml is there because the Static Tests build fails on pre-existing line-length warnings in any touched file.

Local gates: unit and static pass. In the Catalog integration suite, SortingTest::testProductListOutOfStockSortOrderWithMysql fails identically on a clean 2.4-develop checkout in the same environment, so it is not related to this change.

Fixed Issues (if relevant)

  1. Fixes [Perf] Add index on type_id column for catalog_product_entity table #37431

Manual testing scenarios (*)

  1. bin/magento setup:upgrade on an existing install, then SHOW INDEX FROM catalog_product_entity shows CATALOG_PRODUCT_ENTITY_TYPE_ID.
  2. bin/magento setup:db-declaration:generate-whitelist --module-name=Magento_Catalog produces no diff.
  3. EXPLAIN SELECT COUNT(*) FROM catalog_product_entity WHERE type_id='configurable' uses the new key.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Core code filters catalog_product_entity by type_id in several places
(export RowCustomizers, widget Chooser, Lowstock collection, frontend
action Synchronizer) with no supporting index, forcing a full scan.

Declarative schema has no index prefix-length support, so this adds a
full-column btree index. Line wrapping in db_schema.xml satisfies the
Static Tests build for the touched file.
@m2-assistant

m2-assistant Bot commented Sep 5, 2026

Copy link
Copy Markdown

Hi @lbajsarowicz. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@lbajsarowicz

Copy link
Copy Markdown
Contributor Author

@magento run all tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P3 May be fixed according to the position in the backlog. Progress: pending review

Projects

Status: Pending Review

Development

Successfully merging this pull request may close these issues.

[Perf] Add index on type_id column for catalog_product_entity table

2 participants