一些能体现个人水平的SQL语句_MySQL
一些能体现个人水平的SQL语句
作为一名小小的开发人员,刚入门的时候觉得很难,过了一段时间之后,发现很简单,很快就可以搞定很bug了。然而这并不能说明你就已经很牛掰了,只能说,你不了解其他太多的东西。应该说,数据库有几个共同的命令,select , update , insert, replace, delete , truncate, drop,只要你学会了,你就感觉你可以为所欲为了。但是在这里,我要总结一些体现水平的语句,而这些东西恰好直接体现你个人的水平和经验问题。
1、show processlist : 查看mysql的进程情况,在网站很慢的时候,你应该要想到是不是有很多死的进程或者很耗时的进程,如果确实是这样,那你应该结束到一些。 kill 99 .
执行状态分析:
Sleep状态通常代表资源未释放,如果是通过连接池,sleep状态应该恒定在一定数量范围内,实战范例:因前端数据输出时(特别是输出到用户终端)未及时关闭数据库连接,导致因网络连接速度产生大量sleep连接,在网速出现异常时,数据库too many connections挂死 Waiting for net, reading from net, writing to net
偶尔出现无妨,如大量出现,迅速检查数据库到前端的网络连接状态和流量
案例:因外挂程序,内网数据库大量读取,内网使用的百兆交换迅速爆满,导致大量连接阻塞在waiting for net,数据库连接过多崩溃Locked状态,有更新操作锁定,通常使用innodb可以很好的减少locked状态的产生,但是切记,更新操作要正确使用索引,即便是低频次更新操作也不能疏忽。如上影响结果集范例所示。在myisam的时代,locked是很多高并发应用的噩梦。所以mysql官方也开始倾向于推荐innodb。
Copy to tmp table
索引及现有结构无法涵盖查询条件,才会建立一个临时表来满足查询要求,产生巨大的恐怖的i/o压力。很可怕的搜索语句会导致这样的情况,如果是数据分析,或者半夜的周期数据清理任务,偶尔出现,可以允许。频繁出现务必优化之。
Copy to tmp table通常与连表查询有关,建议逐渐习惯不使用连表查询。
实战范例:
u 某社区数据库阻塞,求救,经查,其服务器存在多个数据库应用和网站,其中一个不常用的小网站数据库产生了一个恐怖的copy to tmp table操作,导致整个硬盘i/o和cpu压力超载。Kill掉该操作一切恢复。
Sending data
Sending data并不是发送数据,别被这个名字所欺骗,这是从物理磁盘获取数据的进程,如果你的影响结果集较多,那么就需要从不同的磁盘碎片去抽取数据,偶尔出现该状态连接无碍。回到上面影响结果集的问题,一般而言,如果sending data连接过多,通常是某查询的影响结果集过大,也就是查询的索引项不够优化。
如果出现大量相似的SQL语句出现在show proesslist列表中,并且都处于sending data状态,优化查询索引,记住用影响结果集的思路去思考。
Storing result to query cache出现这种状态,如果频繁出现,使用setprofiling分析,如果存在资源开销在SQL整体开销的比例过大(即便是非常小的开销,看比例),则说明query cache碎片较多,使用flush query cache可即时清理,也可以做成定时任务
Query cache参数可适当酌情设置。
Freeing items
理论上这玩意不会出现很多。偶尔出现无碍,如果大量出现,内存,硬盘可能已经出现问题。比如硬盘满或损坏。
i/o压力过大时,也可能出现Free items执行时间较长的情况。
Sorting for 和Sending data类似,结果集过大,排序条件没有索引化,需要在内存里排序,甚至需要创建临时结构排序。
其他还有很多状态,遇到了,去查查资料。基本上我们遇到其他状态的阻塞较少,所以不关心
2、checksum 在逻辑备份时候前后是否可以用于验证数据一致性;
load data [local] infile $filename INTO TABLE table_name TERMINATED BY ',',从文件导入数据到数据库中。
指定Windows 路径名时,使用的是斜线而不是反斜线。如果要用反斜线,必须双写。
出于安全的原因,当读取位于服务器上的文本文件时,文件必须位于数据库目录下或者可以被所有用户读取。也就是说,当对服务器上的文件执行LOAD DATA INFILE 时,用户必须获得FILE 权限。
3、索引: 不必要的索引只会占用空间和时间,建立必要索引,唯一索引,主键索引。
4、联表操作:更新,updatetable1 t1 left join table2 t2 on t1.uid=t2.uid set t1.name=t2.name,t1.desc='xxxx' where t1.date='2015-10-11';删除:delete t1,t2,t3from table1 t1 left join table2 t2 on t1.uid=t2.uid inner join table3 t3 ont2.uid = t3.uid where t1.date='2015-10-11';事务:set@@autocommit=0;

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



1. First, open the enterprise WeChat software you downloaded on your mobile phone. When logging in, there are two ways to choose: one is to use WeChat ID, the other is to use mobile phone number. 3. At this time, the enterprise administrator needs to add your mobile phone number in the background, and then Enterprise WeChat will identify the enterprise based on your mobile phone number. Then display your business and click the Enter Business option below. 4. Then you can enter the use of functions in the software. It can be said that the most important thing is that your mobile phone number must be added to the enterprise by the administrator, otherwise it will not be available.

Many gamers are very curious about the graphics card 3060 and want to buy a 3060 display, but they don’t know what level the RTX3060 is at. In fact, this graphics card is also at a relatively mainstream level. What is the level of 3060 graphics card: Answer: Mainstream level. This RTX3060 graphics card is very popular among players and is also on sale. It has the unique advantages of the RTX30 series and is very cost-effective. Players with higher requirements for game graphics can choose this graphics card. 3060 graphics card expansion introduction: Frame rate: It is loved by the majority of players. It has a large video memory of 12GB and is very popular. With 12GB of large video memory, it can run AAA masterpieces smoothly without any lag, and the frame rate can even reach 60. Value for money: it

As a new star in the mobile phone market this year, Kirin 9000s has attracted much attention. As competition in the smartphone market becomes increasingly fierce, as Huawei's flagship processor, the Kirin series has always attracted much attention. As the latest processor, how does the Kirin 9000s perform? What is its performance and level? The following will evaluate the Kirin 9000s and analyze its advantages and disadvantages from various aspects. First of all, from the perspective of performance, Kirin 9000s is manufactured using an advanced 5nm process and integrates ARM’s latest Co

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

Horizontally scrollable sections are a common web design pattern used to display content that exceeds the width of the viewport. This design pattern allows users to scroll horizontally, providing a unique and engaging way to display large images, galleries, timelines, maps, and other content. This is achieved by using CSS properties such as overflow−x:auto or overflow−x:scroll. This uses native browser functionality for horizontal scrolling and is responsive across devices. Allows easy navigation and exploration of content. It does not require any additional libraries or plugins. The algorithm uses the "container" class to define container elements. Set the container's "overflow−x" property to "auto" to enable horizontal scrolling. container

Python is a widely used high-level programming language. It is easy to learn, efficient and flexible, and is deeply loved by developers. In Python, flow control statements are an important part of implementing program logic. This article will introduce commonly used flow control statements in Python and provide code examples to deepen understanding. In Python, common flow control statements include conditional statements and loop statements. Conditional statements execute different code blocks based on the true or false condition and are used to determine and select execution branches. The loop statement is used to repeat

How to implement the statement of inserting data in MySQL? When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples. The INSERT statement in MySQL is used to insert new records into the database table. Its basic syntax format is as follows: INSERTINTOt

How to implement the statement to unlock the table in MySQL? In MySQL, table locks are a commonly used locking mechanism used to protect data integrity and consistency. When a transaction is reading and writing to a table, other transactions cannot modify the table. This locking mechanism ensures data consistency to a certain extent, but may also cause other transactions to be blocked. Therefore, if a transaction cannot continue for some reason, we need to manually unlock the table so that other transactions can continue. MySQL provides a variety of
