Primary key design is a core part of relational database table design. In OceanBase Database, a primary key not only ensures data uniqueness but also, because data is sorted by the primary key, can be used to optimize the performance of range queries. Different primary key design schemes suit different business scenarios.
Scenarios without a primary key
Choose a single-table columnstore table without a primary key
In data warehouse or online analytical processing (OLAP) scenarios, business data tables often hold a huge volume of data, and most queries access only a few columns. If the business has no special requirements, such as ensuring data uniqueness, preventing duplicate writes, or performing transactional update or insert operations, creating a columnstore table without a primary key is more efficient:
- Data import: In AP scenarios, data does not need to be sorted by the primary key, so importing large volumes of data is faster than with tables that have a primary key.
- Lower primary key management cost.
show parameters like "%store_format%";
CREATE TABLE customer (
user_id bigint NOT NULL,
login_time timestamp NOT NULL,
customer_name varchar(100) NOT NULL, -- Assume that the customer name is at most 100 characters.
phone_num bigint NOT NULL, -- Store the phone number as bigint.
city_name varchar(50) NOT NULL, -- Assume that the city name is at most 50 characters.
sex int NOT NULL, -- Store gender as int (for example, 0 for female and 1 for male).
id_number varchar(18) NOT NULL, -- Assume that the ID number is at most 18 characters.
home_address varchar(255) NOT NULL, -- Assume that the home address is at most 255 characters.
office_address varchar(255) NOT NULL, -- Assume that the office address is at most 255 characters.
age int NOT NULL -- Store age as int.
);
Scenarios with a primary key
AP scenarios use heap tables by default, which have the following characteristics:
Data storage: Data is stored in insertion order, without physical sorting.
Role of the primary key:
- Ensures data uniqueness, so the primary key must include fields that uniquely identify a record (such as user_id).
- Supports partition key embedding, so the primary key must include all partition keys (such as user_id and age) to ensure even data distribution.
- Requires indexes or partitioning strategies to accelerate queries (such as
ORDER BY).
Example: a heap-organized table with a primary key
CREATE TABLE customer (
user_id BIGINT NOT NULL,
login_time TIMESTAMP NOT NULL,
customer_name VARCHAR(100) NOT NULL,
phone_num BIGINT NOT NULL,
city_name VARCHAR(50) NOT NULL,
sex INT NOT NULL,
id_number VARCHAR(18) NOT NULL,
home_address VARCHAR(255) NOT NULL,
office_address VARCHAR(255) NOT NULL,
age INT NOT NULL,
-- The primary key includes all partition keys (user_id and age).
PRIMARY KEY (user_id, age, login_time)
)
-- Primary partition: distributed by HASH on user_id
PARTITION BY HASH(user_id)
PARTITIONS 128
SUBPARTITION BY RANGE(age)
SUBPARTITION TEMPLATE (
-- Example partitions: divided by age group
SUBPARTITION p_youth VALUES LESS THAN (25), -- Age < 25
SUBPARTITION p_adult VALUES LESS THAN (40), -- 25 ≤ age < 40
SUBPARTITION p_middle_aged VALUES LESS THAN (60), -- 40 ≤ age < 60
SUBPARTITION p_senior VALUES LESS THAN (MAXVALUE) -- Age 60 and above
);
Scenarios for adding a primary key
- Ensure data uniqueness: When each record in a table must be unique, a primary key must be designed.
- Improve query efficiency: A primary key helps the optimizer generate more efficient query plans, especially when queries depend on the primary key. It is recommended to include the fields that queries always carry as primary key fields, so that the primary key can be used to improve query performance.
CREATE TABLE customer (
user_id BIGINT NOT NULL,
login_time TIMESTAMP NOT NULL,
customer_name VARCHAR(100) NOT NULL,
phone_num BIGINT NOT NULL,
city_name VARCHAR(50) NOT NULL,
sex INT NOT NULL,
id_number VARCHAR(18) NOT NULL,
home_address VARCHAR(255) NOT NULL,
office_address VARCHAR(255) NOT NULL,
age INT NOT NULL,
-- The primary key includes all partition keys (user_id and age).
PRIMARY KEY (user_id, age, login_time)
)
-- Primary partition: distributed by HASH on user_id
PARTITION BY HASH(user_id)
PARTITIONS 128
SUBPARTITION BY RANGE(age)
SUBPARTITION TEMPLATE (
-- Example partitions: divided by age group
SUBPARTITION p_youth VALUES LESS THAN (25), -- Age < 25
SUBPARTITION p_adult VALUES LESS THAN (40), -- 25 ≤ age < 40
SUBPARTITION p_middle_aged VALUES LESS THAN (60), -- 40 ≤ age < 60
SUBPARTITION p_senior VALUES LESS THAN (MAXVALUE) -- Age 60 and above
);