Databases 3 min read

How to Query Sharded Tables: Single, Multi-Value and Cross-DB Joins

This article explains how to query sharded tables by routing single‑value and multi‑value lookups using hash functions, handling non‑sharded fields, and performing cross‑database joins, illustrating each case with SQL examples and diagrams of shard selection.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Query Sharded Tables: Single, Multi-Value and Cross-DB Joins

Querying Sharded Fields

Single‑value query SELECT * FROM table1 WHERE user_id = 'test1234'; When user_id is the sharding key, the routing algorithm computes the hash of 'test1234' to determine the target shard, then retrieves the record directly from that shard.

Example:

hash('test1234') = 2398283927 % 1024 = 531 → shard 3
Shard calculation example
Shard calculation example

Multi‑value query

SELECT * FROM table1 WHERE user_id IN ('test1234','papa17','abcd');

Each value is routed independently to its shard; the results from the relevant shards are then merged.

Example:

hash('test1234') = 2398283927 % 1024 = 531 → shard 3
hash('papa17') = 3339829221 % 1024 = 511 → shard 3
hash('abcd') = 55239822711 % 1024 = 130 → shard 1
Multi‑value shard routing
Multi‑value shard routing

Querying Non‑Sharded Fields

SELECT * FROM table1 WHERE name = 'dys';

Since name is not a sharding key, the query must be executed on every shard and the results merged, which is less efficient.

Non‑sharded field query across shards
Non‑sharded field query across shards

Cross‑Database Join Queries

SELECT * FROM table1 INNER JOIN table2 ON table1.user_id = table2.name;

Cross‑shard joins have no highly efficient solution; each shard must be queried and the results combined, which can be costly.

Cross‑shard join illustration
Cross‑shard join illustration

Pseudo‑code example

for row in (select * from table1) {
    add (select * from table2 where table2.name = row.user_id) to result;
}

In practice, middleware often only supports simple joins; complex cross‑shard queries are usually offloaded to search services such as Solr.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLshardingquery routingcross-database join
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.