是否存在支持多独立主键的数据库引擎?该特性是否已实现?
Great question! Let's break this down clearly since you're specifically asking for multiple independent primary keys (not composite ones) where each key maps to a full copy of the data—something that's totally valid if you're willing to trade storage for query performance.
Here are the main implementations across different database types:
NoSQL Databases (Native Support)
These engines are designed from the ground up to handle multi-dimensional access patterns with automatic data replication for alternate keys:
- Amazon DynamoDB: Use Global Secondary Indexes (GSIs). When you create a GSI, you define a completely independent primary key (different from the base table's primary key), and DynamoDB automatically replicates the full row data to the GSI's storage layer. It handles syncing updates/deletes between the base table and GSI (you can choose between eventual or strong consistency for reads). This is exactly the behavior you're looking for—each GSI acts as an additional primary key with its own data copy.
- Apache Cassandra: Leverage Materialized Views. You can create a materialized view with a new partition key (Cassandra's equivalent of a primary key for partitioning), and Cassandra will automatically sync the view with the base table. The materialized view stores full row data, so querying against its partition key is as fast as querying the base table's primary key, with the engine managing all replication behind the scenes.
Relational Databases (Via Engine Features)
While standard SQL only allows one PRIMARY KEY per table, you can achieve the same effect using built-in features that create replicated data copies tied to alternate unique keys:
- PostgreSQL: Use refreshable materialized views with a unique constraint (acting as your secondary "primary key"). For near-real-time sync, pair the materialized view with triggers on the base table that update the view whenever data changes. If you can tolerate slight delays, you can use scheduled refreshes (e.g., with
pg_cron). - Oracle: Create fast-refresh materialized views. Oracle supports materialized views that sync incrementally with the base table whenever data is modified. You can define a primary key on the materialized view, effectively creating a replicated copy of the data indexed by a different key.
- SQL Server: Use Indexed Views (also called materialized views). When you create a clustered index on a view, SQL Server physically stores the view's data as a separate table. You can set the clustered index key as your alternate primary key, and the engine automatically keeps the view synced with the base table in real time.
Niche/Specialized Databases
- TileDB: A columnar database optimized for multi-dimensional data. It supports multiple "dimension indexes" that act as independent primary keys, with data stored in layouts optimized for each dimension. This is ideal if you're working with array-like data (e.g., geospatial, time-series) and need fast access across different key dimensions.
All these solutions handle the data replication automatically, so you don't have to manage copies manually—you just define your alternate keys, and the engine takes care of syncing.
内容的提问来源于stack exchange,提问作者yigal

