有人有充分的证据证明 CHAR 比 VARCHAR 更快吗?

发布于 2024-09-13 13:45:52 字数 136 浏览 4 评论 0原文

有任何基准、图表吗?整个网络都是学术和理论的。

好吧,这不是第一次问这个问题了,他们都说使用 CHAR 会导致更快的选择?我什至读过 MySQL 书籍,都是一样的,但我还没有遇到任何基准证明这一点。

任何人都可以阐明这一点吗?

Any benchmark, graph anything at all ? Its all academic and theoretical across the web.

Ok its not the first time that this question has been asked, they all say that using CHAR results in faster selects? I even read in MySQL books, its all the same but I have not come across any benchmark that proves this.

Can any one shed some light over this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫以 2024-09-20 13:45:52

这是简单的逻辑,为了简化,我将以 CSV 文件为例...

在这一行中搜索是否会更快

1231;231;32345;21312;23435552;1231;1;243;211;3525321;44343112 ;

或者这个

12;23;43;54;56;76;54;83;45;91;28;92

只要你正确定义你的长度 CHAR 应该更快,因为预定义的格式有助于缩短处理时间。

This is simple logic, to simplify I'll take the example of a CSV file...

would it be faster to search in this line

1231;231;32345;21312;23435552;1231;1;243;211;3525321;44343112;

or this one

12;23;43;54;56;76;54;83;45;91;28;92

as long as you define your length correctly CHAR should be faster as the predefined format help the processing time.

時窥 2024-09-20 13:45:52

重点是,事实并非如此。无论如何,它本身并不是。

然而,事实是,如果表中固定宽度字段,MySQL 不需要执行一些计算来找出每个字段的开头。

对于非常短的字段也可能存在差异。如果比较 CHAR(1) 和 VARCHAR(1),后者占用的内存是前者的两倍(在单字节编码中)

The point is, it is not. Not by itself anyway.

What is true however, is that if there are only fixed width fields in the table, MySQL does not need to perform some calculations to find out the beginnings of each field.

Also there might be a difference for very short fields. If you compare CHAR(1) vs VARCHAR(1), the latter takes twice as much memory as the first (in single byte encodings)

好听的两个字的网名 2024-09-20 13:45:52

CHAR 会更快,因为它是固定长度。
例如 CHAR(10) 和 VARCHAR(10)
CHAR(10) 是固定长度的字符串,长度为 10,而 VARCHAR 是可变长度的字符串,最大长度为 10。

VARCHAR(10)

 {Index} (Length) [String]
 -------------------------
 {0} (8) [AAAAAAAA]
 {1} (5) [BBBBB]
 {2} (3) [CCC]
 {3} (7) [DDDDDDD]
 {4} (2) [EE]
 {5} (4) [FFFF]

CHAR(10)

 {Index} (Length) [String]
 -------------------------
 {0} (10) [AAAAAAAA  ]
 {1} (10) [BBBBB     ]
 {2} (10) [CCC       ]
 {3} (10) [DDDDDDD   ]
 {4} (10) [EE        ]
 {5} (10) [FFFF      ]

所以想象一下您有一个包含 1,000,000 条记录的表,并且需要在偏移量 500,000 处获取一条记录。

CHAR - 数据库引擎必须乘以 500,000 x 10 = 偏移量为 5,000,000。

VARCHAR - 数据库引擎必须获取每行长度并将它们全部相加5 + 8 + 9 + 3 + 2 + 4... 500,000 次才能获得偏移

CHAR 填充是 x00 基本上是字符串的结尾所以
CHAR(10) = [A,A,A,A,A,00,00,00,00,00]“AAAAA”

CHAR will be faster as it is fixed length.
For example CHAR(10) and VARCHAR(10)
CHAR(10) is a fixed-length string of 10 while VARCHAR is a variable-length string with maximum length of 10.

VARCHAR(10)

 {Index} (Length) [String]
 -------------------------
 {0} (8) [AAAAAAAA]
 {1} (5) [BBBBB]
 {2} (3) [CCC]
 {3} (7) [DDDDDDD]
 {4} (2) [EE]
 {5} (4) [FFFF]

CHAR(10)

 {Index} (Length) [String]
 -------------------------
 {0} (10) [AAAAAAAA  ]
 {1} (10) [BBBBB     ]
 {2} (10) [CCC       ]
 {3} (10) [DDDDDDD   ]
 {4} (10) [EE        ]
 {5} (10) [FFFF      ]

So imagine you have a table with 1,000,000 records and you need to get a record at offset 500,000.

CHAR - database engine would have to do multiply 500,000 x 10 = offset is 5,000,000.

VARCHAR - database engine would have to get each row length and sum them all 5 + 8 + 9 + 3 + 2 + 4... 500,000 times to get offset

CHAR padding is x00 which basically is end of the string so
CHAR(10) = [A,A,A,A,A,00,00,00,00,00] is "AAAAA".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文