简体   繁体   English

是否需要索引具有唯一约束的列?

[英]Do columns with a unique-constraint need to be indexed?

I am using Hibernate to add unique-constraints over certain columns in my table as follows uniqueConstraints = {@UniqueConstraint(columnNames = {"warehouseCode", "orderCode", "vendorId"})}, .我正在使用 Hibernate 在我的表中的某些列上添加唯一约束,如下所示uniqueConstraints = {@UniqueConstraint(columnNames = {"warehouseCode", "orderCode", "vendorId"})}, I need to add an index over these exact columns as well.我还需要在这些确切的列上添加索引。 My question is, do I need to explicitly create an index for these columns, or does the unique-constraint automatically do it for me?我的问题是,我是否需要为这些列显式创建索引,还是唯一约束会自动为我创建索引?

Technically, a unique constraint does not actually guarantee that an index will be made, but I'm not aware of any DB engine that doesn't.从技术上讲,唯一约束实际上并不能保证会创建索引,但我不知道有任何数据库引擎不这样做。

Note that a DB engine is nevertheless fully free to treat them differently, and many DBs do just that.请注意,数据库引擎仍然可以完全自由地以不同方式对待它们,许多数据库就是这样做的。 However, there is such a thing as a unique index - and you'd need to compare one of those to a unique constraint to see meaningful differences.但是,存在唯一索引之类的东西 - 您需要将其中一个与唯一约束进行比较以查看有意义的差异。 A unique index can be disabled, which in passing then disables the uniqueness checking (at least, on most DB engines).可以禁用唯一索引,然后禁用唯一性检查(至少在大多数数据库引擎上)。 A unique constraint often cannot.唯一约束通常不能。

In DB land, the advice is to make a unique index if you actually intend to use it as an index, and to make a constraint if you just care about uniqueness.在 DB Land 中,如果您真的打算将其用作索引,建议是创建一个唯一索引,如果您只关心唯一性,则建议创建一个约束。 As I said, in practice, every DB engine I know implements a unique constraint by making an index, and every DB engine I know will use that index for general queries because it is there.正如我所说,在实践中,我知道的每个数据库引擎都通过创建索引来实现唯一约束,我知道的每个数据库引擎都会使用该索引进行一般查询,因为它就在那里。

How does this all translate to hibernate?这一切如何转化为休眠? That's the problem with hibernate - you're just adding to the learning curve and simplify nothing, because often you need to know what's happening on the SQL side.这就是 hibernate 的问题——你只是增加了学习曲线而没有简化任何事情,因为通常你需要知道 SQL 端发生了什么。

In this case, it translates quite badly , hibernate has a bit of an oversight and does not appear to understand that there is such a concept as a 'unique index'.在这种情况下,它的翻译非常糟糕,hibernate 有一些疏忽,似乎不理解存在“唯一索引”这样的概念。

EDIT编辑

Thinking about it some more, this really is nuts - there are good reasons to want an index, specifically.再考虑一下,这真的很疯狂 - 有充分的理由需要一个索引,特别是。 For example, let's say you want an index on a asc, b desc, c asc , which cannot be faked in any way with eg a asc, b asc, c asc .例如,假设您想要a asc, b desc, c asc上的索引,不能以任何方式伪造,例如a asc, b asc, c asc And you also want uniqueness over a/b/c in one go.而且您还希望一次性获得 a/b/c 的唯一性。 Just about every DB engine can do this efficiently by way of making an index of a asc, b desc, c asc , and adding the UNIQUE keyword as you make it.几乎每个数据库引擎都可以通过创建a asc, b desc, c asc的索引并在创建时添加 UNIQUE 关键字来有效地做到这一点。

If hibernate cannot be cajoled into making such a thing, that sure sounds like hibernate is not fit for purpose.如果不能诱使 hibernate 做出这样的事情,那肯定听起来 hibernate 不适合目的。

I'm trying to find docs on how to make it, but I just can't find it.我正在尝试查找有关如何制作它的文档,但我就是找不到。 It's bizarre.这很奇怪。 Can hibernate really not do this?休眠真的可以不这样做吗?

Note that tools like JDBI and JOOQ exists, which let you actually do SQL things.请注意,存在 JDBI 和 JOOQ 等工具,它们可让您实际执行 SQL 操作。 If you're already familiar with DB engines and SQL and the like, and you're early enough in this project, you may want to look for how to make a unique index in hibernate, and if you cannot, consider switching.如果你已经熟悉数据库引擎和 SQL 之类的,并且你在这个项目中足够早,你可能想寻找如何在 hibernate 中创建唯一索引,如果你不能,考虑切换。

END EDIT结束编辑

So, you have three choices:所以,你有三个选择:

  1. Make both an index and the unique constraint, doubling the DBs work, slowing everything down, and doubling the size of things.创建索引和唯一约束,使 DB 工作量加倍,减慢一切速度,并使事物的大小加倍。 Pray that hibernate is smart enough not to actually make that index, but I doubt that's how it would work.祈祷 hibernate 足够聪明,不会实际制作该索引,但我怀疑它是如何工作的。 Given that it's a 3-field index, it's a pricey one, this does not sound like a good idea.鉴于它是一个 3 字段索引,这是一个昂贵的索引,这听起来不是一个好主意。

  2. Leave it as is, and rely on the fact that all DB engines will be implementing this with an index and will use that index for queries as well, even though it's not quite the right 'style' especially when connecting straight to the DB.保持原样,并依赖于这样一个事实,即所有数据库引擎都将使用索引实现这一点,并将使用该索引进行查询,即使它不是完全正确的“样式”,尤其是在直接连接到数据库时。

  3. File a feature request with hibernate or look up how you can flag an index as 'requires uniqueness' within hibernate's @Index functionality.使用 hibernate 提交功能请求或查找如何在 hibernate 的@Index功能@Index索引标记为“需要唯一性”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Liferay服务构建器中的唯一约束 - unique-constraint in liferay service builder Hibernate产生了我不需要和不需要的唯一约束 - Hibernate producing unique constraint that I do not want and do not need JPA2独特的约束:我真的需要冲洗吗? - JPA2 unique constraint: do I really need to flush? Mysql 多列的唯一约束 - Mysql Unique constraint over multiple columns 多于两列组合的唯一约束 - Unique constraint on combination of more than two columns 我需要 org.sqlite.SQLiteException 的正则表达式:[SQLITE_CONSTRAINT_UNIQUE] 唯一约束失败 - i need regular expression for org.sqlite.SQLiteException: [SQLITE_CONSTRAINT_UNIQUE] A UNIQUE constraint failed 打开连接上的 UCanAccess 异常 - 引用的列上不存在 UNIQUE 约束 - UCanAccess exception on open connection - UNIQUE constraint does not exist on referenced columns 如何使用UCanAccess在两列上创建具有唯一约束的表? - How to CREATE TABLE with unique constraint on two columns using UCanAccess? 约束无效:表上没有与外键中的列数和类型相匹配的唯一键或主键约束 - Constraint is invalid: there is no unique or primary key constraint on table that matches the number and types of the columns in the foreign key DynamoDB中的全局二级索引是否需要唯一? - Do global secondary indexes in DynamoDB need to be unique?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM