Help Center> GaussDB(DWS)> Troubleshooting> Cluster Performance> SQL Execution Is Slow with Low Performance and Sometimes Does Not End After a Long Period of Time
Updated on 2024-01-25 GMT+08:00

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. For details about how to tune SQL performance, see "Query Performance Optimization" in the Data Warehouse Service (DWS) Developer Guide. Common problem locating methods are as follows:

  • Method 1: Periodically collect statistics on the table and optimize the table data.
    If you frequently run the INSERT statement to insert data into a table, you need to periodically run the ANALYZE statement on the table.
    1
    ANALYZE table_name;
    
    If you frequently run the DELETE statement to delete data from a table, you need to periodically run the VACUUM FULL statement on the table.
    1
    Vacuum full table_name;
    

    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.
    1
    2
    SELECT * FROM pg_size_pretty(pg_table_size('tablename'));
    Vacuum full table_name;
    

Method 2: Query information about running SQL statements in the PGXC_STAT_ACTIVITY view.

  1. Run the following command to view the information about the SQL statements that are not in the idle state:

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

  2. Run the following command to view blocked query statements:

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

  3. Check whether the query statement is blocked.

    • If no blocking occurs, search for related service tables and rectify the fault according to Method 1.
    • If a statement is blocked, end the blocked statement based on the thread ID of the faulty session.
      1
      SELECT pg_terminate_backend(pid);