database - 使用 Prisma 时是否应该指定 PostgreSQL 字段的大小?

标签 database postgresql orm prisma

我使用 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 varyingcharacter :

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 fact character(<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/

相关文章:

mysql - 搜索包含数十亿条记录的 MySQL 数据库的最快方法是什么?

database - 在 Redis 中对一组的所有项目进行排名

node.js - sequelize 中不区分大小写的搜索

java - JPA JoinColumn 与mappedBy

django - 使用 Django 的 ORM 加速批量插入?

sql - 搜索和替换 SQL

postgresql - 如何创建接受 NULL 的 DOMAIN?

sql - 有什么办法可以在 postgreSQL 中将假期与工作日分开吗?

postgresql - 请求的未知数据库类型 json,Doctrine\DBAL\Platforms\PostgreSqlPlatform 可能不支持

sql-server - 在 sql 中,除了重新启动服务之外,还有另一种清除临时数据库及其日志的方法吗?