索引
Prisma ORM 允许配置数据库索引、唯一约束和主键约束。这是 4.0.0
及更高版本中的普遍可用性。你可以使用 3.5.0
及更高版本中的 extendedIndexes
预览功能启用此功能。
¥Prisma ORM allows configuration of database indexes, unique constraints and primary key constraints. This is in General Availability in versions 4.0.0
and later. You can enable this with the extendedIndexes
Preview feature in versions 3.5.0
and later.
版本 3.6.0
还通过新的 @@fulltext
属性引入了对 MySQL 和 MongoDB 中全文索引的自省和迁移的支持,该属性可通过 fullTextIndex
预览功能获得。
¥Version 3.6.0
also introduces support for introspection and migration of full text indexes in MySQL and MongoDB through a new @@fulltext
attribute, available through the fullTextIndex
Preview feature.
如果你要从 4.0.0 之前的版本升级,并且你的数据库已使用这些功能,则对索引配置和全文索引的这些更改可能会是重大更改。有关如何升级的详细信息,请参阅 从以前的版本升级。
¥If you are upgrading from a version earlier than 4.0.0, these changes to index configuration and full text indexes might be breaking changes if you have a database that already uses these features. See Upgrading from previous versions for more information on how to upgrade.
索引配置
¥Index configuration
你可以使用以下属性参数配置索引、唯一约束和主键约束:
¥You can configure indexes, unique constraints, and primary key constraints with the following attribute arguments:
-
length
参数 允许你指定要在String
和Bytes
类型上索引的值的子部分的最大长度¥The
length
argument allows you to specify a maximum length for the subpart of the value to be indexed onString
andBytes
types-
适用于
@id
、@@id
、@unique
、@@unique
和@@index
属性¥Available on the
@id
,@@id
,@unique
,@@unique
and@@index
attributes -
仅限 MySQL
¥MySQL only
-
-
sort
参数 允许你指定约束或索引的条目在数据库中的存储顺序¥The
sort
argument allows you to specify the order that the entries of the constraint or index are stored in the database-
适用于所有数据库中的
@unique
、@@unique
和@@index
属性以及 SQL Server 中的@id
和@@id
属性¥Available on the
@unique
,@@unique
and@@index
attributes in all databases, and on the@id
and@@id
attributes in SQL Server
-
-
type
参数 允许你支持除 PostgreSQL 默认BTree
访问方法之外的索引访问方法¥The
type
argument allows you to support index access methods other than PostgreSQL's defaultBTree
access method-
可用于
@@index
属性¥Available on the
@@index
attribute -
仅限 PostgreSQL
¥PostgreSQL only
-
支持的索引访问方法:
Hash
、Gist
、Gin
、SpGist
和Brin
¥Supported index access methods:
Hash
,Gist
,Gin
,SpGist
andBrin
-
-
clustered
参数 允许你配置约束或索引是聚集的还是非聚集的¥The
clustered
argument allows you to configure whether a constraint or index is clustered or non-clustered-
适用于
@id
、@@id
、@unique
、@@unique
和@@index
属性¥Available on the
@id
,@@id
,@unique
,@@unique
and@@index
attributes -
仅限 SQL Server
¥SQL Server only
-
请参阅链接部分,了解首次引入每个功能的版本的详细信息。
¥See the linked sections for details of which version each feature was first introduced in.
使用 length
配置索引长度 (MySQL)
¥Configuring the length of indexes with length
(MySQL)
length
参数特定于 MySQL,允许你在 String
和 Byte
类型的列上定义索引和约束。对于这些类型,MySQL 要求你指定要索引的值的子部分的最大长度,以防整个值超出 MySQL 对索引大小的限制。详细信息请参见 MySQL 文档。
¥The length
argument is specific to MySQL and allows you to define indexes and constraints on columns of String
and Byte
types. For these types, MySQL requires you to specify a maximum length for the subpart of the value to be indexed in cases where the full value would exceed MySQL's limits for index sizes. See the MySQL documentation for more details.
length
参数可用于 @id
、@@id
、@unique
、@@unique
和 @@index
属性。它通常在 4.0.0 及更高版本中提供,并在 3.5.0 及更高版本中作为 extendedIndexes
预览功能的一部分提供。
¥The length
argument is available on the @id
, @@id
, @unique
, @@unique
and @@index
attributes. It is generally available in versions 4.0.0 and later, and available as part of the extendedIndexes
preview feature in versions 3.5.0 and later.
例如,以下数据模型声明最大长度为 3000 个字符的 id
字段:
¥As an example, the following data model declares an id
field with a maximum length of 3000 characters:
model Id {
id String @id @db.VarChar(3000)
}
这在 MySQL 中无效,因为它超出了 MySQL 的索引存储限制,因此 Prisma ORM 拒绝数据模型。生成的 SQL 将被数据库拒绝。
¥This is not valid in MySQL because it exceeds MySQL's index storage limit and therefore Prisma ORM rejects the data model. The generated SQL would be rejected by the database.
CREATE TABLE `Id` (
`id` VARCHAR(3000) PRIMARY KEY
)
length
参数允许你指定仅 id
值的子部分代表主键。在下面的示例中,使用了前 100 个字符:
¥The length
argument allows you to specify that only a subpart of the id
value represents the primary key. In the example below, the first 100 characters are used:
model Id {
id String @id(length: 100) @db.VarChar(3000)
}
如果在数据模型中指定,Prisma Migrate 能够使用 length
参数创建约束和索引。这意味着你可以对 Prisma 架构类型 Byte
和 String
的值创建索引和约束。如果你不指定参数,则索引将被视为像以前一样覆盖完整值。
¥Prisma Migrate is able to create constraints and indexes with the length
argument if specified in your data model. This means that you can create indexes and constraints on values of Prisma schema type Byte
and String
. If you don't specify the argument the index is treated as covering the full value as before.
自省将获取现有数据库中存在的这些限制。这使得 Prisma ORM 能够支持以前被抑制的索引和约束,并利用此功能更好地支持 MySQL 数据库。
¥Introspection will fetch these limits where they are present in your existing database. This allows Prisma ORM to support indexes and constraints that were previously suppressed and results in better support of MySQL databases utilizing this feature.
length
参数也可以用于复合主键,使用 @@id
属性,如下例所示:
¥The length
argument can also be used on compound primary keys, using the @@id
attribute, as in the example below:
model CompoundId {
id_1 String @db.VarChar(3000)
id_2 String @db.VarChar(3000)
@@id([id_1(length: 100), id_2(length: 10)])
}
类似的语法可用于 @@unique
和 @@index
属性。
¥A similar syntax can be used for the @@unique
and @@index
attributes.
使用 sort
配置索引排序顺序
¥Configuring the index sort order with sort
sort
参数适用于 Prisma ORM 支持的所有数据库。它允许你指定索引或约束条目在数据库中的存储顺序。这可能会影响数据库是否能够对特定查询使用索引。
¥The sort
argument is available for all databases supported by Prisma ORM. It allows you to specify the order that the entries of the index or constraint are stored in the database. This can have an effect on whether the database is able to use an index for specific queries.
sort
参数适用于 @unique
、@@unique
和 @@index
上的所有数据库。此外,SQL Server 还允许在 @id
和 @@id
上使用它。它通常在 4.0.0 及更高版本中提供,并在 3.5.0 及更高版本中作为 extendedIndexes
预览功能的一部分提供。
¥The sort
argument is available for all databases on @unique
, @@unique
and @@index
. Additionally, SQL Server also allows it on @id
and @@id
. It is generally available in versions 4.0.0 and later, and available as part of the extendedIndexes
preview feature in versions 3.5.0 and later.
举个例子,如下表
¥As an example, the following table
CREATE TABLE `Unique` (
`unique` INT,
CONSTRAINT `Unique_unique_key` UNIQUE (`unique` DESC)
)
现在被内省为
¥is now introspected as
model Unique {
unique Int @unique(sort: Desc)
}
sort
参数也可以用于复合索引:
¥The sort
argument can also be used on compound indexes:
model CompoundUnique {
unique_1 Int
unique_2 Int
@@unique([unique_1(sort: Desc), unique_2])
}
示例:一起使用 sort
和 length
¥Example: using sort
and length
together
以下示例演示如何使用 sort
和 length
参数为 Post
模型配置索引和约束:
¥The following example demonstrates the use of the sort
and length
arguments to configure indexes and constraints for a Post
model:
model Post {
title String @db.VarChar(300)
abstract String @db.VarChar(3000)
slug String @unique(sort: Desc, length: 42) @db.VarChar(3000)
author String
created_at DateTime
@@id([title(length: 100, sort: Desc), abstract(length: 10)])
@@index([author, created_at(sort: Desc)])
}
使用 type
配置索引的访问类型 (PostgreSQL)
¥Configuring the access type of indexes with type
(PostgreSQL)
type
参数可用于使用 @@index
属性配置 PostgreSQL 中的索引类型。可用的索引访问方法有 Hash
、Gist
、Gin
、SpGist
和 Brin
,以及默认的 BTree
索引访问方法。type
参数通常在 4.0.0 及更高版本中可用。Hash
索引访问方法作为 extendedIndexes
预览功能的一部分在 3.6.0 及更高版本中提供,Gist
、Gin
、SpGist
和 Brin
索引访问方法在 3.14.0 及更高版本中以预览形式提供。
¥The type
argument is available for configuring the index type in PostgreSQL with the @@index
attribute. The index access methods available are Hash
, Gist
, Gin
, SpGist
and Brin
, as well as the default BTree
index access method. The type
argument is generally available in versions 4.0.0 and later. The Hash
index access method is available as part of the extendedIndexes
preview feature in versions 3.6.0 and later, and the Gist
, Gin
, SpGist
and Brin
index access methods are available in preview in versions 3.14.0 and later.
哈希值
¥Hash
Hash
类型将以搜索和插入速度更快的格式存储索引数据,并且使用更少的磁盘空间。但是,只有 =
和 <>
比较可以使用索引,因此其他比较运算符(例如 <
和 >
)使用 Hash
时会比使用默认 BTree
类型时慢得多。
¥The Hash
type will store the index data in a format that is much faster to search and insert, and that will use less disk space. However, only the =
and <>
comparisons can use the index, so other comparison operators such as <
and >
will be much slower with Hash
than when using the default BTree
type.
例如,以下模型将 type
为 Hash
的索引添加到 value
字段:
¥As an example, the following model adds an index with a type
of Hash
to the value
field:
model Example {
id Int @id
value Int
@@index([value], type: Hash)
}
这将转换为以下 SQL 命令:
¥This translates to the following SQL commands:
CREATE TABLE "Example" (
id INT PRIMARY KEY,
value INT NOT NULL
);
CREATE INDEX "Example_value_idx" ON "Example" USING HASH (value);
广义倒排索引 (GIN)
¥Generalized Inverted Index (GIN)
GIN 索引存储复合值,例如数组或 JsonB
数据。这对于加快查询一个对象是否是另一对象的一部分很有用。它通常用于全文搜索。
¥The GIN index stores composite values, such as arrays or JsonB
data. This is useful for speeding up querying whether one object is part of another object. It is commonly used for full-text searches.
索引字段可以定义运算符类,该类定义索引处理的运算符。
¥An indexed field can define the operator class, which defines the operators handled by the index.
Prisma ORM 尚不支持使用函数(例如 to_tsvector
)确定索引值的索引。以这种方式定义的索引对于 prisma db pull
来说是不可见的。
¥Indexes using a function (such as to_tsvector
) to determine the indexed value are not yet supported by Prisma ORM. Indexes defined in this way will not be visible with prisma db pull
.
例如,以下模型将 Gin
索引添加到 value
字段,其中 JsonbPathOps
作为允许使用该索引的运算符类别:
¥As an example, the following model adds a Gin
index to the value
field, with JsonbPathOps
as the class of operators allowed to use the index:
model Example {
id Int @id
value Json
// ^ field type matching the operator class
// ^ operator class ^ index type
@@index([value(ops: JsonbPathOps)], type: Gin)
}
这将转换为以下 SQL 命令:
¥This translates to the following SQL commands:
CREATE TABLE "Example" (
id INT PRIMARY KEY,
value JSONB NOT NULL
);
CREATE INDEX "Example_value_idx" ON "Example" USING GIN (value jsonb_path_ops);
作为 JsonbPathOps
的一部分,@>
运算符由索引处理,从而加快了 value @> '{"foo": 2}'
等查询的速度。
¥As part of the JsonbPathOps
the @>
operator is handled by the index, speeding up queries such as value @> '{"foo": 2}'
.
GIN 支持的运算符类
¥Supported Operator Classes for GIN
Prisma ORM 一般支持 PostgreSQL 10 及更高版本提供的运算符类。如果运算符类要求字段类型为 Prisma ORM 尚不支持的类型,则使用带有字符串输入的 raw
函数允许你无需验证即可使用这些运算符类。
¥Prisma ORM generally supports operator classes provided by PostgreSQL in versions 10 and later. If the operator class requires the field type to be of a type Prisma ORM does not yet support, using the raw
function with a string input allows you to use these operator classes without validation.
索引定义中可以省略默认运算符类(用 ✅ 标记)。
¥The default operator class (marked with ✅) can be omitted from the index definition.
运算符等级 | 允许的字段类型(原生类型) | 默认 | 其他 |
---|---|---|---|
ArrayOps | 任意数组 | ✅ | 也可在 CockroachDB 中使用 |
JsonbOps | Json (@db.JsonB ) | ✅ | 也可在 CockroachDB 中使用 |
JsonbPathOps | Json (@db.JsonB ) | ||
raw("other") |
阅读有关 PostgreSQL 官方文档 中内置运算符类的更多信息。
¥Read more about built-in operator classes in the official PostgreSQL documentation.
CockroachDB
GIN 和 BTree 是 CockroachDB 支持的唯一索引类型。标记为与 CockroachDB 一起使用的运算符类是该数据库上唯一允许并受 Prisma ORM 支持的运算符类。运算符类不能在 Prisma Schema 语言中定义:CockroachDB 上不需要或不允许使用 ops
参数。
¥GIN and BTree are the only index types supported by CockroachDB. The operator classes marked to work with CockroachDB are the only ones allowed on that database and supported by Prisma ORM. The operator class cannot be defined in the Prisma Schema Language: the ops
argument is not necessary or allowed on CockroachDB.
广义搜索树 (GiST)
¥Generalized Search Tree (GiST)
GiST 索引类型用于实现用户定义类型的索引方案。默认情况下,GiST 索引没有太多直接用途,但例如 B-Tree 索引类型是使用 GiST 索引构建的。
¥The GiST index type is used for implementing indexing schemes for user-defined types. By default there are not many direct uses for GiST indexes, but for example the B-Tree index type is built using a GiST index.
例如,以下模型将 Gist
索引添加到 value
字段,并使用 InetOps
作为将使用该索引的运算符:
¥As an example, the following model adds a Gist
index to the value
field with InetOps
as the operators that will be using the index:
model Example {
id Int @id
value String @db.Inet
// ^ native type matching the operator class
// ^ index type
// ^ operator class
@@index([value(ops: InetOps)], type: Gist)
}
这将转换为以下 SQL 命令:
¥This translates to the following SQL commands:
CREATE TABLE "Example" (
id INT PRIMARY KEY,
value INET NOT NULL
);
CREATE INDEX "Example_value_idx" ON "Example" USING GIST (value inet_ops);
比较 IP 地址的查询,例如 value > '10.0.0.2'
,将使用索引。
¥Queries comparing IP addresses, such as value > '10.0.0.2'
, will use the index.
GiST 支持的运算符类
¥Supported Operator Classes for GiST
Prisma ORM 一般支持 PostgreSQL 10 及更高版本提供的运算符类。如果运算符类要求字段类型为 Prisma ORM 尚不支持的类型,则使用带有字符串输入的 raw
函数允许你无需验证即可使用这些运算符类。
¥Prisma ORM generally supports operator classes provided by PostgreSQL in versions 10 and later. If the operator class requires the field type to be of a type Prisma ORM does not yet support, using the raw
function with a string input allows you to use these operator classes without validation.
运算符等级 | 允许的字段类型(允许的原生类型) |
---|---|
InetOps | String (@db.Inet ) |
raw("other") |
阅读有关 PostgreSQL 官方文档 中内置运算符类的更多信息。
¥Read more about built-in operator classes in the official PostgreSQL documentation.
空间分区 GiST (SP-GiST)
¥Space-Partitioned GiST (SP-GiST)
SP-GiST 索引对于许多不同的非平衡数据结构来说是一个不错的选择。如果查询符合分区规则,则速度会非常快。
¥The SP-GiST index is a good choice for many different non-balanced data structures. If the query matches the partitioning rule, it can be very fast.
与 GiST 一样,SP-GiST 作为用户定义类型的构建块非常重要,允许直接使用数据库实现自定义搜索运算符。
¥As with GiST, SP-GiST is important as a building block for user-defined types, allowing implementation of custom search operators directly with the database.
例如,以下模型将 SpGist
索引添加到 value
字段,并使用 TextOps
作为使用该索引的运算符:
¥As an example, the following model adds a SpGist
index to the value
field with TextOps
as the operators using the index:
model Example {
id Int @id
value String
// ^ field type matching the operator class
@@index([value], type: SpGist)
// ^ index type
// ^ using the default ops: TextOps
}
这将转换为以下 SQL 命令:
¥This translates to the following SQL commands:
CREATE TABLE "Example" (
id INT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE INDEX "Example_value_idx" ON "Example" USING SPGIST (value);
诸如 value LIKE 'something%'
之类的查询将通过索引加速。
¥Queries such as value LIKE 'something%'
will be sped up by the index.
SP-GiST 支持的运算符类
¥Supported Operator Classes for SP-GiST
Prisma ORM 一般支持 PostgreSQL 10 及更高版本提供的运算符类。如果运算符类要求字段类型为 Prisma ORM 尚不支持的类型,则使用带有字符串输入的 raw
函数允许你无需验证即可使用这些运算符类。
¥Prisma ORM generally supports operator classes provided by PostgreSQL in versions 10 and later. If the operator class requires the field type to be of a type Prisma ORM does not yet support, using the raw
function with a string input allows you to use these operator classes without validation.
索引定义中可以省略默认运算符类(用 ✅ 标记)。
¥The default operator class (marked with ✅) can be omitted from the index definition.
运算符等级 | 允许的字段类型(原生类型) | 默认 | 支持的 PostgreSQL 版本 |
---|---|---|---|
InetOps | String (@db.Inet ) | ✅ | 10+ |
TextOps | String (@db.Text 、@db.VarChar ) | ✅ | |
raw("other") |
阅读 PostgreSQL 官方文档 中有关内置运算符类的更多信息。
¥Read more about built-in operator classes from official PostgreSQL documentation.
区块范围索引 (BRIN)
¥Block Range Index (BRIN)
如果你有大量数据在插入后不会更改(例如日期和时间值),则 BRIN 索引类型非常有用。如果你的数据非常适合索引,则它可以在最小的空间中存储大型数据集。
¥The BRIN index type is useful if you have lots of data that does not change after it is inserted, such as date and time values. If your data is a good fit for the index, it can store large datasets in a minimal space.
例如,以下模型将 Brin
索引添加到 value
字段,并使用 Int4BloomOps
作为将使用该索引的运算符:
¥As an example, the following model adds a Brin
index to the value
field with Int4BloomOps
as the operators that will be using the index:
model Example {
id Int @id
value Int
// ^ field type matching the operator class
// ^ operator class ^ index type
@@index([value(ops: Int4BloomOps)], type: Brin)
}
这将转换为以下 SQL 命令:
¥This translates to the following SQL commands:
CREATE TABLE "Example" (
id INT PRIMARY KEY,
value INT4 NOT NULL
);
CREATE INDEX "Example_value_idx" ON "Example" USING BRIN (value int4_bloom_ops);
像 value = 2
这样的查询现在将使用索引,该索引使用 BTree
或 Hash
索引所用空间的一小部分。
¥Queries like value = 2
will now use the index, which uses a fraction of the space used by the BTree
or Hash
indexes.
BRIN 支持的运算符类
¥Supported Operator Classes for BRIN
Prisma ORM 通常支持 PostgreSQL 10 及更高版本提供的运算符类,并且某些受支持的运算符仅在 PostgreSQL 14 及更高版本中可用。如果运算符类要求字段类型为 Prisma ORM 尚不支持的类型,则使用带有字符串输入的 raw
函数允许你无需验证即可使用这些运算符类。
¥Prisma ORM generally supports operator classes provided by PostgreSQL in versions 10 and later, and some supported operators are only available from PostgreSQL versions 14 and later. If the operator class requires the field type to be of a type Prisma ORM does not yet support, using the raw
function with a string input allows you to use these operator classes without validation.
索引定义中可以省略默认运算符类(用 ✅ 标记)。
¥The default operator class (marked with ✅) can be omitted from the index definition.
运算符等级 | 允许的字段类型(原生类型) | 默认 | 支持的 PostgreSQL 版本 |
---|---|---|---|
BitMinMaxOps | String (@db.Bit ) | ✅ | |
VarBitMinMaxOps | String (@db.VarBit ) | ✅ | |
BpcharBloomOps | String (@db.Char ) | 14+ | |
BpcharMinMaxOps | String (@db.Char ) | ✅ | |
ByteaBloomOps | Bytes (@db.Bytea ) | 14+ | |
ByteaMinMaxOps | Bytes (@db.Bytea ) | ✅ | |
DateBloomOps | DateTime (@db.Date ) | 14+ | |
DateMinMaxOps | DateTime (@db.Date ) | ✅ | |
DateMinMaxMultiOps | DateTime (@db.Date ) | 14+ | |
Float4BloomOps | Float (@db.Real ) | 14+ | |
Float4MinMaxOps | Float (@db.Real ) | ✅ | |
Float4MinMaxMultiOps | Float (@db.Real ) | 14+ | |
Float8BloomOps | Float (@db.DoublePrecision ) | 14+ | |
Float8MinMaxOps | Float (@db.DoublePrecision ) | ✅ | |
Float8MinMaxMultiOps | Float (@db.DoublePrecision ) | 14+ | |
InetInclusionOps | String (@db.Inet ) | ✅ | 14+ |
InetBloomOps | String (@db.Inet ) | 14+ | |
InetMinMaxOps | String (@db.Inet ) | ||
InetMinMaxMultiOps | String (@db.Inet ) | 14+ | |
Int2BloomOps | Int (@db.SmallInt ) | 14+ | |
Int2MinMaxOps | Int (@db.SmallInt ) | ✅ | |
Int2MinMaxMultiOps | Int (@db.SmallInt ) | 14+ | |
Int4BloomOps | Int (@db.Integer ) | 14+ | |
Int4MinMaxOps | Int (@db.Integer ) | ✅ | |
Int4MinMaxMultiOps | Int (@db.Integer ) | 14+ | |
Int8BloomOps | BigInt (@db.BigInt ) | 14+ | |
Int8MinMaxOps | BigInt (@db.BigInt ) | ✅ | |
Int8MinMaxMultiOps | BigInt (@db.BigInt ) | 14+ | |
NumericBloomOps | Decimal (@db.Decimal ) | 14+ | |
NumericMinMaxOps | Decimal (@db.Decimal ) | ✅ | |
NumericMinMaxMultiOps | Decimal (@db.Decimal ) | 14+ | |
OidBloomOps | Int (@db.Oid ) | 14+ | |
OidMinMaxOps | Int (@db.Oid ) | ✅ | |
OidMinMaxMultiOps | Int (@db.Oid ) | 14+ | |
TextBloomOps | String (@db.Text 、@db.VarChar ) | 14+ | |
TextMinMaxOps | String (@db.Text 、@db.VarChar ) | ✅ | |
TextMinMaxMultiOps | String (@db.Text 、@db.VarChar ) | 14+ | |
TimestampBloomOps | DateTime (@db.Timestamp ) | 14+ | |
TimestampMinMaxOps | DateTime (@db.Timestamp ) | ✅ | |
TimestampMinMaxMultiOps | DateTime (@db.Timestamp ) | 14+ | |
TimestampTzBloomOps | DateTime (@db.Timestamptz ) | 14+ | |
TimestampTzMinMaxOps | DateTime (@db.Timestamptz ) | ✅ | |
TimestampTzMinMaxMultiOps | DateTime (@db.Timestamptz ) | 14+ | |
TimeBloomOps | DateTime (@db.Time ) | 14+ | |
TimeMinMaxOps | DateTime (@db.Time ) | ✅ | |
TimeMinMaxMultiOps | DateTime (@db.Time ) | 14+ | |
TimeTzBloomOps | DateTime (@db.Timetz ) | 14+ | |
TimeTzMinMaxOps | DateTime (@db.Timetz ) | ✅ | |
TimeTzMinMaxMultiOps | DateTime (@db.Timetz ) | 14+ | |
UuidBloomOps | String (@db.Uuid ) | 14+ | |
UuidMinMaxOps | String (@db.Uuid ) | ✅ | |
UuidMinMaxMultiOps | String (@db.Uuid ) | 14+ | |
raw("other") |
阅读有关 PostgreSQL 官方文档 中内置运算符类的更多信息。
¥Read more about built-in operator classes in the official PostgreSQL documentation.
使用 clustered
配置索引是聚集还是非聚集 (SQL Server)
¥Configuring if indexes are clustered or non-clustered with clustered
(SQL Server)
clustered
参数可用于在 SQL Server 中配置(非)聚集索引。它可用于 @id
、@@id
、@unique
、@@unique
和 @@index
属性。它通常在 4.0.0 及更高版本中提供,并在 3.13.0 及更高版本中作为 extendedIndexes
预览功能的一部分提供。
¥The clustered
argument is available to configure (non)clustered indexes in SQL Server. It can be used on the @id
, @@id
, @unique
, @@unique
and @@index
attributes. It is generally available in versions 4.0.0 and later, and available as part of the extendedIndexes
preview feature in versions 3.13.0 and later.
例如,以下模型将 @id
配置为非集群(而不是默认的集群):
¥As an example, the following model configures the @id
to be non-clustered (instead of the clustered default):
model Example {
id Int @id(clustered: false)
value Int
}
这将转换为以下 SQL 命令:
¥This translates to the following SQL commands:
CREATE TABLE [Example] (
id INT NOT NULL,
value INT,
CONSTRAINT [Example_pkey] PRIMARY KEY NONCLUSTERED (id)
)
每个属性的 clustered
默认值如下:
¥The default value of clustered
for each attribute is as follows:
属性 | 值 |
---|---|
@id | true |
@@id | true |
@unique | false |
@@unique | false |
@@index | false |
一张表最多可以有一个聚集索引。
¥A table can have at most one clustered index.
从以前的版本升级
¥Upgrading from previous versions
当激活现有数据库的某些现有 Prisma 模式的功能时,这些索引配置更改可能会造成重大更改。启用使用它们所需的预览功能后,运行 prisma db pull
来检查现有数据库以更新你的 Prisma 架构,然后再次使用 Prisma Migrate。
¥These index configuration changes can be breaking changes when activating the functionality for certain, existing Prisma schemas for existing databases. After enabling the preview features required to use them, run prisma db pull
to introspect the existing database to update your Prisma schema before using Prisma Migrate again.
在以下情况下可能会发生重大变更:
¥A breaking change can occur in the following situations:
-
现有的排序约束和索引:如果没有明确指定顺序,早期版本的 Prisma ORM 将假定所需的排序顺序是升序。这意味着,如果你有使用降序排序的现有约束或索引,并且在未首先在数据模型中指定这一点的情况下迁移数据库,那么这是一个重大更改。
¥Existing sort constraints and indexes: earlier versions of Prisma ORM will assume that the desired sort order is ascending if no order is specified explicitly. This means that this is a breaking change if you have existing constraints or indexes that are using descending sort order and migrate your database without first specifying this in your data model.
-
现有的长度约束和索引:在 Prisma ORM 的早期版本中,MySQL 中长度受限的索引和约束无法在 Prisma 模式中表示。因此
prisma db pull
没有获取这些,你无法手动指定它们。当你运行prisma db push
或prisma migrate dev
时,如果它们已存在于数据库中,它们将被忽略。由于你现在可以指定这些内容,因此如果数据模型中缺少这些内容但存在于数据库中,则迁移命令现在将删除它们。¥Existing length constraints and indexes: in earlier versions of Prisma ORM, indexes and constraints that were length constrained in MySQL could not be represented in the Prisma schema. Therefore
prisma db pull
was not fetching these and you could not manually specify them. When you ranprisma db push
orprisma migrate dev
they were ignored if already present in your database. Since you are now able to specify these, migrate commands will now drop them if they are missing from your data model but present in the database. -
除
BTree
(PostgreSQL) 之外的现有索引:早期版本的 Prisma ORM 仅支持默认的BTree
索引类型。在迁移数据库之前需要添加其他支持的索引(Hash
、Gist
、Gin
、SpGist
和Brin
)。¥Existing indexes other than
BTree
(PostgreSQL): earlier versions of Prisma ORM only supported the defaultBTree
index type. Other supported indexes (Hash
,Gist
,Gin
,SpGist
andBrin
) need to be added before migrating your database. -
现有(非)聚集索引 (SQL Server):早期版本的 Prisma ORM 不支持将索引配置为聚集或非聚集。对于不使用默认值的索引,需要在迁移数据库之前添加这些索引。
¥Existing (non-)clustered indexes (SQL Server): earlier versions of Prisma ORM did not support configuring an index as clustered or non-clustered. For indexes that do not use the default, these need to be added before migrating your database.
在上述每种情况下,可以通过在必要时在数据模型中正确指定这些属性来防止对数据库进行不必要的更改。最简单的方法是使用 prisma db pull
检索任何现有的约束或配置。或者,你也可以手动添加这些参数。这应该在升级后第一次使用 prisma db push
或 prisma migrate dev
之前完成。
¥In each of the cases above unwanted changes to your database can be prevented by properly specifying these properties in your data model where necessary. The easiest way to do this is to use prisma db pull
to retrieve any existing constraints or configuration. Alternatively, you could also add these arguments manually. This should be done before using prisma db push
or prisma migrate dev
the first time after the upgrade.
全文索引(MySQL 和 MongoDB)
¥Full text indexes (MySQL and MongoDB)
fullTextIndex
预览功能在 3.6.0 及更高版本中提供了对 MySQL 和 MongoDB 全文索引自省和迁移的支持。这可以使用 @@fulltext
属性进行配置。使用 db pull
进行内省后,数据库中的现有全文索引将添加到 Prisma 架构中,并且使用 Prisma Migrate 时会在数据库中创建 Prisma 架构中添加的新全文索引。这还可以防止某些以前无法正常工作的数据库模式中出现验证错误。
¥The fullTextIndex
preview feature provides support for introspection and migration of full text indexes in MySQL and MongoDB in version 3.6.0 and later. This can be configured using the @@fulltext
attribute. Existing full text indexes in the database are added to your Prisma schema after introspecting with db pull
, and new full text indexes added in the Prisma schema are created in the database when using Prisma Migrate. This also prevents validation errors in some database schemas that were not working before.
启用 fullTextIndex
预览功能
¥Enabling the fullTextIndex
preview feature
要启用 fullTextIndex
预览功能,请将 fullTextIndex
功能标志添加到 schema.prisma
文件的 generator
块中:
¥To enable the fullTextIndex
preview feature, add the fullTextIndex
feature flag to the generator
block of the schema.prisma
file:
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextIndex"]
}
示例
¥Examples
以下示例演示了向 Post
模型的 title
和 content
字段添加 @@fulltext
索引:
¥The following example demonstrates adding a @@fulltext
index to the title
and content
fields of a Post
model:
model Post {
id Int @id
title String @db.VarChar(255)
content String @db.Text
@@fulltext([title, content])
}
在 MongoDB 上,你可以使用 @@fulltext
索引属性(通过 fullTextIndex
预览功能)和 sort
参数,以升序或降序将字段添加到全文索引。以下示例为 Post
模型的 title
和 content
字段添加 @@fulltext
索引,并对 title
字段按降序排序:
¥On MongoDB, you can use the @@fulltext
index attribute (via the fullTextIndex
preview feature) with the sort
argument to add fields to your full-text index in ascending or descending order. The following example adds a @@fulltext
index to the title
and content
fields of the Post
model, and sorts the title
field in descending order:
generator js {
provider = "prisma-client-js"
previewFeatures = ["fullTextIndex"]
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model Post {
id String @id @map("_id") @db.ObjectId
title String
content String
@@fulltext([title(sort: Desc), content])
}
从以前的版本升级
¥Upgrading from previous versions
当激活现有数据库的某些现有 Prisma 模式的功能时,这可能是一个重大更改。启用使用它们所需的预览功能后,运行 prisma db pull
来检查现有数据库以更新你的 Prisma 架构,然后再次使用 Prisma Migrate。
¥This can be a breaking change when activating the functionality for certain, existing Prisma schemas for existing databases. After enabling the preview features required to use them, run prisma db pull
to introspect the existing database to update your Prisma schema before using Prisma Migrate again.
早期版本的 Prisma ORM 使用 @@index
属性而不是 @@fulltext
属性转换全文索引。启用 fullTextIndex
预览功能后,运行 prisma db pull
将这些索引转换为 @@fulltext
,然后再使用 Prisma Migrate 再次迁移。如果不这样做,现有索引将被删除,并在其位置创建普通索引。
¥Earlier versions of Prisma ORM converted full text indexes using the @@index
attribute rather than the @@fulltext
attribute. After enabling the fullTextIndex
preview feature, run prisma db pull
to convert these indexes to @@fulltext
before migrating again with Prisma Migrate. If you do not do this, the existing indexes will be dropped instead and normal indexes will be created in their place.