Home Database Mysql Tutorial sql update更新不同字段类型性能分析

sql update更新不同字段类型性能分析

Jun 07, 2016 pm 05:51 PM
Performance analysis

MySQL在使用update更新数据时,如果条件字段的类型为数字型,但参数是字符型的而且该条件又匹配不到记录,就会引起严重的性能问题

。如下:

 代码如下 复制代码
1,update test007 set key1 = key1 + '1' where id = 200000;
2,update test007 set key1 = key1 + '1' where id = '200000';

注意上面查询语句区别在于参数的类型不同,前者为数字型,后者为字符型,同时id为200000这条记录是不存在的。

如果使用第二条查询,而且满足记录不存在,这条查询将出现严重的效率问题,测试情况如下:

二,测试实践
1,创建一张测试数据表test007

 代码如下 复制代码
CREATE TABLE `test007` (                                  
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,          
`key1` int(10) NOT NULL DEFAULT '0',                 
`key2` int(10) NOT NULL DEFAULT '0',                 
`key3` int(10) NOT NULL DEFAULT '0',                    
PRIMARY KEY (`id`)                                                        
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=gbk

2,创建测试数据

 代码如下 复制代码


$db = _connect("localhost","root","");
mysql__db("test");
set_time_limit(0);
$table = 'test007';
for($i=0;$i     $k1 = rand(10000,300000);
    $k2 = rand(0,3);
    $k3 = rand(1,100000);
    mysql_query("insert into $table (key1,key2,key3) values ('".$k1."','".$k2."','".$k3."')",$db);
}
?>


说明:创建1000000(100W)条记录,数据大小为16.2 MB

3,测试参数类型为数字型的情况

 代码如下 复制代码

mysql> update test007 set key1=key1+'1' where id=10000001;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

查询语句的性能情况
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000104 |
| checking permissions | 0.000005 |
| Opening tables       | 0.000010 |
| System lock          | 0.013440 |
| Table lock           | 0.000004 |
| init                 | 0.000035 |
| Updating             | 0.000020 |
| end                  | 0.000034 |
| query end            | 0.000002 |
| freeing items        | 0.000028 |
| logging slow query   | 0.000001 |
| cleaning up          | 0.000005 |
+----------------------+----------+
12 rows in set (0.00 sec)

说明:主键id的字段类型为数字型

4,测试参数类型为字符型的情况

 代码如下 复制代码

mysql> update test007 set key1=key1+'1' where id='100000001';
Query OK, 0 rows affected (0.03 sec)
Rows matched: 0  Changed: 0  Warnings: 0

查询语句的性能情况
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000108 |
| checking permissions | 0.000005 |
| Opening tables       | 0.029382 |
| System lock          | 0.000003 |
| Table lock           | 0.000003 |
| init                 | 0.000039 |
| Updating             | 0.000074 |
| end                  | 0.000022 |
| query end            | 0.000002 |
| freeing items        | 0.000033 |
| logging slow query   | 0.000001 |
| cleaning up          | 0.000001 |
+----------------------+----------+
12 rows in set (0.00 sec)

在使用UPDATE更新记录时,如果被更新的字段的类型和所赋的值不匹配时,MySQL将这个值转换为相应类型的值。如果这个字段是数值类型,而且所赋值超 过了这个数据类型的最大范围,那么MySQL就将这个值转换为这个范围最大或最小值。如果字符串太长,MySQL就将多余的字符串截去。如果设置非空字段 为空,那么将这个字段设置为它们的默认值,数字的默认值是0,字符串的默认值是空串(不是null,是"")。

由于测试环境数据量比较小,所以测试的结果不明显,但关键是在开发过程中一定要注意字段类型与参数类型的一致性,避免在特定情况下造成数据在更新和删除过程中的额外开销。

5,测试大数据量的情况,过程如下

 代码如下 复制代码

第一步:创建数据表
CREATE TABLE `test008` (                                  
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,          
`key1` int(10) NOT NULL DEFAULT '0',                 
`key2` text,                 
`key3` int(10) NOT NULL DEFAULT '0',                    
PRIMARY KEY (`id`)                                                        
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=gbk

第二步:创建测试数据
创建1000000(100W)条记录,数据大小为2.07 GB (2,224,000,000 字节)

第三步:两条查询性能比较
mysql> update test008 set key1=key1+'1' where id='100000001';
Query OK, 0 rows affected (0.03 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> update test008 set key1=key1+'1' where id=100000001;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

第四步:创建索引
mysql> alter table test008 add index key3 (key3);
Query OK, 1000000 rows affected (5 min 54.33 sec)
Records: 1000000  Duplicates: 0  Warnings: 0

第五步:测试不同的条件
mysql> update test008 set key1 = key1 + '1' where id='';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> update test008 set key1 = key1 + '1' where id='12321232123';
Query OK, 0 rows affected (44.58 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> update test008 set key1 = key1 + '1' where id=12321232123;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> update test008 set key1= key1+ '1' where id='test';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

注意:上面测试中部分条件已经超出id字段的范围

 

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Performance analysis of Kirin 8000 and Snapdragon processors: detailed comparison of strengths and weaknesses Performance analysis of Kirin 8000 and Snapdragon processors: detailed comparison of strengths and weaknesses Mar 24, 2024 pm 06:09 PM

Kirin 8000 and Snapdragon processor performance analysis: detailed comparison of strengths and weaknesses. With the popularity of smartphones and their increasing functionality, processors, as the core components of mobile phones, have also attracted much attention. One of the most common and excellent processor brands currently on the market is Huawei's Kirin series and Qualcomm's Snapdragon series. This article will focus on the performance analysis of Kirin 8000 and Snapdragon processors, and explore the comparison of the strengths and weaknesses of the two in various aspects. First, let’s take a look at the Kirin 8000 processor. As Huawei’s latest flagship processor, Kirin 8000

Performance comparison: speed and efficiency of Go language and C language Performance comparison: speed and efficiency of Go language and C language Mar 10, 2024 pm 02:30 PM

Performance comparison: speed and efficiency of Go language and C language In the field of computer programming, performance has always been an important indicator that developers pay attention to. When choosing a programming language, developers usually focus on its speed and efficiency. Go language and C language, as two popular programming languages, are widely used for system-level programming and high-performance applications. This article will compare the performance of Go language and C language in terms of speed and efficiency, and demonstrate the differences between them through specific code examples. First, let's take a look at the overview of Go language and C language. Go language is developed by G

How to use the php extension XDebug for powerful debugging and performance analysis How to use the php extension XDebug for powerful debugging and performance analysis Jul 28, 2023 pm 07:45 PM

How to use the PHP extension Xdebug for powerful debugging and performance analysis Introduction: In the process of developing PHP applications, debugging and performance analysis are essential links. Xdebug is a powerful debugging tool commonly used by PHP developers. It provides a series of advanced functions, such as breakpoint debugging, variable tracking, performance analysis, etc. This article will introduce how to use Xdebug for powerful debugging and performance analysis, as well as some practical tips and precautions. 1. Install Xdebug and start using Xdebu

How to perform performance analysis of C++ code? How to perform performance analysis of C++ code? Nov 02, 2023 pm 02:36 PM

How to perform performance analysis of C++ code? Performance is an important consideration when developing C++ programs. Optimizing the performance of your code can improve the speed and efficiency of your program. However, to optimize your code, you first need to understand where its performance bottlenecks are. To find the performance bottleneck, you first need to perform code performance analysis. This article will introduce some commonly used C++ code performance analysis tools and techniques to help developers find performance bottlenecks in the code for optimization. Profiling tool using Profiling tool

Tools and techniques for code optimization and performance analysis in JavaScript Tools and techniques for code optimization and performance analysis in JavaScript Jun 16, 2023 pm 12:34 PM

With the rapid development of Internet technology, JavaScript, as a widely used front-end language, is receiving more and more attention. However, when processing large amounts of data or complex logic, JavaScript performance will be affected. In order to solve this problem, we need to master some code optimization and performance analysis tools and techniques. This article will introduce you to some commonly used JavaScript code optimization and performance analysis tools and techniques. 1. Code optimization to avoid global variables: global variables will occupy more

How to use performance analysis tools to analyze and optimize Java functions? How to use performance analysis tools to analyze and optimize Java functions? Apr 29, 2024 pm 03:15 PM

Java performance analysis tools can be used to analyze and optimize the performance of Java functions. Choose performance analysis tools: JVisualVM, VisualVM, JavaFlightRecorder (JFR), etc. Configure performance analysis tools: set sampling rate, enable events. Execute the function and collect data: Execute the function after enabling the profiling tool. Analyze performance data: identify bottleneck indicators such as CPU usage, memory usage, execution time, hot spots, etc. Optimize functions: Use optimization algorithms, refactor code, use caching and other technologies to improve efficiency.

Analysis and optimization strategies for Java Queue queue performance Analysis and optimization strategies for Java Queue queue performance Jan 09, 2024 pm 05:02 PM

Performance Analysis and Optimization Strategy of JavaQueue Queue Summary: Queue (Queue) is one of the commonly used data structures in Java and is widely used in various scenarios. This article will discuss the performance issues of JavaQueue queues from two aspects: performance analysis and optimization strategies, and give specific code examples. Introduction Queue is a first-in-first-out (FIFO) data structure that can be used to implement producer-consumer mode, thread pool task queue and other scenarios. Java provides a variety of queue implementations, such as Arr

C++ development advice: How to perform performance analysis of C++ code C++ development advice: How to perform performance analysis of C++ code Nov 22, 2023 pm 08:25 PM

As a C++ developer, performance optimization is one of our inevitable tasks. In order to improve the execution efficiency and response speed of the code, we need to understand the performance analysis methods of C++ code in order to better debug and optimize the code. In this article, we will introduce you to some commonly used C++ code performance analysis tools and techniques. Compilation options The C++ compiler provides some compilation options that can be used to optimize the execution efficiency of the code. Among them, the most commonly used option is -O, which tells the compiler to optimize the code. Normally, we would set

See all articles