Help Center> Data Warehouse Service> Troubleshooting> Database Performance and Resources> SQL Execution Is Slow with Low Performance and Sometimes Does Not End After a Long Period of Time

SQL Execution Is Slow with Low Performance and Sometimes Does Not End After a Long Period of Time

Symptom

The SQL execution is slow with low performance and sometimes does not end after a long period of time.

Possible Causes

Analyze the causes of slow SQL execution from the following aspects:

  1. Run the EXPLAIN command to view the SQL execution plan and determine whether to optimize the SQL statements based on the plan.
  2. Check whether the query is blocked. If the query is blocked, the statement execution takes a long time. In this case, you can forcibly terminate the abnormal sessions.
  3. Review and modify the table definitions. Select an appropriate distribution key to avoid data skew.
  4. Check whether the SQL statements use functions that do not support pushdown. You are advised to use the syntax or function that supports pushdown.
  5. Run the VACUUM FULL and ANALYZE commands periodically to reclaim the disk space occupied by updated or deleted data.
  6. Check whether the table has an index. You are advised to re-create the index regularly.

    After multiple deletion operations are performed on the database, the index key on the index page will be deleted, causing index expansion. Re-creating the index regularly can improve the query efficiency.

  7. Optimize services and analyze whether large tables can be divided.

Handling Procedure

GaussDB(DWS) provides methods for analyzing and optimizing queries, as well as some common cases and error handling methods. For details about how to tune SQL performance, see Query Performance Optimization.Common problem locating methods are as follows:

Method 1: Periodically collect statistics on the table and optimize the table data.

  • Perform ANALYZE on tables that you often insert data to.
    Analyze tablename;
  • Perform VACUUM FULL on tables that you often remove data from.
    Vacuum full tablename;

    VACUUM FULL cannot be used when other tasks are running.

  • Query the table size. If the table size is large but only a small amount of data exists, run the VACUUM FULL command to reclaim the space.
    select * from pg_size_pretty(pg_table_size('tablename'));

Method 2: Run the pg_stat_activity statement to query information about the running SQL statements.

  1. Query the active SQL statements in the background.

    SELECT pid,datname, usename, state,waiting, query FROM pgxc_stat_activity WHERE  state <> 'idle';

  2. Query the SQL statements of locked background services.

    SELECT pid,datname, usename, state,waiting, query FROM pgxc_stat_activity WHERE  state <> 'idle' and waiting=true;

  3. Check whether a lock exists.

    1. If no, search for the SQL statements of related services and rectify the fault by referring to method 1.
    2. If yes, find the pid column and run the following statement to end the task and release the lock:
      SELECT pg_terminate_backend(pid);