我使用 Prisma 作为我的 ORM,使用 PostgreSQL 作为我的数据库,所以这个问题适用于 Prisma 和 Postgres,并且我有以下 prisma 架构:
model Employee {
id String @id @default(uuid())
registrationNumber String @db.VarChar @map("registration_number") @unique
fullName String @db.VarChar @map("full_name")
username String? @db.VarChar @map("username")
email String? @db.VarChar @map("email")
isSapUser Boolean @db.Boolean @map("is_sap_user") @default(false)
@@map("employee")
}
我的问题是:我应该为 varchar 设置一个大小,例如 @db.VarChar(20) 吗? 如果我设置了大小,数据库是否会分配更多空间?如果不这样做,数据库是否只使用每个字段占用的内容?
最佳答案
不要指定数据库列的最大长度,除非应用程序需要或者您想要在列上创建 B 树索引。
性能是一样的,PostgreSQL只分配数据实际占用的空间。
这就是 the documentation不得不说text
, character varying
和character
:
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(<em><strong>n</strong></em>)
has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in factcharacter(<em><strong>n</strong></em>)
is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.
关于database - 使用 Prisma 时是否应该指定 PostgreSQL 字段的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71396234/