143 lines
6.8 KiB
Markdown
143 lines
6.8 KiB
Markdown
# 🐬 MySQL & MariaDB
|
|
|
|
## Overview
|
|
|
|
MySQL is the most widespread open-source relational database, especially in web environments (LAMP stack). MariaDB is a fork after Oracle's acquisition, fully compatible with extensions. Default choice for WordPress, Drupal, Magento, and most PHP applications.
|
|
|
|
## Architecture (server + storage engine)
|
|
|
|
Based on *High Performance MySQL* (Schwartz, Zaitsev, Tkachenko):
|
|
|
|
```text
|
|
MySQL Server Layer
|
|
├── Connection handling (thread-per-connection)
|
|
├── Query parser & optimizer
|
|
├── Built-in functions
|
|
└── Storage Engine API
|
|
├── InnoDB (default, MVCC, ACID)
|
|
├── MyISAM (legacy, table-level locks)
|
|
├── MEMORY (in-memory, HEAP)
|
|
└── ... (others)
|
|
```
|
|
|
|
### InnoDB (default engine since MySQL 5.5+)
|
|
|
|
- **MVCC** — Multi-Version Concurrency Control (snapshot isolation)
|
|
- **REPEATABLE READ** (default) — next-key locking prevents phantom reads
|
|
- **Clustered index** — primary key = physical data ordering
|
|
- **Buffer pool** — cache of data and indexes in RAM (main performance parameter)
|
|
- **Doublewrite buffer** — prevents partial page writes
|
|
|
|
### Schema design tips
|
|
|
|
- Prefer smaller data types (MEDIUMINT over INT, TIMESTAMP over DATETIME)
|
|
- Use NULL carefully (each NULL column increases index complexity)
|
|
- Use ENUM only for truly small, stable value lists
|
|
- JSON columns in MySQL 8+ — useful for flexible schema, but not for joins
|
|
|
|
### Deferred join pattern
|
|
|
|
```sql
|
|
-- 1. covering index finds PK
|
|
-- 2. only then join to full row
|
|
SELECT * FROM users
|
|
INNER JOIN (
|
|
SELECT id FROM users
|
|
WHERE status = 'active'
|
|
ORDER BY created_at DESC
|
|
LIMIT 100 OFFSET 1000
|
|
) AS tmp USING (id);
|
|
```
|
|
|
|
**Join decomposition**: Sometimes it's better to split a JOIN into several simple queries (better cache utilization, fewer locks, scaling across servers).
|
|
|
|
**IN() optimization**: MySQL sorts values in the IN() list and uses binary search (O(log n)), unlike OR clauses (O(n)).
|
|
|
|
## MariaDB differences from MySQL
|
|
|
|
| Feature | MySQL 8.x | MariaDB 11.x |
|
|
|---------|-----------|--------------|
|
|
| **Storage engine** | InnoDB (only) | InnoDB + XtraDB (fork) + Aria + MyRocks |
|
|
| **JSON** | Native JSON type | JSON alias to LONGTEXT + JSON functions |
|
|
| **CTE** | WITH (non-recursive + recursive) | WITH (non-recursive + recursive) |
|
|
| **Window functions** | Yes (8.0+) | Yes (10.2+) |
|
|
| **Sequence** | No (auto_increment only) | Yes (CREATE SEQUENCE) |
|
|
| **Thread pooling** | Enterprise only | Built-in |
|
|
| **Galera cluster** | No (natively) | Yes (native synchronous clustering) |
|
|
|
|
## ProxySQL
|
|
|
|
ProxySQL is an advanced proxy for MySQL with sophisticated routing:
|
|
|
|
| Feature | Description |
|
|
|---------|-------------|
|
|
| **Query routing** | Rules for directing queries (read/write split, sharding) |
|
|
| **Connection pooling** | Multiplexing thousands of connections into a small pool |
|
|
| **Query cache** | Result caching in memory (TTL, size limit) |
|
|
| **Query rewriting** | Rewrite SQL queries in transit |
|
|
| **Active monitoring** | Backend outage detection, automatic failover |
|
|
|
|
## Recommendations — where MySQL is better
|
|
|
|
| Area | MySQL | Competition | Why MySQL |
|
|
|------|-------|------------|-----------|
|
|
| **Web applications** | De facto standard for WP, Drupal, Magento | PostgreSQL (fewer CMS plugins) | Broadest support in web hosting providers |
|
|
| **Read-heavy (SELECT heavy)** | InnoDB buffer pool, covering index, adaptive hash | PostgreSQL (MVCC overhead on reads) | Cache-efficient, fast point lookups |
|
|
| **Replication** | Async replication, Group Replication, InnoDB Cluster | PostgreSQL (streaming replication) | Simpler setup, extensive documentation |
|
|
| **Ecosystem** | ProxySQL, Orchestrator, Vitess, PlanetScale | PostgreSQL (fewer tools) | Most tooling for cluster management |
|
|
| **JSON in MySQL 8+** | JSON data type, Multi-Value Indexes | PostgreSQL (jsonb, GIN) | Comparable, Multi-Value Index unique |
|
|
|
|
### When to use MySQL / MariaDB
|
|
|
|
- **CMS / e-commerce** — WordPress, Drupal, Magento, Joomla (all require MySQL)
|
|
- **Read-heavy applications** — InnoDB buffer pool efficiently caches frequently read data
|
|
- **Simple replication** — Group Replication / InnoDB Cluster for HA
|
|
- **MariaDB for Galera cluster** — synchronous multi-master clustering
|
|
- **PHP applications** — native PHP MySQL extensions (mysqli, PDO_MySQL)
|
|
|
|
## MySQL / MariaDB licensing
|
|
|
|
### MySQL licensing
|
|
|
|
| Variant | License | Price | Restrictions |
|
|
|---------|---------|-------|-------------|
|
|
| **MySQL Community (GPL)** | GPL v2 | $0 | If you distribute an application that contains MySQL (e.g., embedded), you must release the entire application under GPL. Web applications (over network) ≠ distribution — GPL does not apply |
|
|
| **MySQL Standard (Commercial)** | Commercial (Oracle) | ~$2,000/server/year | No GPL restrictions, production support, MySQL Enterprise Monitor |
|
|
| **MySQL Enterprise** | Commercial (Oracle) | ~$5,000/server/year | All above + MySQL Enterprise Backup, Audit, Firewall, Thread Pool, Encryption |
|
|
| **MySQL Cluster CGE** | Commercial (Oracle) | ~$10,000/server/year | Distributed multi-master cluster (NDB), telco-grade |
|
|
|
|
**When GPL matters**: If you embed MySQL into a commercial product (e.g., desktop application with MySQL library). Web applications communicating over TCP/IP are **not** distribution — GPL does not apply.
|
|
|
|
### MariaDB licensing
|
|
|
|
| Variant | License | Price | Restrictions |
|
|
|---------|---------|-------|-------------|
|
|
| **MariaDB Community** | GPL v2 | $0 | Same as MySQL Community — GPL, but without Oracle licensing risks |
|
|
| **MariaDB Enterprise** | Business Source License (BSL) | Subscription (~$2-5k/server/year) | Automatically converts to GPL v2 after 3 years. Includes enterprise features (ColumnStore, Spider, Xpand) |
|
|
| **MariaDB SkySQL** | Managed (BSL) | Pay-per-use (~$0.10-1.00/hour) | Fully managed DBaaS |
|
|
|
|
**Key difference from Oracle MySQL**:
|
|
- MariaDB is an independent fork, not controlled by Oracle
|
|
- BSL model is more liberal — becomes open source after 3 years
|
|
- MariaDB does not require commercial license for enterprise features (in MySQL they are enterprise-only)
|
|
|
|
### When to use something else
|
|
|
|
- **Complex queries / CTE / window functions** → PostgreSQL (more advanced optimizer)
|
|
- **GIS / geospatial data** → PostgreSQL + PostGIS
|
|
- **Consistency > speed** → PostgreSQL (SSI serializable)
|
|
- **High-throughput writes** → Cassandra (MySQL master bottleneck)
|
|
- **Distributed SQL cluster** → CockroachDB, Vitess (MySQL compatible sharding)
|
|
|
|
## Sources
|
|
|
|
References, books, and standards: [sources/databases/sources.en.md](sources/databases/sources.en.md)
|
|
|
|
### Recommended reading
|
|
|
|
| Book | Authors | ISBN | Description |
|
|
|------|---------|------|-------------|
|
|
| High Performance MySQL (4th ed.) | Schwartz, Zaitsev, Tkachenko | 978-1492075292 | Comprehensive guide to MySQL architecture, optimization, and monitoring |
|
|
|
|
*Last revision: 2026-06-03*
|