日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql tinyint和char(1)性能对比

發布時間:2025/3/20 数据库 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql tinyint和char(1)性能对比 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在數據庫設計的時候會遇到很多只需要0、1、2這種固定幾個值的狀態字段,基本上都建議設置為只占一字節的tinyint類型,有些覺得char(1)是一樣,畢竟char(1)存儲數字和字母時一個字符也只是占一個字節


mysql是用c++寫的,而在c++中字符類型是存放對應ascii碼的二進制到存儲空間,而整型數字是直接存數字的二進制,雖然最終都是二進制存儲,但是環節上有少許不同,同樣在msyql查找時也會有所不同,下圖摘自小白版c++教程《c++ primer plus》:



今天對tinyint和char(1)做了個簡單測試,分表建兩個表t1、t2,結構如下:

mysql> show create table t1\G

*************************** 1. row ***************************

? ? ? ?Table: t1

Create Table: CREATE TABLE `t1` (

? `_id` int(11) NOT NULL AUTO_INCREMENT,

? `id` tinyint(4) DEFAULT NULL,

? `title` text,

? PRIMARY KEY (`_id`),

? KEY `id` (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2400096 DEFAULT CHARSET=utf8

1 row in set (0.00 sec)


mysql> show create table t2\G

*************************** 1. row ***************************

? ? ? ?Table: t2

Create Table: CREATE TABLE `t2` (

? `_id` int(11) NOT NULL AUTO_INCREMENT,

? `id` char(1) DEFAULT NULL,

? `title` text,

? PRIMARY KEY (`_id`),

? KEY `id` (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2400096 DEFAULT CHARSET=utf8

1 row in set (0.00 sec)


兩個表唯一不同為id字段類型,總數據量都為2400096,id只有0、1、2三個,為了兩個表的數據一樣且磁盤上分布也一樣,降低IO對測試的影響,分別加載的數據如下:

mysql> select id,count(*) from t1 group by id;

+------+----------+

| id ? | count(*) |

+------+----------+

| ? ?0 | ?1199998 |

| ? ?1 | ?1199998 |

| ? ?2 | ? ? ? 99 |

+------+----------+

3 rows in set (0.55 sec)


mysql> select id,count(*) from t2 group by id;?

+------+----------+

| id ? | count(*) |

+------+----------+

| 0 ? ?| ?1199998 |

| 1 ? ?| ?1199998 |

| 2 ? ?| ? ? ? 99 |

+------+----------+

3 rows in set (0.77 sec)


查看執行計劃:

mysql> explain select _id from test.t2 where id='1'; ? ? ? ? ? ? ? ? ? ? ?

+----+-------------+-------+------+---------------+------+---------+-------+---------+--------------------------+

| id | select_type | table | type | possible_keys | key ?| key_len | ref ? | rows ? ?| Extra ? ? ? ? ? ? ? ? ? ?|

+----+-------------+-------+------+---------------+------+---------+-------+---------+--------------------------+

| ?1 | SIMPLE ? ? ?| t2 ? ?| ref ?| id ? ? ? ? ? ?| id ? | 4 ? ? ? | const | 1170900 | Using where; Using index |

+----+-------------+-------+------+---------------+------+---------+-------+---------+--------------------------+

1 row in set (0.00 sec)


mysql> explain select _id from test.t1 where id=1;?

+----+-------------+-------+------+---------------+------+---------+-------+---------+-------------+

| id | select_type | table | type | possible_keys | key ?| key_len | ref ? | rows ? ?| Extra ? ? ? |

+----+-------------+-------+------+---------------+------+---------+-------+---------+-------------+

| ?1 | SIMPLE ? ? ?| t1 ? ?| ref ?| id ? ? ? ? ? ?| id ? | 2 ? ? ? | const | 1170601 | Using index |

+----+-------------+-------+------+---------------+------+---------+-------+---------+-------------+

1 row in set (0.00 sec)


兩個表都使用了id索引,再看看information_schema.tables的信息是否和之前理解的存儲字節大小是否有出入:

mysql> select DATA_LENGTH/1024/1024,INDEX_LENGTH/1024/1024,data_free from tables where table_name in ('t1','t2');

+-----------------------+------------------------+-----------+

| DATA_LENGTH/1024/1024 | INDEX_LENGTH/1024/1024 | data_free |

+-----------------------+------------------------+-----------+

| ? ? ? ? ?310.81250000 | ? ? ? ? ? ?27.56250000 | ? ? ? ? 0 |

| ? ? ? ? ?313.81250000 | ? ? ? ? ? ?29.56250000 | ? ? ? ? 0 |

+-----------------------+------------------------+-----------+

2 rows in set (0.00 sec)


兩個表大小相差不多,確認char(1)和tinyint占字節數相同,現在直接看執行時間:

mysql> show profiles;

+----------+------------+---------------------------------------------------------------+

| Query_ID | Duration ? | Query ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |

+----------+------------+---------------------------------------------------------------+

| ? ? ? ?1 | 0.60804275 | select count(*) from (select _id from test.t1 where id=1) a ? |

| ? ? ? ?2 | 0.59277575 | select count(*) from (select _id from test.t1 where id=1) a ? |

| ? ? ? ?3 | 0.60398000 | select count(*) from (select _id from test.t1 where id=1) a ? |

| ? ? ? ?4 | 0.69068025 | select count(*) from (select _id from test.t2 where id='1') a |

| ? ? ? ?5 | 0.69654200 | select count(*) from (select _id from test.t2 where id='1') a |

| ? ? ? ?6 | 0.67788800 | select count(*) from (select _id from test.t2 where id='1') a |

+----------+------------+---------------------------------------------------------------+


這樣就很明顯可以看出為char(1)字段的t2表查詢時消耗時間偏多,如果幾條幾百條的情況根本看不出char(1)和tinyint的差別,畢竟現在CPU的效率是非常高的,這里測試的利用了id=1的數據,有1199998條,這樣就可以看出點差別了!!雖然效率差別不是很大,為了生產環境統一以及提升QPS還是使用短小的整型更好


轉載于:https://blog.51cto.com/xiaozhong991/1892569

總結

以上是生活随笔為你收集整理的mysql tinyint和char(1)性能对比的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。