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:
- Run the EXPLAIN command to view the SQL execution plan and determine whether to optimize the SQL statements based on the plan.
- 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.
- Review and modify the table definitions. Select an appropriate distribution key to avoid data skew.
- Check whether the SQL statements use functions that do not support pushdown. You are advised to use the syntax or function that supports pushdown.
- Run the VACUUM FULL and ANALYZE commands periodically to reclaim the disk space occupied by updated or deleted data.
- 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.
- 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. 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.
- 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';
- 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;
- 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);
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.