Comparing the performance of int, char, and varchar in MySQL

Comparing the performance of int, char, and varchar in MySQL

There are many seemingly true "rumors" on the Internet. Of course, they are not malicious. Most of them are because developers are unwilling to take the initiative to research themselves, and instead believe what others say.

There are also many rumors about databases, such as "int performance is much higher than char".

I recently conducted a performance test on int, long, char, and varchar, and found that there is actually not much performance difference between them:

Note: c8=char(8), s8=varchar(8), i8=(bigint), c4=char(4), s4=varchar(4), i4=char(4)

Query 1 million rows without index:

Execute [c8 query] 20 times, average time consumption 312.0ms
Execute [s8 query] 20 times, average time consumption 334.3ms
Execute [i8 query] 20 times, average time consumption 276.95ms
Execute [c4 query] 20 times, average time consumption 354.95ms
Execute [s4 query] 20 times, average time consumption 340.45ms
Execute [i4 query] 20 times, average time taken is 291.1ms

Create an index:

c8 indexing took 2439ms
s8 indexing took 2442ms
i8 indexing took 1645ms
c4 indexing took 2296ms
s4 indexing took 2303ms
i4 indexing took 1403ms

Query with index:

Execute [c8 query] 10000 times, average time consumption 0.271ms
Execute [s8 query] 10000 times, average time consumption 0.2354ms
Execute [i8 query] 10000 times, average time consumption is 0.2189ms
Execute [c4 query] 10000 times, average time consumption 0.303ms
Execute [s4 query] 10000 times, average time consumption 0.3094ms
Execute [i4 query] 10000 times, average time taken is 0.25ms

in conclusion:

No index: Full table scan does not become faster because the data is smaller, but the overall speed is the same, int/bigint as native types is slightly faster by 12%.

With index: char and varchar have similar performance, int is slightly faster by 18%

In terms of data storage, reading and writing, integers are the same as strings of equal length, but varchar has an extra byte so performance may be slightly affected (1/n).

In terms of data operations and comparisons, integers benefit from native support and are therefore slightly faster than strings.

If indexes are used, the performance difference between integers and strings is even smaller.

In actual development, many developers often use strings such as char(1) and char(4) to represent type enumerations. In my opinion, this approach is the best solution because it is far superior to data types such as int and enum in terms of storage space, computing performance, readability, maintainability, and scalability.

You may also be interested in:
  • How to choose between MySQL CHAR and VARCHAR
  • Some things to note about varchar type in Mysql
  • Differences between MySQL CHAR and VARCHAR when storing and reading
  • The difference between char and varchar in MYSQL
  • The difference between char, varchar and text field types in MySQL
  • Mysql varchar type sum example operation
  • How to dynamically modify the length of varchar in MySQL
  • How to set the length of varchar in Mysql
  • How to convert varchar type to int type in Mysql database
  • How does mysql handle special characters in varchar and nvarchar types
  • When the interviewer asked the difference between char and varchar in mysql

<<:  WeChat applet implements the snake game

>>:  Detailed explanation of the use of nohup /dev/null 2>&1

Recommend

How to make a centos base image

Preface Now the operating system used by my compa...

Implementation of Docker deployment of web projects

The previous article has installed the docker ser...

Example code for using @media in CSS3 to achieve web page adaptation

Nowadays, the screen resolution of computer monit...

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

React implements multi-component value transfer function through conetxt

The effect of this function is similar to vue的pro...

Explanation of the steps for Tomcat to support https access

How to make tomcat support https access step: (1)...

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

Do you know the difference between empty value and null value in mysql

Preface Recently I found that my friend's met...

Example of using javascript to drag and swap div positions

1 Implementation Principle This is done using the...

How to construct a table index in MySQL

Table of contents Supports multiple types of filt...

Complete code for implementing the vue backtop component

Effect: Code: <template> <div class=&quo...

An article tells you how to write a Vue plugin

Table of contents What is a plugin Writing plugin...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which me...