Home Database Mysql Tutorial 缓慢的update语句性能分析

缓慢的update语句性能分析

Jun 07, 2016 pm 02:59 PM
o update statement database

最近处理一个问题的时候,先是收到DB time升高的报警,然后查看DB time的情况发现,已经有近1000%的负载了。

最近处理一个问题的时候,先是收到DB time升高的报警,然后查看DB time的情况发现,已经有近1000%的负载了。

缓慢的update语句性能分析

带着好奇心想看看到底是什么样的一个语句导致如此的情况。

先抓取了一个awr报告,因为问题发生的时间段比较集中而且时间持续有几个小时,所以抓取了一个小时的快照。

得到的awr部分内容如下:

Cache Sizes

 BeginEnd  

Buffer Cache: 39,472M 39,472M Std Block Size: 8K

Shared Pool Size: 1,440M 1,440M Log Buffer: 14,256K

 

从下面的部分可以看出数据库其实内部的活动并不多,redo生成量不高,tps也不高。
Load Profile

 Per SecondPer Transaction

Redo size: 154,276.41 24,024.13

Logical reads: 4,864.90 757.57

Block changes: 779.75 121.42

Physical reads: 509.53 79.35

Physical writes: 359.90 56.04

User calls: 2,658.46 413.98

Parses: 837.89 130.48

Hard parses: 0.09 0.01

Sorts: 171.22 26.66

Logons: 0.47 0.07

Executes: 949.10 147.80

Transactions: 6.42  

 

而查看等待时间,发现第一个等待事件是db file sequential read,平均等待时间有近17ms,

延迟一般需要在10ms以下,或者至少100 reads/sec,在基于SAN存储缓存数据的情况下,sequential read的指标有时候会保持在2ms左右,这个只能说明SAN已经把数据转化为缓存了,倒不能说明硬盘驱动确实很快。这个地方已经超过了10ms说明IO上还是存在较大的影响。我们先放过这个问题,继续往下看。

EventWaitsTime(s)Avg Wait(ms)% Total Call TimeWait Class

db file sequential read 917,810 15,310 17 96.1 User I/O

CPU time   596   3.7  

log file sync 16,085 186 12 1.2 Commit

log file parallel write 15,466 140 9 .9 System I/O

ARCH wait on SENDREQ 374 10 27 .1 Network


而根据时间模型来看,绝大部分的DB time都在sql语句方面,所以关注sql语句就是一个很重要的部分。

Statistic NameTime (s)% of DB Time

sql execute elapsed time 15,533.43 97.47

DB CPU 596.11 3.74

connection management call elapsed time 82.89 0.52

parse time elapsed 20.22 0.13

而对于top1的sql语句让自己和吃惊,竟然是一个很简单的update.

Elapsed Time (s)CPU Time (s)ExecutionsElap per Exec (s)% Total DB TimeSQL IdSQL ModuleSQL Text

8,659 69 622 13.92 54.34   update user_test t set t.login_status='' where t.CN_TEST=:1

第一感觉就是这个语句走了全表扫描,因为一个简单的Update竟然需要花费近13秒的时间,,已经算很长的了。
当然猜测也需要验证,我们来看看awrsqrpt的结果。
发现这个报告还是蛮有意思。至于执行计划是走了唯一性索引扫描,所以执行计划的情况来看还是没有问题的。

IdOperationNameRowsBytesCost (%CPU)Time

0 UPDATE STATEMENT       1 (100)  

1   UPDATE USER_BILLING        

2     INDEX UNIQUE SCAN IDX_USER_TEST_CNMASTER 1 30 1 (0) 00:00:01

但是查看sql语句的执行统计信息,就有些奇怪了。

Stat NameStatement TotalPer Execution% Snap Total

Elapsed Time (ms) 8,659,180 13,921.51 54.34

CPU Time (ms) 69,346 111.49 11.63

Executions 622    

Buffer Gets 3,146,068 5,057.99 35.91

Disk Reads 645,229 1,037.35 70.31

Parse Calls 622 1.00 0.04

Rows 621,827 999.72  

User I/O Wait Time (ms) 8,608,075  

sql语句的执行总共持续8659s左右,然后8608s的时间在user I/O的等待上,这样下来,622次的执行其实花费的时间并不多。
 对于这个问题,自己也比较疑惑,开始怀疑是否是磁盘的IO上出现了问题。
 但是使用MegaCli查看的时候,发现不存在任何的坏块。
# MegaCli -CfgDsply -a0|grep Error
 Media Error Count: 0
 Other Error Count: 0
 Media Error Count: 0
 Other Error Count: 0
 Media Error Count: 0
 Other Error Count: 0
 Media Error Count: 0
 Other Error Count: 0
 Media Error Count: 0
 Other Error Count: 0
 Media Error Count: 0
 Other Error Count: 0
 Media Error Count: 0
这个时候的一个猜测就是可能由绑定变量的数据类型不同导致的sql性能问题。但是排查一番,发现还是没有得到自己期望的结果。
 查看输入的参数类型,都是期望中的varchar2,所以sql语句的过程中还是不会出现自己猜想的全表扫描的可能性。
  select name,datatype_string,value_string,datatype from DBA_HIST_SQLBIND where sql_id='94p345yuqh3zd' and snap_id between 58711 and 58712
 NAME                          DATATYPE_STRING VALUE_STRING                    DATATYPE
 ------------------------------ --------------- ------------------------------ ----------
 :1                            VARCHAR2(128)  xxxxxx9@test.com                        1
 :1                            VARCHAR2(128)  23234324324234                              1
对于IO的瓶颈问题,自己还是从addm中得到了自己需要的东西。
 对于磁盘吞吐量的说法,addm的报告中是这么描述的。

FINDING 6: 39% impact (6136 seconds)
 ------------------------------------
 The throughput of the I/O subsystem was significantly lower than expected.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings Jun 05, 2024 pm 10:21 PM

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.

How to install and register the btc trading app? How to install and register the btc trading app? Feb 21, 2025 pm 07:09 PM

This article will provide a detailed introduction to how to install and register a Bitcoin trading application. The Bitcoin trading app allows users to manage and trade cryptocurrencies such as Bitcoin. The article guides users through the installation and registration process step by step, including downloading applications, creating accounts, performing identity verification, and first deposit. The goal of the article is to provide beginners with clear and easy-to-understand guidelines to help them easily enter the world of Bitcoin trading.

Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Mar 12, 2025 pm 05:48 PM

This article recommends the top ten digital currency trading apps in the world, including Binance, OKX, Huobi Global, Coinbase, Kraken, Gate.io, KuCoin, Bitfinex, Gemini and Bitstamp. These platforms have their own characteristics in terms of transaction pair quantity, transaction speed, security, compliance, user experience, etc. For example, Binance is known for its high transaction speed and extensive services, while Coinbase is more suitable for novices. Choosing a platform that suits you requires comprehensive consideration of your own needs and risk tolerance. Learn about the world's mainstream digital currency trading platforms to help you conduct digital asset trading safely and efficiently.

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

See all articles