views:

33

answers:

2

Hello everyone,

Our current database (MySQL) already has Indexes for its foreign keys since these use InnoDB, which (afaik) automatically generates IndexTables for foreign keys. Hibernate wasnt used to create these tables. So my question is: Is MySQL using Indexes for their foreign keys automatically, when I use Hibernate to fetch Data from its tables? Or do I have to configure Hibernate to use these Indexes?

Thank you, ymene

+1  A: 

Yes, the query optimizer decides to use or not to use an index. Use EXPLAIN on your SELECT statements to see what it uses. There is no difference in a query created by Hibernate or a hardcoded query.

Frank Heikens
Thank you. Didnt know about EXPLAIN before, useful hint!
ymene
+1  A: 

Is MySQL using Indexes for their foreign keys automatically, when I use Hibernate to fetch Data from its tables? Or do I have to configure Hibernate to use these Indexes?

There is nothing to configure in Hibernate to make the SQL backend use indices for joins. Let Hibernate perform its queries and if the optimizer decides that using an index is appropriate, it will use it.

PS: I'm not talking about Index Hints syntax here, see HHH-2736 - support for native/SQL query hints in HQL/Criteria (Oracle SELECT hints for example), this is something different.

Pascal Thivent
I already guessed, that the query optimizer takes care, just wasnt sure, cause the index usages seemed too small in php-admin statistics, so guess the mistake is somewhere else. So thank you too, for your answer!
ymene