pg varchar和varchar(N)对性能有影响吗?

舍得先森V 2020-05-12 08:42:28
在不知道业务字段长度的情况下,将字段类型设置为varchar,和varchar(N)相比,有什么区别,对查询的性能有影响吗?
...全文
681 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
简即美 2020-06-06
  • 打赏
  • 举报
回复 1
varchar(N)最大的意义不在于性能在于约束实现一下功能 1.约束数据质量,比如身份证号字段 2.避免某个情绪不稳定程序员或者用户因为某种神乎其技的操作,往user表的name列每一行的插一本《战争与和平》进去。
linggx 2020-06-04
  • 打赏
  • 举报
回复
这三种类型之间没有性能差异,除了使用空白填充类型时增加了存储空间,以及在存储到长度限制的列时检查长度时增加了一些额外的CPU周期。虽然字符(n)在其他一些数据库系统中具有性能优势,但在PostgreSQL中却没有这种优势;事实上,字符(n)通常是这三种方法中最慢的,因为它需要额外的存储成本。在大多数情况下,应该使用文本或字符变化。
linggx 2020-06-04
  • 打赏
  • 举报
回复
https://www.postgresql.org/docs/current/datatype-character.html varchar性能更好: Tip There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.
foucus、 2020-05-13
  • 打赏
  • 举报
回复
varchar表示接收任何长度的字符串,在pg中和text类型类似,最大存储1G的数据

954

社区成员

发帖
与我相关
我的任务
社区描述
PostgreSQL相关内容讨论
sql数据库数据库架构 技术论坛(原bbs)
社区管理员
  • PostgreSQL社区
  • yang_z_1
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧