Bumalik sa blog
Enero 27, 2026Mga Gabay

Paano Mag-optimize ng MySQL Performance

Kumpletong gabay sa pag-optimize ng MySQL database performance: configuration tuning, query optimization, indexing, at monitoring.

Paano Mag-optimize ng MySQL Performance

Ang MySQL performance optimization ay kritikal para sa mga applications na nagha-handle ng malalaking dami ng data o mataas na traffic. Saklaw ng gabay na ito ang mahahalagang techniques para mapabuti ang MySQL performance, mula sa configuration tuning hanggang sa query optimization.

Pag-optimize ng MySQL Configuration

I-edit ang MySQL configuration file para ma-optimize ang performance batay sa iyong server resources:

bash
# I-edit ang MySQL configuration (Ubuntu/Debian)
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

# I-edit ang MySQL configuration (CentOS/RHEL)
sudo nano /etc/my.cnf

# Magdagdag o baguhin ang mga settings na ito:
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
max_connections = 200
query_cache_size = 64M
query_cache_type = 1
tmp_table_size = 64M
max_heap_table_size = 64M

# I-restart ang MySQL
sudo systemctl restart mysql  # Ubuntu/Debian
sudo systemctl restart mysqld   # CentOS/RHEL

InnoDB Optimization

Ang InnoDB ay ang default storage engine ng MySQL. I-optimize ito para sa mas magandang performance:

bash
# Sa /etc/mysql/mysql.conf.d/mysqld.cnf o /etc/my.cnf
[mysqld]
# InnoDB buffer pool (gumamit ng 70-80% ng available RAM)
innodb_buffer_pool_size = 2G

# InnoDB log file size
innodb_log_file_size = 512M

# InnoDB flush method (para sa SSD)
innodb_flush_method = O_DIRECT

# InnoDB I/O threads
innodb_read_io_threads = 4
innodb_write_io_threads = 4

# InnoDB flush log sa transaction commit
innodb_flush_log_at_trx_commit = 2

Query Cache Configuration

Maaaring mapabuti ng query cache ang performance para sa read-heavy workloads (Tandaan: Deprecated na ang query cache sa MySQL 8.0):

bash
# Para sa MySQL 5.7 at mas nauna
[mysqld]
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 2M

# I-check ang query cache status
mysql> SHOW VARIABLES LIKE 'query_cache%';

# I-check ang query cache statistics
mysql> SHOW STATUS LIKE 'Qcache%';

Index Optimization

Ang tamang indexing ay kritikal para sa query performance:

bash
# I-analyze ang table para ma-update ang index statistics
ANALYZE TABLE table_name;

# I-check ang unused indexes
SELECT * FROM sys.schema_unused_indexes;

# Lumikha ng index
CREATE INDEX idx_column ON table_name(column_name);

# Lumikha ng composite index
CREATE INDEX idx_multi ON table_name(col1, col2, col3);

# Ipakita ang indexes
SHOW INDEXES FROM table_name;

# Tanggalin ang unused index
DROP INDEX idx_name ON table_name;

Query Optimization

I-optimize ang iyong SQL queries para sa mas magandang performance:

  • Gumamit ng EXPLAIN para i-analyze ang query execution plans: EXPLAIN SELECT * FROM table WHERE column = 'value'
  • Iwasan ang SELECT * - pumili lamang ng kailangang columns
  • Gumamit ng LIMIT para limitahan ang result sets
  • Gumamit ng tamang WHERE clauses na may indexed columns
  • Iwasan ang functions sa WHERE clauses (hal. WHERE YEAR(date) = 2026 → WHERE date >= '2026-01-01')
  • Gumamit ng JOINs sa halip na subqueries kapag posible
  • Gumamit ng UNION sa halip na OR para sa multiple conditions kapag angkop

Pag-monitor ng MySQL Performance

bash
# I-check ang MySQL status
mysql> SHOW STATUS;

# I-check ang slow queries
mysql> SHOW VARIABLES LIKE 'slow_query%';

# I-enable ang slow query log
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;

# I-check ang process list
mysql> SHOW PROCESSLIST;

# I-check ang table status
mysql> SHOW TABLE STATUS LIKE 'table_name';

# I-check ang InnoDB status
mysql> SHOW ENGINE INNODB STATUS;

Performance Optimization Tips

  • Regular na mag-run ng OPTIMIZE TABLE para ma-defragment ang tables
  • I-monitor ang slow query log at i-optimize ang slow queries
  • Gumamit ng connection pooling para mabawasan ang connection overhead
  • I-partition ang malalaking tables ayon sa date o range para sa mas magandang performance
  • Gumamit ng read replicas para sa read-heavy workloads
  • Panatilihin ang MySQL na updated sa pinakabagong stable version
  • I-monitor ang disk I/O at isaalang-alang ang paggamit ng SSD/NVMe storage
  • Gumamit ng EXPLAIN ANALYZE (MySQL 8.0+) para sa detalyadong query analysis