This skill helps you design efficient OceanBase schemas for MySQL and Oracle modes, covering tables, partitions, indexes, and table groups to boost performance.
npx playbooks add skill amber-moe/oceanbase-doc-skills --skill oceanbase-schema-design
Expert in database schema design for OceanBase distributed database (MySQL & Oracle modes).
Provides best practices for designing efficient table structures, partitioning strategies, table groups, indexes, and schema optimization in OceanBase. This skill covers both MySQL mode and Oracle mode.
---
While OceanBase uses the same underlying storage and partitioning mechanisms for both MySQL and Oracle modes, there are important syntax differences:
| Aspect | MySQL Mode | Oracle Mode |
|--------|-----------|-------------|
| Data Types | BIGINT, VARCHAR, DATETIME | NUMBER, VARCHAR2, DATE, TIMESTAMP |
| Schema Access | database.table | schema.table |
| Default Index | LOCAL (for partitioned tables) | GLOBAL (for partitioned tables) |
| Time Functions | NOW(), CURRENT_TIMESTAMP | SYSDATE, SYSTIMESTAMP |
| String Functions | CONCAT() | \|\| operator |
Design Principles:
Examples in this skill are marked with mode indicators: MySQL Mode: or Oracle Mode:
---
Three Normal Forms:
Important: Schema design should prioritize business performance over strict normalization. Appropriate data redundancy is acceptable to reduce table joins and improve performance. Redundancy fields should not be frequently modified and not be very long VARCHAR fields.
1. Primary key design:
-- ✅ GOOD: Use business fields as primary key
obclient [SALES_DB]> CREATE TABLE order_table (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT NOT NULL,
gmt_create TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- MySQL: CURRENT_TIMESTAMP, Oracle: SYSDATE
gmt_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) COMMENT='Order table';
-- ✅ GOOD: Composite primary key
obclient [SALES_DB]> CREATE TABLE order_item_table (
order_id BIGINT,
item_id BIGINT,
PRIMARY KEY (order_id, item_id)
);
Key points:
gmt_create, gmt_modifiedCOMMENT attributes and be NOT NULL with appropriate DEFAULT valuesTINYINT UNSIGNED for boolean fields (1=yes, 0=no)---
Use partitioning when:
Critical Rule:
Partition key selection principles:
-- ✅ GOOD: Hash partition on high-cardinality field
obclient [SALES_DB]> CREATE TABLE order_table (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT,
order_date DATE
) PARTITION BY HASH(order_id) PARTITIONS 8;
-- ✅ GOOD: Range partition for time-based data
obclient [SALES_DB]> CREATE TABLE order_log_table (
log_id BIGINT,
log_date DATE NOT NULL,
PRIMARY KEY (log_id, log_date)
) PARTITION BY RANGE COLUMNS(log_date) (
PARTITION p202401 VALUES LESS THAN('2024-02-01'),
PARTITION p202402 VALUES LESS THAN('2024-03-01'),
PARTITION pMAX VALUES LESS THAN MAXVALUE
);
Limitations: Hash partition is not suitable for range queries on partition key. Range partition is better for time-based queries.
Supported partition types:
-- Hash partition
obclient [SALES_DB]> CREATE TABLE order_table (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT
) PARTITION BY HASH(order_id) PARTITIONS 8;
-- Range partition
obclient [SALES_DB]> CREATE TABLE order_table (
order_id BIGINT,
order_date DATE NOT NULL,
PRIMARY KEY (order_id, order_date)
) PARTITION BY RANGE COLUMNS(order_date) (
PARTITION p2024Q1 VALUES LESS THAN('2024-04-01'),
PARTITION p2024Q2 VALUES LESS THAN('2024-07-01'),
PARTITION pMAX VALUES LESS THAN MAXVALUE
);
-- Composite partition (Hash + Range)
obclient [SALES_DB]> CREATE TABLE order_table (
order_id BIGINT,
customer_id BIGINT,
order_date DATE NOT NULL,
PRIMARY KEY (order_id, customer_id, order_date)
) PARTITION BY HASH(customer_id) PARTITIONS 8
SUBPARTITION BY RANGE COLUMNS(order_date) (
PARTITION p0 (
SUBPARTITION sp0_q1 VALUES LESS THAN('2024-04-01'),
SUBPARTITION sp0_q2 VALUES LESS THAN('2024-07-01')
)
-- ... more partitions
);
Partition pruning avoids accessing irrelevant partitions, significantly improving SQL execution efficiency.
Rules:
-- ✅ GOOD: Partition pruning works
obclient [SALES_DB]> SELECT * FROM order_table WHERE order_id = 12345;
-- Execution plan shows: partitions(p1) - only one partition accessed
-- ❌ BAD: No partition pruning (missing partition key or function on partition key)
obclient [SALES_DB]> SELECT * FROM order_table WHERE customer_id = 1001;
-- Execution plan shows: all partitions scanned
---
Table Group is a logical concept representing a collection of tables. It controls the physical storage proximity of related tables to enable Partition Wise Join.
1. SHARDING = 'NONE': All partitions co-located on same machine, no partition restrictions
2. SHARDING = 'PARTITION': Tables sharded by first-level partitions, all tables must have identical first-level partition definitions
3. SHARDING = 'ADAPTIVE' (Default): Tables sharded adaptively, enables Partition Wise Join for both first-level and second-level partitions
Use table groups when: Multiple tables are frequently joined together, tables have related partition keys, need to avoid cross-partition joins
-- Create table group and add tables
obclient [SALES_DB]> CREATE TABLEGROUP order_tg SHARDING = 'PARTITION'
PARTITION BY HASH(customer_id) PARTITIONS 8;
obclient [SALES_DB]> CREATE TABLE order_table (
order_id BIGINT,
customer_id BIGINT,
PRIMARY KEY (order_id, customer_id)
) PARTITION BY HASH(customer_id) PARTITIONS 8;
obclient [SALES_DB]> ALTER TABLE order_table SET TABLEGROUP order_tg;
-- Now JOIN queries can use Partition Wise Join
---
1. Local Index (LOCAL):
2. Global Index (GLOBAL):
-- MySQL Mode: Default is LOCAL
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date);
-- Creates LOCAL index by default
-- Create GLOBAL index explicitly
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date) GLOBAL;
1. Leftmost prefix principle: Index must match query pattern from leftmost column
-- ✅ GOOD: Index matches query
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date);
obclient [SALES_DB]> SELECT * FROM order_table
WHERE customer_id = 1001 AND order_date >= '2024-01-01';
-- ❌ BAD: Missing leftmost column prevents index usage
obclient [SALES_DB]> SELECT * FROM order_table WHERE order_date >= '2024-01-01';
-- Execution plan shows: TABLE SCAN (full table scan)
2. Composite index column order: Place high cardinality columns first
-- ✅ GOOD: High cardinality column first
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date);
3. Covering index: Include all query columns to avoid table lookup
obclient [SALES_DB]> CREATE INDEX idx_customer_date_amount ON order_table(customer_id, order_date)
STORING (total_amount, status);
4. ORDER BY optimization: Index can support ORDER BY if WHERE conditions match leftmost columns
5. Join field indexes: Always index join columns for efficient joins
General principles:
NOT NULL with appropriate DEFAULT valuesPartitioned table index:
Index maintenance:
ALTER TABLE ADD INDEX and DROP INDEX in one DDLCommon mistakes to avoid:
---
Before deploying a table schema to production:
---
Partition types: Hash (even distribution, point queries) | Range (time-based data) | List (discrete value sets) | Key (similar to Hash) | Composite (two partition types)
Table group SHARDING: NONE (no restriction) | PARTITION (first-level partitions must match) | ADAPTIVE (all first-level or all second-level must match)
Index types: LOCAL (default MySQL, better DML performance) | GLOBAL (default Oracle, cross-partition queries)
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.