MySQL Management long running queries

Source: Internet
Author: User
Tags sql injection stmt

1. Reasons for a long-time executed query

Long-time queries due to poor SQL execution efficiency:

Long queries caused by SQL injection:

Because DDL statements cause table metadata lock waits:


2. Problems caused by long-time queries

In general, queries that are executed for long periods of time are not meaningful to the application unless they are bi/report class queries.

Consuming system resources, such as a large number of long queries, can cause problems such as excessive CPU, IOPS, and/or high usage of connections.

The potential for system instability (such as long-time queries on InnoDB engine tables may result in an increase in file size for ibdata1 systems)


3. How to avoid long-run queries

Applications should be aware of increased protection against SQL injection.

Before the new feature module goes live, stress tests are performed to avoid the execution of a poorly executed SQL.

Index creation deletes, table structure modifications, table maintenance, and table deletions are done as much as possible during low-peak business periods.


4. How to handle queries that are executed for a long time

A, through the order show processlist; View the current execution session and the kill session for a long time query.

B, create an event to automatically clean up queries that have been executed for a long time

create event my_long_running_query_monitoron schedule every 5 minutestarts  ' 2018-08-08 11:00:00 ' on completion preserve enable dobegin  declare v_ Sql varchar (+);  declare no_more_long_running_query integer default 0;   declare c_tid cursor for    select concat  (' Kill   ', id, '; ')  from     information_schema.processlist    where time  >= 3600    and user = substring (Current_User (), 1,instr ( Current_User (), ' @ ')-1)     and command not in  (' Sleep ')      and state not like  (' Waiting for table%lock ');   declare  continue handler for not found    set no_more_long_running_ Query=1;   open c_tid;  repeat    fetch c_tid into v_sql;     set  @v_sql =v_sql;    prepare stmt from  @v_sql;     execute stmt;    deallocate prepare stmt;   until no_more_long_running_query end repeat;  close c_tid;end;


MySQL Management long running queries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.