在 SQLite 中读取固定宽度列是否更快?
从没有存储 varchar 或其他可变长度数据的表中读取通常会更快吗?在 MySQL 中,这更快,因为它可以准确计算行将存储在磁盘上的位置。
Would it generally be faster to read from a table where there are no varchar or other variable length data stored? In MySQL, this is faster because it can calculate exactly where a row will be stored on the disk.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题在 SQLite 上下文中没有意义,因为它仅支持单个
TEXT
字段类型。这里不存在“固定宽度”与“可变长度”的区别。虽然 SQLite 允许您将字段定义为具有某种类型,但所做的一切(至多)只是设置该字段对存储不明确数据时要使用的类型的偏好(例如,“3”是否将存储为 INTEGER、REAL、或文本)。您仍然可以在任何 SQLite 字段中存储任何类型的数据,无论其类型如何。
具体到 CHAR 与 VARCHAR,http://www.sqlite.org/datatype3.html 告诉我们:
This question is not meaningful in the context of SQLite as it supports only a single
TEXT
field type. The distinction of "fixed-width" vs. "variable-length" doesn't exist here.While SQLite will let you define a field as having a certain type, all this does is (at most) set that field's preference for the type to use when storing ambiguous data (e.g., whether "3" will be stored as INTEGER, REAL, or TEXT). You can still store any kind of data in any SQLite field regardless of its type.
Specifically relating to CHAR vs. VARCHAR, http://www.sqlite.org/datatype3.html tells us:
由于 SQLite 仅使用可变长度记录,我猜他们确实这么做了当行恰好具有相同长度时,不实现固定宽度查找优化。
正如 Dave 指出的那样,人们仍然可以在 INT 字段中存储文本。由于 SQLite 从不截断数据,这意味着 SQLite 允许像 INT 这样看似固定宽度的列来存储变量-长度数据也是如此。所以不可能实现定宽查找优化。
Since SQLite uses variable-length records only, I would guess they did not implement fix-width lookup optimization when rows happen to have the same length.
And as Dave pointed out, one can still store text in INT fields. Since SQLite never truncates data, this means SQLite permits seemingly fixed width column like INT to store variable-length data too. So it is impossible to implement fixed-width lookup optimization.