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

MySQL database migration quickly exports and imports large amounts of data

Database migration is a problem we often encounte...

Use of Linux chkconfig command

1. Command Introduction The chkconfig command is ...

Vue makes div height draggable

This article shares the specific code of Vue to r...

MySQL uses the Partition function to implement horizontal partitioning strategy

Table of contents 1 Review 2 Five strategies for ...

Solve the problem that Docker must use sudo operations

The steps are as follows 1. Create a docker group...

Solution to the automatic stop of MySQL service

This article mainly introduces the solution to th...

Summary of Mysql common benchmark commands

mysqlslap Common parameter description –auto-gene...

In-depth understanding of the use of CSS clear:both

clear:both is used to清除浮動This is the impression I...

How to run postgreSQL with docker

1. Install Docker. Reference URL: Docker Getting ...

This article teaches you how to play with CSS border

Border Style The border-style property specifies ...

vue-electron problem solution when using serialport

The error is as follows: Uncaught TypeError: Cann...

How to install Docker and configure Alibaba Cloud Image Accelerator

Docker Installation There is no need to talk abou...

How to import js configuration file on Vue server

Table of contents background accomplish Supplemen...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...