Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

MySQL Storage Engines and Index Implementation

Tech May 15 13

MySQL Architecture Layers

MySQL's architecture comprises four interconnected layers:

  1. Network Connection Layer: Manages client connections via thread pooling
  2. Core Service Layer: Handles SQL parsing, optimization, and caching
  3. Storage Engine Layer: Plugin-based data managemnet system
  4. File System Layer: Stores configuration, data, and log files

Storage Engine Comparision

Feature InnoDB MyISAM MEMORY
Transactional Support Yes No No
Locking Mechanism Row/Table Table Table
Foreign Keys Supported Not Supported Not Supported
Crash Recovery ACID Compliant Partial None

Storage Engine Operations

-- List supported engines
SHOW ENGINES;

-- Create table with specific engine
CREATE TABLE device_data(
  device_id INT PRIMARY KEY AUTO_INCREMENT,
  sensor_value DECIMAL(10,2)
) ENGINE = MEMORY;

-- Modify table engine
ALTER TABLE device_data ENGINE = INNODB;

Endex Implementation

Indexes accelerate data retrieval through optimized data structures:

  • B-Tree: Balanced tree structure for efficient range queries
  • B+Tree: Enhanced version with sequential leaf node pointers
  • Hash: Key-value pairs for exact match lookups

Index Operations

-- Create composite index
CREATE INDEX idx_sensor_location 
ON device_data(location_id, reading_time);

-- Display table indexes
SHOW INDEX FROM device_data;

-- Remove index
DROP INDEX idx_sensor_location ON device_data;

Locking Mechanisms

Lock Type Scope Conflict Cases
Shared (Read) Row/Table Blocks write operations
Exclusive (Write) Row/Table Blocks read/write operations
-- Acquire shared lock
SELECT * FROM network_logs 
WHERE status = 'error' LOCK IN SHARE MODE;

-- Acquire exclusive lock
SELECT * FROM user_balance 
FOR UPDATE;

Optimistic Locking Example

CREATE TABLE inventory(
  item_id INT PRIMARY KEY,
  quantity INT,
  version INT DEFAULT 0
);

-- Concurrent update handling
UPDATE inventory 
SET quantity = 95, version = version + 1
WHERE item_id = 101 AND version = 5;

Clustering with MyCat

Configuration for horizontal sharding:

<schema name="SHARDED_DB">
  <table name="user_actions" dataNode="dn1,dn2" rule="shard_mod" />
</schema>

<dataNode name="dn1" dataHost="host1" database="shard1" />
<dataNode name="dn2" dataHost="host2" database="shard2" />

<function name="shard_mod" class="PartitionByMod">
  <property name="count">2</property>
</function>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.