Multi-Tenant Sharding
Extract data for specific tenants from multi-tenant SQL dumps.
Use Cases
Section titled “Use Cases”- Tenant isolation: Give customers their own data export
- Debugging: Reproduce issues with specific tenant data
- Testing: Create tenant-specific test fixtures
- Compliance: Data portability requests
Basic Extraction
Section titled “Basic Extraction”sql-splitter shard multi_tenant.sql \ --tenant-value 123 \ -o tenant_123.sql \ --progressHow It Works
Section titled “How It Works”- Identifies tables with the tenant column
- Finds all rows matching the tenant value
- Follows foreign keys to include related data
- Outputs a complete, consistent dump for that tenant
Tenant Column Detection
Section titled “Tenant Column Detection”sql-splitter auto-detects common column names:
tenant_idcompany_idaccount_idorg_idorganization_idworkspace_id
Override if needed:
sql-splitter shard dump.sql \ --tenant-column customer_id \ --tenant-value 42 \ -o tenant.sqlMultiple Tenants
Section titled “Multiple Tenants”Extract multiple tenants to separate files:
sql-splitter shard multi_tenant.sql \ --tenant-values "1,2,3" \ -o shards/ \ --progressCreates:
shards/├── tenant_1.sql├── tenant_2.sql└── tenant_3.sqlHandling Global Tables
Section titled “Handling Global Tables”Multi-tenant apps often have shared/lookup tables. Control how they’re handled:
# Include small lookup tables (default)sql-splitter shard dump.sql --tenant-value 123 --include-global lookups
# Include all non-tenant tablessql-splitter shard dump.sql --tenant-value 123 --include-global all
# Skip global tables entirelysql-splitter shard dump.sql --tenant-value 123 --include-global noneExample Schema
Section titled “Example Schema”-- Global tables (no tenant_id)CREATE TABLE countries (id INT PRIMARY KEY, name VARCHAR(100));CREATE TABLE currencies (id INT PRIMARY KEY, code VARCHAR(3));
-- Tenant tablesCREATE TABLE tenants (id INT PRIMARY KEY, name VARCHAR(100));CREATE TABLE users ( id INT PRIMARY KEY, tenant_id INT REFERENCES tenants(id), name VARCHAR(100));CREATE TABLE orders ( id INT PRIMARY KEY, tenant_id INT REFERENCES tenants(id), user_id INT REFERENCES users(id));Sharding for tenant 42:
- Includes all
usersandorderswheretenant_id = 42 - Follows FK to include the
tenantsrow - Includes
countriesandcurrencies(with--include-global lookups)
Combining with Sample
Section titled “Combining with Sample”For large tenants, combine with sampling:
# First shardsql-splitter shard dump.sql --tenant-value 123 -o tenant_123.sql
# Then samplesql-splitter sample tenant_123.sql \ -o tenant_123_sample.sql \ --percent 10 \ --preserve-relationsCombining with Redact
Section titled “Combining with Redact”Anonymize after sharding:
sql-splitter shard dump.sql --tenant-value 123 -o - | \ sql-splitter redact - \ --hash "*.email" \ --fake "*.name" \ -o tenant_123_safe.sql