很多人都遇到过这样的情况,当网站达到一定的访问量,数据库就会成为瓶颈,进而引起阻塞。 有人认为这可能就是硬件的极限了,于是想办法增加硬件设备。而我本人认为问题的元凶可能是性能不高的sql脚本,引起了阻塞。 如果你和我有相同的看法,那我们就一起想
很多人都遇到过这样的情况,当网站达到一定的访问量,数据库就会成为瓶颈,进而引起阻塞。
有人认为这可能就是硬件的极限了,于是想办法增加硬件设备。而我本人认为问题的元凶可能是性能不高的sql脚本,引起了阻塞。
如果你和我有相同的看法,那我们就一起想办法找出问题的源头。
案例1.
某一天我被告知,我们的书城网站不能访问了,我马上查看,发现书城的有两台iis服务器均显示service unavailable,我初步断定是sqlserver数据库发生了阻塞,因为同一套程序使两台iis服务器同时当机的可能性不大。
要知道是否发生了阻塞,当然要看master库的sysprocess表,看看是否有什么进程堵住了别的进程,语句如下:
Select * from master..sysprocesses where blocked > 0
很快我发现,有一个blocked = 51 堵住了很多进程(查看blocked列可见),果然和我的判断吻合;为了进一步找出发生阻塞的语句,我用到的如下的语句
dbcc inputbuffer(51);
结果如下:
EventType Parameters EventInfo
------------------------------------------------
RPC Event 0 p_Book_content;1
从上面就可以看出是p_Book_content (是个存储过程)引起的阻塞,但是这个过程里面同时对多个表进行了操作,到底是那个语句出了问题呢?
下面我们再来进一步定位阻塞的位置:
Sp_lock
结果如下(大部分数据略)
Spid dbid objid indid type resource mode status
-------------------------------------------------------------------------------------------------
51 14 206623779 0 TAB X WAIT
52 14 0 0 DB S GRANT
53 14 0 0 DB S GRANT
。。。
。。。
。。。
现在我们来看看spid = 51 这行, mode = X 表示排它锁, status = WAIT表示正在等待(即被阻塞了),dbid = 14 是数据库的id,objid = 206623779 是被锁的对象id,我们可以通过下列函数得到数据库和表:
Select db_name(@dbid) -----》book_db
select object_name(@objid) -------》 t_book
即book_db库的t_book表被锁住了,这时候再回投仔细检查 p_Book_content 存储过程,发现只有一个语句对t_book进行了操作:
update t_book set hitcount = hitcount + 1 where bookid = @bookid
这个语句的作用是更新书本的点击次数,为什么上面这个语句会引起阻塞呢?我认为最可能的情况应该是同时访问的人过多,同时对表进行过多的update操作引起的,所以最终改用别的方式,不再实时对t_book表进行update操作,而是每次访问都先insert一条记录到一个中间表中,然后再用一个作业,每隔10分钟定时更新书本的点击次数,如此改进之后,此问题终于圆满解决了。
相关资料在book online可以找到, 关键字: sp_lock , sysprocesses , dbcc inputbuffer , db_name(), object_name()
注:原来发在csdn的,现在荒芜了,挪到这边来栽培一下。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool