SQLite中的主键是否需要索引? [英] Is an index needed for a primary key in SQLite?

查看:221
本文介绍了SQLite中的主键是否需要索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQLite表中将整数列标记为主键时,是否也应为其显式创建索引? SQLite似乎不会自动为主键列创建索引,但是也许可以给它建立索引,以达到其目的? (我将一直在该列上进行搜索.)

When an integer column is marked as a primary key in an SQLite table, should an index be explicitly created for it as well? SQLite does not appear to automatically create an index for a primary key column, but perhaps it indexes it anyway, given its purpose? (I will be searching on that column all the time).

字符串主键的情况会有所不同吗?

Would the situation be any different for a string primary key?

推荐答案

它会为您完成.

除了INTEGER PRIMARY KEY列外,UNIQUE和PRIMARY KEY 约束是通过在数据库中创建索引来实现的(在 与创建唯一索引"语句相同的方式).这样的 索引与数据库中的任何其他索引一样用于优化 查询.结果,通常没有优势(但显着 开销)在一组已经存在的列上创建索引 共同受到UNIQUE或PRIMARY KEY约束.

INTEGER PRIMARY KEY columns aside, both UNIQUE and PRIMARY KEY constraints are implemented by creating an index in the database (in the same way as a "CREATE UNIQUE INDEX" statement would). Such an index is used like any other index in the database to optimize queries. As a result, there often no advantage (but significant overhead) in creating an index on a set of columns that are already collectively subject to a UNIQUE or PRIMARY KEY constraint.

这篇关于SQLite中的主键是否需要索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆