Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add foreign key indices for *massive* performance wins! #1502

Closed
skid opened this issue Apr 4, 2022 · 1 comment
Closed

Add foreign key indices for *massive* performance wins! #1502

skid opened this issue Apr 4, 2022 · 1 comment
Milestone

Comments

@skid
Copy link
Contributor

skid commented Apr 4, 2022

Is your feature request related to a problem? Please describe.

Once you get to thousands of orders/products/customers, queries start to become slow. For exmple, we have around 60,000 imported orders and the admin order listing screen is quite slow. A simple explain analyze revealed the problem:

At least on PostgreSQL, indices are not automatically added to foreign key relations. So, for example, translation tables do not have indices on their baseId columns. This causes left joins on translation tables to perform sequential scans, and Vendure has quite a lot of queries like this one: SELECT * FROM product p LEFT JOIN product_translation t ON p.id = t.baseId.

It's not only on translation tables - these sequential scans happen everywhere. Here's an example visualization:

seq_scans

Here the `product_variant_price` does a seq scan each time it's joined to the `product_variant` table, which is basically every time you load a list of products with prices, which is ... 90% of the queries.

Describe the solution you'd like

I assume that ecommerce applications are read heavy. Many people browse all the time, and some of them add items to cart. It makese sense to add indices to ALL foreign keys.

Alternatively, it would also be useful for a developer using Vendure to be able to decorate Vendure entities with indices, or maybe add indices through configuration. There are also cases where non-foreign key indices are needed - e.g. we are searching the facet_value_translation tables by name.

@michaelbromley
Copy link
Member

Note: it seems that MySQL automatically adds an index to all FK constraints: https://stackoverflow.com/a/304339/772859

Also confirmed by looking at the schema definition for OrderLine:
image

michaelbromley added a commit that referenced this issue Apr 14, 2022
Relates to #1506, relates to #1502. This new config option allows the modification of TypeORM
metadata for the built-in entities. This allows, for instance, adding of
indices to columns to improve performance.
michaelbromley added a commit that referenced this issue Apr 25, 2022
Relates to #1502. Performance & efficiency improvement for Postgres specifically.

BREAKING CHANGE: Explicit indexes have been added to many-to-one relations used throughout the data
model. If you are using MySQL/MariaDB you should not notice a change from this, since they
automatically add indexes to FK relations. Postgres, however, does not so this change will require
a DB migration.
@michaelbromley michaelbromley moved this to 🔖 Ready in Vendure OS Roadmap Jul 1, 2022
@michaelbromley michaelbromley added this to the v2.0 milestone Jul 1, 2022
michaelbromley added a commit that referenced this issue May 30, 2023
@michaelbromley michaelbromley moved this from 🔖 Ready to ✅ Done in Vendure OS Roadmap Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

2 participants