Partitioning in Google Spanner
Google Spanner does not support traditional database partitioning (like PostgreSQL range or hash partitioning). Instead, it automatically…
Partitioning in Google Spanner

google spanner
Google Spanner does not support traditional database partitioning (like PostgreSQL range or hash partitioning). Instead, it automatically shards data using a combination of interleaving, primary key design, and load-balancing strategies.
1️⃣ How Spanner Handles Partitioning
Spanner distributes data across multiple nodes automatically, optimizing for performance and scalability. The key techniques include:
✅ Automatic Sharding — Spanner breaks large datasets into splits and spreads them across nodes. ✅ Primary Key-Based Distribution — Data is stored based on the lexicographical order of primary keys. ✅ Interleaving Tables — Helps logically partition child tables under a parent table for faster queries. ✅ Load Balancing — Spanner dynamically moves data splits to balance traffic across nodes.
2️⃣ Partitioning Strategy in Spanner
Since manual partitioning isn’t needed, proper schema design ensures efficient performance.
A. Using Primary Key Partitioning
- Spanner distributes data based on the primary key.
- Choosing a good primary key prevents hotspots (uneven load distribution).
- Example of a poor key:
TIMESTAMP(sequential inserts create write hotspots). - Example of a better key:
HASH(UserID) + TIMESTAMP(better distribution).
CREATE TABLE Orders (
OrderID STRING(36) NOT NULL,
UserID STRING(36) NOT NULL,
OrderDate TIMESTAMP NOT NULL,
Amount NUMERIC,
) PRIMARY KEY (UserID, OrderDate);
- This partitions orders by UserID, avoiding sequential writes.
B. Using Interleaved Tables for Logical Partitioning
- Interleaved tables improve performance by storing parent-child records together.
- Example: Partitioning transactions by user.
CREATE TABLE Users (
UserID STRING(36) NOT NULL,
Name STRING(100)
) PRIMARY KEY (UserID);
CREATE TABLE Transactions (
UserID STRING(36) NOT NULL,
TransactionID STRING(36) NOT NULL,
Amount NUMERIC NOT NULL,
TransactionDate TIMESTAMP NOT NULL
) PRIMARY KEY (UserID, TransactionID),
INTERLEAVE IN PARENT Users ON DELETE CASCADE;
- This ensures that user transactions stay close to their parent UserID, improving query performance.
3️⃣ Best Practices for Partitioning in Spanner
- Avoid Sequential Primary Keys — Instead of
AUTO_INCREMENTorTIMESTAMP, use UUIDs or hash-based keys. - Use Interleaving for Hierarchical Data — Helps with efficient reads and writes.
- Shard Writes with Hash Buckets — For high-traffic tables, prefix the primary key with a hash.
- Monitor Query Performance — Use Query Execution Plan to check for hotspots.
- Leverage Load Balancing — Spanner automatically moves splits to balance traffic, so design schemas accordingly.
4️⃣ Basic Importance of Using Google Spanner
✅ Massive Scalability — Supports multi-region distributed workloads. ✅ High Availability — 99.999% SLA with automatic failover. ✅ Strong Consistency — Unlike NoSQL, Spanner provides ACID transactions.
You can try it out and share your feedback.
Cheers!!!
메타데이터
- post_id
- ceeff53d733b
- slug
- partitioning-in-google-spanner-ceeff53d733b
- url
- https://medium.com/@sunnex0/partitioning-in-google-spanner-ceeff53d733b
- canonical_url
- https://medium.com/@sunnex0/partitioning-in-google-spanner-ceeff53d733b
- author_url
- https://medium.com/@sunnex0
- status
- ok
- fetched_at
- 2026-06-26 03:39:16