Explain the concept of database sharding and how it impacts query performance.
Instruction: Provide an explanation of database sharding, including the reasons for its use, and discuss its implications on SQL query performance.
Updated
Official answer
Sharding partitions a dataset across independently managed database units, usually routing rows with a shard key. It can distribute storage and work, but improves a workload only when the routing and capacity fit its access patterns.
A request for one customer's data may be efficient if the related rows are colocated on the customer's shard. Queries without a useful shard-key restriction may fan out to many shards, adding network work and coordination. Cross-shard joins, global ordering, uniqueness and transactions require explicit support or additional design.
Choose the key by examining query locality, skew, growth and rebalancing. Equal row counts do not imply equal traffic: a hot customer or monotonically growing range can overload one shard. A key that frequently changes can make moving data expensive.
Sharding is different from replication. Putting different customers in different regions does not by itself protect a customer's only copy from a regional outage. Availability requires an appropriate replication, failover and recovery design, with clearly stated consistency and data-loss tradeoffs.
Measure per-shard load and tail latency, test fan-out and failure behavior, and plan moves or resharding without double writes or missing reads. The operational complexity can outweigh the benefit when a simpler indexed or vertically scaled database is sufficient.
Assessment: Explain the routing key, the common query path, the expensive cross-shard path and the separate availability strategy.