> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pg-sharding.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Direct key sharding

Direct key sharding (also known as range-based sharding) is a method of distributing data across multiple database shards where each shard holds a contiguous range of data. The sharding key value is used directly to determine which key range (and therefore which shard) the data belongs to.

For example, if you are sharding a database of user records by user ID, you might have one shard that holds users with IDs from 1 to 1000, another shard that holds users with IDs from 1001 to 2000, and so on.

This method allows for efficient range queries, as all the data within a specific range is located on the same shard. However, it can lead to uneven distribution of data and load between key ranges. If you need more uniform distribution, consider using [hash-based sharding](/sharding/hashed), which also uses key ranges but applies a hash function to the key first.

SPQR also supports [composite (multi-column) sharding keys](/sharding/composite_keys) for more complex range-based sharding scenarios, such as sharding by tenant ID and user ID together.

<img src="https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_ranged.png?fit=max&auto=format&n=9v2nyWcfr6Uwa9yC&q=85&s=8394bfc64c02e9b6e8dad98d3e56b9ab" alt="ranged" width="2386" height="722" data-path="images/balancing_ranged.png" />

## How to use it?

In administrative router or coordinator console it looks like this:

```sql theme={null}
CREATE KEY RANGE krid1 FROM 1 ROUTE TO shard01 FOR DISTRIBUTION ds1;
 add key range 
---------------
 bound -> 1
(1 row)
```

For clarity, we recommend you take a look [get started](/welcome/get_started) guide.
