MySQL字符集和copy_and_convert_MySQL
bitsCN.com
MySQL字符集和copy_and_convert
关于copy_and_convert
在对MySQL做业务压力测试的时候,我们在perf结果中发现 copy_and_convert 是一个耗费cpu的操作。这个函数的意思,就是在字符集之间做内容转换。
如果源和目标的字符集相同,就可以直接用memcpy,这显然比做字符集转换(按字节或字长拷贝更快,和节省cpu)
当整个系统是CPU瓶颈时,我们希望能够减少这种cpu消耗。
一次查询涉及的拷贝
如果我们执行一个简单的select语句,会涉及到两部分的内容:metadata和data。MySQL的返回包是能够“自解析”的,也就是说不仅有内容,还有描述信息。这些描述信息我们称为metadata。
在将这两部分信息返回给客户端的时候,就涉及到字符串拷贝,如果源字符集和目标字符集不同,就需要作转换。
字符集里面可以定义的部分
1、 表的字符集
比如我们创建如下的表
CREATE TABLE `t` (
`c` int(11) DEFAULT NULL,
`d` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
这就定义了这个表的字符集,要求存入数据按照gbk格式存储。在我们查询的时候,从表中读出的数据,就是我们上文说到的“源字符集”,在这里是gbk.
比如
Insert t values(2012, ‘南方很冷’);
2、连接字符集
在连接的时候我们可以在mysql客户端指定参数 mysql -default-character-set=gbk ,表示我们接收的内容希望是gbk编码,这个就是上文说的“目标字符集”。
一般我们会被建议:连接字符集和表字符集相同。
有的文章说防止乱码—-乱码倒是不会,实际的原因就是在内容拷贝时避免字符集转换计算。
本文要说的“但是”
但是即使这么做,在压力测试(或者你gdb)时,还是会发现会调用到 copy_and_convert。
原因有二:
首先,metadata部分,是固定的使用utf8,这个值可以从show variables like ‘character_set_system’;的结果看到,hardcode,配置无法修改。
其次,虽然我们定义表字符集是gbk, 但是整型字段是是Latin1存储,
#define my_charset_numeric my_charset_latin1,配置无法修改。
因此在我们上面的设置中,一次查询,在传回metadata的时候需要utf8->gbk, 返回结果中c这个字段需要latin1->gbk.
问题和建议
从上面分析知道,这存在的问题就是,无论用户怎么设置,都无法完全避免这种转换。
由于数字类型转成字符串后,使用什么字符集都占用一样的空间。
1) 在代码上最简单的修改可以如下:
将 #define my_charset_numeric my_charset_latin1 改成
#define my_charset_numeric my_charset_utf8_general_ci
这样跟metadata部分相同,然后建表和客户端都是使用utf-8。
2) 更灵活一点是把character_set_system改成可配置,然后整型存储按照用户定义的字符集。
bitsCN.com

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

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

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



How to use Docker for container performance testing and stress testing requires specific code examples. Introduction The rise of container virtualization technology has made the deployment and operation of applications more flexible and efficient. One of the most popular tools is Docker. As a lightweight containerization platform, Docker provides a convenient way to package, distribute and run applications, but how to test and evaluate the performance of containers, especially stress testing under high load conditions, It is a question that many people are concerned about. This article will introduce

Linux is an open source operating system that provides a wealth of network testing and stress testing tools, so Linux is a very good choice when conducting network testing and stress testing. In this article, we'll cover how to use Linux for network testing and stress testing. 1. Network testing Network testing is the process of testing network performance, which usually includes the following tests: Bandwidth test When performing network bandwidth testing, we need to measure the transmission speed of data in the network. One of the commonly used tools is iperf, which is available on Linux

Software stress testing is a basic quality assurance activity that is part of every important software testing effort. Therefore, stress testing is very important, so how to conduct stress testing? In this article, I will share with you a super practical stress testing tool - ab tool (apache bench). I hope it will be helpful to you!

PHP is a scripting language widely used in web development. It is used to develop many large-scale websites and applications. Performance optimization and stress testing are very critical during PHP application development, as this will help you ensure that the application can withstand high loads of user traffic during actual operation without performance issues or system issues. collapse. This article mainly introduces some common stress testing tools used in PHP. ApacheBenchApacheBench(ab) is a basic

How to use MTR for MySQL database stress testing? Overview: MySQLTestRun (MTR) is a testing tool officially provided by MySQL for testing the functionality and performance of MySQL databases. In addition to functional testing, MTR can also be used for database stress testing. This article will introduce how to use MTR for MySQL database stress testing and provide some code examples. Step 1: Install MTR First, we need to install the MTR tool. MTR is in the MySQL source code

MTR: Steps for database stress testing using the MySQL testing framework Introduction: With the rapid development of the Internet, database stress testing has become more and more important. Database stress testing is designed to simulate the load under actual usage and evaluate the performance and stability of the database under high concurrency. This article will introduce how to use the MySQL test framework (MySQLTestRunner, MTR for short) to conduct database stress testing and provide corresponding code examples. 1. Introduction to MTR MySQLTest

How to perform performance testing and stress testing in Go language development. With the continuous development of Internet applications, performance testing and stress testing have become an indispensable part of the software development process. In Go language development, we also need to conduct performance testing and stress testing to ensure the stability and reliability of the application. This article will introduce how to perform performance testing and stress testing in Go language development. 1. Performance testing Performance testing refers to the evaluation and testing of the load performance of the system through various means, which mainly includes the following aspects: Determining the progress of performance indicators

How to perform performance testing and stress testing on Java development projects. With the continuous development of the Internet, Java has become one of the most commonly used development languages. When conducting Java development projects, performance testing and stress testing are very important links. Through performance testing and stress testing, you can evaluate the performance of the system under specific loads, discover and solve potential performance problems, and ensure that the system has high performance and high availability. This article will introduce how to conduct performance testing and stress testing of Java development projects. 1. Performance Test Performance Test
