🧚 주목! Beekeeper Studio는 빠르고 현대적이며 오픈 소스 데이터베이스 GUI입니다 다운로드
July 15, 2024 작성자: Matthew Rathbone

Killing a Query

Before you can kill a query, you need to identify which queries are causing problems. Use the following SQL to list all running queries along with their duration:

SELECT
    pid,
    user_name,
    starttime,
    query,
    elapsed
FROM
    stv_recents
WHERE
    state = 'Running'
ORDER BY
    elapsed DESC;

Expected Output:

pid user_name starttime query elapsed
123 admin 2023-10-01 10:00:00 SELECT * FROM ? 01:20:15
456 analyst 2023-10-01 10:45:00 UPDATE table ? 00:35:10

The elapsed column shows how long the query has been running.

Once you’ve identified the problematic query (using the pid from the list above), you can terminate it. Use the following command:

CANCEL pid;

Example:

CANCEL 123;

Verifying the Query Termination

To ensure the query has been terminated, re-run the initial query:

SELECT
    pid,
    user_name,
    starttime,
    query,
    elapsed
FROM
    stv_recents
WHERE
    state = 'Running'
ORDER BY
    elapsed DESC;

The terminated query should no longer appear in the results.

Handling Blocked Queries

Sometimes, queries get blocked and need to be killed to release the resources. You can identify blocked queries using the STV_BLOCKED table.

SELECT
    b.pid,
    b.user_name,
    b.db,
    b.query,
    l.pid AS blocked_by_pid,
    l.user_name AS blocked_by_user
FROM
    stv_blocked b
JOIN
    stv_locks l
ON
    b.lock_owner_pid = l.pid;

Expected Output:

pid user_name db query blocked_by_pid blocked_by_user
5678 analytics reporting SELECT count(*) FROM ?; 9101 Admin

To kill a blocked query, use the CANCEL command as shown earlier:

CANCEL 5678;

Automating Query Termination

For databases with stringent performance requirements, automating the detection and termination of long-running queries can be beneficial. Below is an example procedure for terminating queries running longer than a specified threshold:

DO $$ 
DECLARE
    long_running_query RECORD;
BEGIN
    FOR long_running_query IN
        SELECT pid
        FROM stv_recents
        WHERE state = 'Running'
        AND elapsed > interval '00:30:00' -- Replace with desired threshold
    LOOP
        EXECUTE 'CANCEL ' || long_running_query.pid;
    END LOOP;
END;
$$;

Important Considerations

  • Permissions: Only users with appropriate permissions can cancel queries.
  • Impact: Terminating a query can affect application performance or cause data inconsistency if performed incorrectly.
  • Monitoring: Regular monitoring of query performance helps in early detection of issues.

Conclusion

Managing long-running queries effectively can ensure optimal performance of your Amazon Redshift cluster. By following these steps, you can quickly identify and terminate any problematic queries. Implementing automated solutions for regular monitoring and query termination can further help maintain performance.

Ensure to test all commands and scripts in a non-production environment before deploying them to production to avoid unintentional disruptions.

Other posts you may like:

Beekeeper Studio는 무료 & 오픈 소스 데이터베이스 GUI입니다

제가 사용해 본 최고의 SQL 쿼리 & 편집기 도구입니다. 데이터베이스 관리에 필요한 모든 것을 제공합니다. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio는 빠르고 직관적이며 사용하기 쉽습니다. Beekeeper는 많은 데이터베이스를 지원하며 Windows, Mac, Linux에서 훌륭하게 작동합니다.

Beekeeper의 Linux 버전은 100% 완전한 기능을 갖추고 있으며, 기능 타협이 없습니다.

사용자들이 Beekeeper Studio에 대해 말하는 것

★★★★★
"Beekeeper Studio는 제 예전 SQL 워크플로를 완전히 대체했습니다. 빠르고 직관적이며 데이터베이스 작업을 다시 즐겁게 만들어 줍니다."
— Alex K., 데이터베이스 개발자
★★★★★
"많은 데이터베이스 GUI를 사용해 봤지만, Beekeeper는 기능과 단순함 사이의 완벽한 균형을 찾았습니다. 그냥 작동합니다."
— Sarah M., 풀스택 엔지니어

SQL 워크플로를 개선할 준비가 되셨나요?

download 무료 다운로드