不需要在具有 UNIQUE 约束的列上创建索引,对吧?

No need to create an index on a column with a UNIQUE constraint, right?

我有一个包含三列的 table。

_id (primary key)    word (unique)     frequency

SQLite.swift 中,我创建了 table 这样的

try db.run( userDictionary.create(ifNotExists: true) {t in
    t.column(wordId, primaryKey: true)
    t.column(word, unique: true)
    t.column(frequency, defaultValue: 1)
    })

我相信 SQLite 语法会是

CREATE TABLE "words" (
    "_id" INTEGER PRIMARY KEY NOT NULL,
    "word" TEXT UNIQUE NOT NULL,
    "frequency" INTEGER DEFAULT 1
)

我正准备在 "word" 列上添加索引以提高性能,因为我将不得不对其进行频繁查询。但后来我在 Indexes tutorial by TutorialsPoint 中读到以下有趣的陈述:

Implicit Indexes:

Implicit indexes are indexes that are automatically created by the database server when an object is created. Indexes are automatically created for primary key constraints and unique constraints.

因此,由于我已经向 "word" 列添加了唯一约束,因此无需执行任何其他操作。那是对的吗?我试图在 SQLite documentation 中确认这一点,但找不到。

正确。 UNIQUE 创建索引。这就是大多数数据库实际执行它的方式。

想象一下他们没有创建索引,会发生什么。他们实际上需要对每个插入进行完整的 table 扫描,这意味着性能将超出 window.