Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you need to create index on the @id column of a Hibernate table

Do I need to add an index anoatation for the primary key of a hibernate table for decent performance, I assumed that marking a field with @id would mean an index was created

@Id
private String guid;

but I didnt notice anything being created in the ddl that was generated

But if I added an @index annotation

@Id
@org.hibernate.annotations.Index(name = "IDX_GUID")
private String guid;

then I do notice an index being created in the DDL.

So I'm thinking I need to do this for every table, but part of me is thinking is this really neccessary as surely hibernate would want indexes created for the primary key as a starting point ?

like image 571
Paul Taylor Avatar asked Jan 18 '13 08:01

Paul Taylor


People also ask

Does hibernate use index?

Hibernate really does not "interfere" with indexes. When the framework executes the queries it will auto-generate the SQL from the provided and the mappings, later then this is sent directly to the database and right there, the RDMS will determine what's the "best way" to execute it. Save this answer.

Do we need to create index on primary key?

But in the database world, it's actually not necessary to create an index on the primary key column — the primary index can be created on any non primary key column as well.

How does hibernate indexing work?

Hibernate Search lets you write your own custom DirectoryProvider . The DirectoryProvider implementation benefits from the same configuration infrastructure available for built-in directory providers. The list of properties matching the current index name is passed to the initialize method.

What is index in hibernate?

Indexing. The short answer is that indexing is automatic: Hibernate Search will transparently index every entity each time it's persisted, updated or removed through Hibernate ORM. Its mission is to keep the index and your database in sync, allowing you to forget about this problem.


1 Answers

You do NOT have to create index explicitly. Instead of seeing DDL statements; I will recommend you to check the final schema created by hibernate. The index is created as part of create table statement.

like image 103
Deepak Singhal Avatar answered Sep 17 '22 09:09

Deepak Singhal