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

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

问题描述

当一个整数列在 SQLite 表中被标记为主键时,是否也应该为它显式创建索引?SQLite 似乎不会自动为主键列创建索引,但鉴于其目的,它可能无论如何都会对其进行索引?(我会一直在那个专栏上搜索)

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

解决方案

它为您做到了.

一个>

<块引用>

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

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?

解决方案

It does it for you.

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天全站免登陆