Dangers of slow query in MySQL Databases

Source: Internet
Author: User

Dangers of slow query in MySQL Databases

1. When a slow query occurs in a MySQL database, it is dangerous. Other DDL operations may cause the entire database to wait.

It can be divided into the following situations:

When a table is a MyiSAM table, the table has slow queries and does not block the Select statement. Other DML and DDL operations on the table are blocked, such as the appearance of Wating for table level lock, the MyiSAM table cannot exist in the database.

When the table is an Innodb table and the table has slow queries without blocking Select and DML, other DDL operations will be blocked, such as waiting for table metadata lock

In summary, when there is a slow query in the database, it is quite dangerous. When performing backup, create index, alter table, flush table, and other operations, it will cause the database to wait.

Solution:

1. Monitor Select statements that have been executed for a long time in the database, and trigger alarms in a timely manner.

2. if allowed, write the script and find a long select statement, kill it directly, and record the log

-B, -- batch Don't use history file. Disable interactive behavior.

-S, -- silent Be more silent. Print results with a tab as separator, each row on new line.

-E, -- execute = name Execute command and quit. (Disables -- force and historyfile .)

# If the database currently has a large number of select statements, you can filter them out and only kill the waiting

Cat killWaitSession. sh

#! /Bin/bash
For I in 'mysql-BSE' show full processlist '| grep-I select | grep-I "Waiting | awk' {print $1 }''
Do
Mysql-Bse "kill $ I"
Done

The show processlist command has many statuses, and Query indicates the command being executed.

Query: The thread is executing a statement.

Cat killLongQuerySession. sh

#! /Bin/bash
Executetime = ('mysql-BSE' show processlist' | grep 'query' | awk '{print $6 "" $1}' | sort-rn | head-1 ') # The first column is the run time and the first column is the session id.
Time =$ {executetime [0]}
Id =$ {executetime [1]}
While:
Do
Maxtime= 300
If [$ time-gt $ maxtime]; then
Echo $ time $ id>/tmp/killqueryid. log
Mysql-Bse "kill $ id"
# Else
# Echo $ time $ id
Fi
Sleep 10 # sleep for 10 s
Done

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151304.htm

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.