Technology Sharing

Architecture Interview - Database Optimization Questions

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

How to locate slow queries

Locating slow queries is a key step in database optimization, especially for relational databases such as MySQL, PostgreSQL, etc. The following are the general steps to locate and optimize slow queries:

1. Enable slow query log

Most relational databases support slow query logging, which can help record SQL statements whose execution time exceeds a set threshold. First, you need to make sure that the slow query log is enabled.

MySQL example:
  • existmy.cnformy.iniAdd or modify the following lines in the configuration file:

    [mysqld]
    slow_query_log = 1
    slow_query_log_file = /var/log/mysql/slow.log
    long_query_time = 2