'In DB MySQL the index already exists for the column, should I add the index in the Hibernate Entity?
If the column has already the index "INDEX_VALUE" in DB MySQL.
Do I still need to add in hiberante Entity?
@NamedQuery(name = "", query = "SELECT e FROM Entity e WHERE e.value = :value ")
@Table(...
indexes = { @Index(name = "INDEX_VALUE", columnNames = { "value" })})
public class Entity {
}
If I don't add this index in the entity, does it affect the search efficiency of @NamedQuery?
Solution 1:[1]
@Index
is used in schema generation to specify creation of an index.
So if you have already created the index it the DB through an SQL script, declaring it in the Entity won't make a difference.
This index does not affect the named query because the named query search for the value column but the index references the status column.
Solution 2:[2]
Your queries are run in MySQL server eventually no matter where you execute them from.
@Index
will help you while creating or updating an schema (of course when hbm2ddl configuration is set set to update or create-drop.
Hence, it makes no difference using @Index in your entity while you database schema already has the column indexed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lazaros Chatzimichalakis |
Solution 2 | Asgar |