|
| 1 | +# AliSQL with DuckDB Engine |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This repository contains **AliSQL** (Alibaba's MySQL fork) integrated with **DuckDB** as an analytical engine. This integration combines the OLTP capabilities of MySQL with the powerful OLAP features of DuckDB, providing a hybrid database solution for both transactional and analytical workloads. |
| 6 | + |
| 7 | +## Version Information |
| 8 | + |
| 9 | +- **AliSQL Version**: Based on upstream MySQL 8.0.44 |
| 10 | +- **DuckDB Engine**: Integrated as a storage/analytical engine within AliSQL |
| 11 | + |
| 12 | +## What is AliSQL? |
| 13 | + |
| 14 | +AliSQL is Alibaba's MySQL branch, forked from official MySQL and used extensively in Alibaba Group's production environment. It includes various performance optimizations, stability improvements, and features tailored for large-scale applications. |
| 15 | + |
| 16 | +## What is DuckDB? |
| 17 | + |
| 18 | +DuckDB is an open-source embedded analytical database system (OLAP) designed for data analysis workloads. DuckDB is rapidly becoming a popular choice in data science, BI tools, and embedded analytics scenarios due to its key characteristics: |
| 19 | + |
| 20 | +- **Exceptional Query Performance**: Single-node DuckDB performance not only far exceeds InnoDB, but even surpasses ClickHouse and SelectDB |
| 21 | +- **Excellent Compression**: DuckDB uses columnar storage and automatically selects appropriate compression algorithms based on data types, achieving very high compression ratios |
| 22 | +- **Embedded Design**: DuckDB is an embedded database system, naturally suitable for integration into MySQL |
| 23 | +- **Plugin Architecture**: DuckDB uses a plugin-based design, making it very convenient for third-party development and feature extensions |
| 24 | +- **Friendly License**: DuckDB's license allows any form of use, including commercial purposes |
| 25 | + |
| 26 | +## Why Integrate DuckDB with AliSQL? |
| 27 | + |
| 28 | +MySQL has long lacked an analytical query engine. While InnoDB is naturally designed for OLTP and excels in TP scenarios, its query efficiency is very low for analytical workloads. This integration enables: |
| 29 | + |
| 30 | +- **Hybrid Workloads**: Run both OLTP (MySQL/InnoDB) and OLAP (DuckDB) queries in a single database system |
| 31 | +- **High-Performance Analytics**: Analytical query performance improves up to **200x** compared to InnoDB |
| 32 | +- **Storage Cost Reduction**: DuckDB read replicas typically use only **20%** of the main instance's storage space due to high compression |
| 33 | +- **100% MySQL Syntax Compatibility**: No learning curve - DuckDB is integrated as a storage engine, so users continue using MySQL syntax |
| 34 | +- **Zero Additional Management Cost**: DuckDB instances are managed, operated, and monitored exactly like regular RDS MySQL instances |
| 35 | +- **One-Click Deployment**: Create DuckDB read-only instances with automatic data conversion from InnoDB to DuckDB |
| 36 | + |
| 37 | +## Architecture |
| 38 | + |
| 39 | +### MySQL's Pluggable Storage Engine Architecture |
| 40 | + |
| 41 | +MySQL's pluggable storage engine architecture allows it to extend its capabilities through different storage engines: |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +The architecture consists of four main layers: |
| 46 | +- **Runtime Layer**: Handles MySQL runtime tasks like communication, access control, system configuration, and monitoring |
| 47 | +- **Binlog Layer**: Manages binlog generation, replication, and application |
| 48 | +- **SQL Layer**: Handles SQL parsing, optimization, and execution |
| 49 | +- **Storage Engine Layer**: Manages data storage and access |
| 50 | + |
| 51 | +### DuckDB Read-Only Instance Architecture |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +DuckDB analytical read-only instances use a read-write separation architecture: |
| 56 | +- Analytical workloads are separated from the main instance, ensuring no mutual impact |
| 57 | +- Data replication from the main instance via binlog mechanism (similar to regular read replicas) |
| 58 | +- InnoDB stores only metadata and system information (accounts, configurations) |
| 59 | +- All user data resides in the DuckDB engine |
| 60 | + |
| 61 | +## Implementation Details |
| 62 | + |
| 63 | +### Query Path |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +1. Users connect via MySQL client |
| 68 | +2. MySQL parses the query and performs necessary processing |
| 69 | +3. SQL is sent to DuckDB engine for execution |
| 70 | +4. DuckDB returns results to server layer |
| 71 | +5. Server layer converts results to MySQL format and returns to client |
| 72 | + |
| 73 | +**Compatibility**: |
| 74 | +- Extended DuckDB's syntax parser to support MySQL-specific syntax |
| 75 | +- Rewrote numerous DuckDB functions and added many MySQL functions |
| 76 | +- Automated compatibility testing platform with ~170,000 SQL tests shows **99% compatibility rate** |
| 77 | + |
| 78 | +### Binlog Replication Path |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +Key features: |
| 83 | + |
| 84 | +**Idempotent Replay**: |
| 85 | +- Since DuckDB doesn't support two-phase commit, custom transaction commit and binlog replay processes ensure data consistency after instance crashes |
| 86 | + |
| 87 | +**DML Replay Optimization**: |
| 88 | +- DuckDB favors large transactions; frequent small transactions cause severe replication lag |
| 89 | +- Implemented batch replay mechanism achieving **30K rows/s** replay capability |
| 90 | +- In Sysbench testing, achieves zero replication lag, even higher than InnoDB replay performance |
| 91 | + |
| 92 | +**Parallel Copy DDL**: |
| 93 | +- For DDL operations DuckDB doesn't natively support (e.g., column reordering), implemented Copy DDL mechanism |
| 94 | +- Natively supported DDL uses Inplace/Instant execution |
| 95 | +- Copy DDL creates a new table to replace the original using multi-threaded parallel execution |
| 96 | +- Execution time reduced by **7x** |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +## Performance Benchmarks |
| 101 | + |
| 102 | +**Test Environment**: |
| 103 | +- ECS Instance: 32 CPU, 128GB Memory, ESSD PL1 Cloud Disk 500GB |
| 104 | +- Benchmark: TPC-H SF100 |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +DuckDB demonstrates significant performance advantages over InnoDB in analytical query scenarios, with up to **200x improvement**. |
| 109 | + |
| 110 | +## Getting Started |
| 111 | + |
| 112 | +### Building AliSQL with DuckDB Engine |
| 113 | + |
| 114 | +**Prerequisites**: |
| 115 | +- [CMake](https://cmake.org) 3.x or higher |
| 116 | +- Python3 |
| 117 | +- C++11 compliant compiler (GCC 5.x+ or Clang 3.4+) |
| 118 | + |
| 119 | +**Build Instructions**: |
| 120 | + |
| 121 | +```bash |
| 122 | +# Clone the repository |
| 123 | +git clone https://github.com/your-repo/myduck.git |
| 124 | +cd myduck |
| 125 | + |
| 126 | +# Build the project |
| 127 | +make |
| 128 | + |
| 129 | +# For development/debugging |
| 130 | +make debug |
| 131 | + |
| 132 | +# Run unit tests |
| 133 | +make unit |
| 134 | +make allunit |
| 135 | + |
| 136 | +# Build with benchmarks (optional) |
| 137 | +BUILD_BENCHMARK=1 BUILD_TPCH=1 make |
| 138 | +``` |
| 139 | + |
| 140 | +### Using DuckDB Engine in MySQL |
| 141 | + |
| 142 | +Once built, you can create tables using the DuckDB storage engine: |
| 143 | + |
| 144 | +```sql |
| 145 | +-- Create a table with DuckDB engine |
| 146 | +CREATE TABLE analytics_table ( |
| 147 | + id INT, |
| 148 | + name VARCHAR(100), |
| 149 | + value DECIMAL(10,2) |
| 150 | +) ENGINE=DuckDB; |
| 151 | + |
| 152 | +-- Import data from Parquet files |
| 153 | +LOAD DATA INFILE '/path/to/data.parquet' INTO TABLE analytics_table; |
| 154 | + |
| 155 | +-- Run analytical queries |
| 156 | +SELECT name, SUM(value) as total |
| 157 | +FROM analytics_table |
| 158 | +GROUP BY name |
| 159 | +ORDER BY total DESC; |
| 160 | +``` |
| 161 | + |
| 162 | +### Configuration |
| 163 | + |
| 164 | +Key MySQL parameters for DuckDB engine: |
| 165 | +- Configure DuckDB-specific settings through MySQL system variables |
| 166 | +- Refer to the documentation for tuning parameters based on your workload |
| 167 | + |
| 168 | +## Try It on Alibaba Cloud |
| 169 | + |
| 170 | +You can experience RDS MySQL with DuckDB engine on Alibaba Cloud: |
| 171 | + |
| 172 | +https://help.aliyun.com/zh/rds/apsaradb-rds-for-mysql/duckdb-based-analytical-instance/ |
| 173 | + |
| 174 | +## Resources |
| 175 | + |
| 176 | +- [DuckDB Official Documentation](https://duckdb.org/docs/stable/) |
| 177 | +- [DuckDB GitHub Repository](https://github.com/duckdb/duckdb) |
| 178 | +- [MySQL 8.0 Documentation](https://dev.mysql.com/doc/refman/8.0/en/) |
| 179 | +- [Detailed Article (Chinese)](https://mp.weixin.qq.com/s/_YmlV3vPc9CksumXvXWBEw) |
| 180 | + |
| 181 | +## SQL Reference |
| 182 | + |
| 183 | +The documentation contains a [SQL introduction and reference](https://duckdb.org/docs/stable/sql/introduction). |
| 184 | + |
| 185 | +## Development |
| 186 | + |
| 187 | +Please refer to the [DuckDB Build Guide](https://duckdb.org/docs/stable/dev/building/overview) for detailed build instructions. |
| 188 | + |
| 189 | +The benchmark details are available in the [Benchmark Guide](benchmark/README.md). |
| 190 | + |
| 191 | +## Support |
| 192 | + |
| 193 | +For DuckDB-specific support, see the [Support Options](https://duckdblabs.com/support/) page. |
| 194 | + |
0 commit comments