Problem/Motivation
Opening this as a more general plan issue than #3628885: Add an experimental entity json storage module after more investigation by @amateescu and some discussion between myself, @amateescu and @plach.
When we first added field API to Drupal core, we made a decision that every configurable field should get its own database table. The main reason for this was to avoid having to migrate field data between the base/data table and dedicated field tables if fields went from single to multiple cardinality or vice versa.
There are known drawbacks to this approach (these were known at the time too, it was an intentional decision at the time to simplify the field storage layer), and ten+ years later we increasingly have the tools to do things differently. On larger Drupal projects, I frequently see developers defining fields as base fields in code so that they appear on the base table, even if otherwise they'd be happy for them to be in config.
Known issues with the current storage:
1. Entity loading requires loading data from each field table, previously via dozens of individual queries, now using dozens of database joins in one large query after #3551308: Combine single cardinality fields into a single database query when loading entities and related issues.
2. Entity saving similarly requires writing to dozens of database tables.
3. Views and entity queries that add conditions on configurable fields, especially with a sort either on another configurable field or a base field are not able to use indexes well, so will usually end up scanning more rows than they would otherwise, sometimes with a temporary table or filesort.
4. Data (ID, revision ID, language, bundle) is duplicated across each table, increasing both raw database size and index sizes (e.g. every field and field revision table has an index on the bundle column). Overall this contributes to higher disk size and memory allocations for database servers, to ensure the database fits on disk and within innodb_buffer_pool_size.
To move field data from the base table to a dedicated table or vice versa, we need to create the table/column and copy data into it, then drop the old table. @amateescu has pointed out that this is mostly already implemented for base field deletion, which copies base field data to a dedicated table, from where the field deletion/purge process kicks in. We also have a more experience handling field schema changes having gone through making entiites revisionable etc. in the past.
We can't however put every single cardinality field on the base table, because MySQL has a row size limit of 65,535 bytes (mostly excludes TEXT/BLOB) and smaller realistic sizes for InnoDB - see https://dev.mysql.com/doc/refman/9.7/en/innodb-limits.html and https://dev.mysql.com/doc/refman/9.7/en/column-count-limit.html There's a limit of 64 secondary indexes with InnoDB.
Either the row size limit or the 64 secondary index limit is something we could very realistically hit on some sites, which can easily have more than 100-200 configured fields per entity type across various different bundles.
Steps to reproduce
Proposed resolution
Because we can't add every possible field to the base table, we will need to allow site owners to control which fields are put there. This can be an additional checkbox in the field UI, only available to single cardinality fields, to store data on the base table. We can add some help text to say this is most useful for fields used as conditions or sorts.
This will then allow existing sites to opt certain configured fields into being stored on the base table. We can default the checkbox to 'true' for new fields, at least for boolean and entity references, with a status report warning if an entity is approaching the limit on how many base fields it can have.
Alongside this, we should also look into adding a 'multi-field' module to core, the most recent discussions about an optimum schema for this would be having the field values in a JSON column, per 'field union JSON' in #3440578: [PP-2] JSON-based data storage proposal for component-based page building - this would help with row size limits. This needs its own plan issue to discuss but could be handled as an experimental module, possibly in contrib first.
Comments
Comment #2
webchickI know I'm not technically a core Product Manager anymore, but as a former Drupal Product Manager, I gotta say... ;)
Noooo, please do NOT add this as a UI element to Field UI. Make it a
drcommand or something.Rationale: The type of user that hangs out in Field UI is the same type of user who we traditionally refer to as a "site builder" and their area is defining content types and fields, clicking together views, enabling modules, etc.
If they check this box and something with this goes wrong mid-way, and suddenly their site is throwing errors, they will be completely helpless to do anything about it themselves.
Whereas, if it's a CLI command that will gate it to someone who has a reasonable shot at getting themselves out of trouble, because they'll know how to log into MySQL and check the database schema, review logs, etc.
Comment #3
catchYes this is true, so they'll know whether a field they're creating is likely to be used as a filter or condition in a view, or whether it's a long text description etc. and I think we can come up with language to make the storage choice easy enough to make.
If something goes wrong mid-way, that's what transactions are for. We can't use transactions for table creation/deletion (at least not in MySQL, it works in pgsql) but we can use it for moving the data itself which allows for rollback.
It might be possible to offer a UI option on field creation, have it disabled once the field has data (same as many other changes), and then you can 'unlock' this via the CLI or development mode (once that exists) or something to prevent it even accidentally being flipped on production. The UI on field creation would mean that site builders would get the performance benefits without having to go to the CLI to mess around.
But either way the actual value will need to live in field config so it can be deployed, and we can do that first without a UI (which means hand-editing config YAML to set it to start with) in one issue, and have a separate issue to discuss what (or if) a UI looks like.
edit: opened #3644274: Allow single cardinality configured fields to be stored in shared tables for the API part of this.