Loading...
Loading...
Compare original and translation side by side
| Aspect | MySQL Mode | Oracle Mode |
|---|---|---|
| Data Types | | |
| Schema Access | | |
| Default Index | LOCAL (for partitioned tables) | GLOBAL (for partitioned tables) |
| Time Functions | | |
| String Functions | | |
| 对比项 | MySQL 模式 | Oracle 模式 |
|---|---|---|
| 数据类型 | | |
| Schema访问方式 | | |
| 默认索引类型 | LOCAL(分区表默认) | GLOBAL(分区表默认) |
| 时间函数 | | |
| 字符串函数 | | |
-- ✅ 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)
);gmt_creategmt_modifiedCOMMENTNOT NULLDEFAULTTINYINT UNSIGNED-- ✅ 推荐:使用业务字段作为主键
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';
-- ✅ 推荐:联合主键
obclient [SALES_DB]> CREATE TABLE order_item_table (
order_id BIGINT,
item_id BIGINT,
PRIMARY KEY (order_id, item_id)
);gmt_creategmt_modifiedCOMMENTNOT NULLDEFAULTTINYINT UNSIGNED-- ✅ 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
);-- ✅ 推荐:基于高基数字段做Hash分区
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;
-- ✅ 推荐:时间类数据使用Range分区
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
);-- 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
);-- Hash分区
obclient [SALES_DB]> CREATE TABLE order_table (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT
) PARTITION BY HASH(order_id) PARTITIONS 8;
-- Range分区
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
);
-- 复合分区(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')
)
-- ... 更多分区
);-- ✅ 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-- ✅ 推荐:分区裁剪生效
obclient [SALES_DB]> SELECT * FROM order_table WHERE order_id = 12345;
-- 执行计划显示:partitions(p1) - 仅访问1个分区
-- ❌ 不推荐:分区裁剪不生效(缺少分区键或分区键上使用了函数)
obclient [SALES_DB]> SELECT * FROM order_table WHERE customer_id = 1001;
-- 执行计划显示:扫描全部分区-- 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-- 创建表组并添加表
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;
-- 现在JOIN查询可以使用Partition Wise Join-- 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;-- MySQL模式:默认创建LOCAL索引
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date);
-- 默认创建LOCAL索引
-- 显式创建GLOBAL索引
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date) GLOBAL;-- ✅ 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)-- ✅ GOOD: High cardinality column first
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date);obclient [SALES_DB]> CREATE INDEX idx_customer_date_amount ON order_table(customer_id, order_date)
STORING (total_amount, status);-- ✅ 推荐:索引匹配查询条件
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';
-- ❌ 不推荐:缺少最左列导致索引无法生效
obclient [SALES_DB]> SELECT * FROM order_table WHERE order_date >= '2024-01-01';
-- 执行计划显示:TABLE SCAN(全表扫描)-- ✅ 推荐:高基数列放在最前
obclient [SALES_DB]> CREATE INDEX idx_customer_date ON order_table(customer_id, order_date);obclient [SALES_DB]> CREATE INDEX idx_customer_date_amount ON order_table(customer_id, order_date)
STORING (total_amount, status);NOT NULLDEFAULTALTER TABLE ADD INDEXDROP INDEXNOT NULLDEFAULTALTER TABLE ADD INDEXDROP INDEX