Understanding N+1 select alerts

9 views
Skip to first unread message

wabrit

unread,
Jan 18, 2013, 11:33:56 AM1/18/13
to hibernate...@googlegroups.com
Hi

I'm getting lots of N+1 select alerts showing up in the Hibernate Profiler console, but I'm struggling to see why the profiler thinks they are N+1 select problems,
so I suspect I have misunderstood what Hibernate Profiler is trying to tell me. (I believe I understand the general concept of the N+1 select problem in Hibernate,
and how to avoid it.)

For example, the following query is flagged with N+1 select alert:

select max(mytable0_.BIT) as col_0_0_
from   MYTABLE mytable0_
where  mytable0_.CATEGORY_FK = ?

Since it's hard to believe a projection query trigger can be the root cause of an N+1 select, is the profiler saying that this query has been called "N+1 times"
because I previously did e.g. a "select * from MYTABLE" rather than a "select *,max(BIT) from MYTABLE"?

There are other examples where a "select * from MYTABLE" is flagged with an "N+1" select alert, but it's the only SQL operation in the transaction and there
are no "for loops"; I'm struggling to understand how that could be interpreted as N+1 select.

Thanks

Alan

Fitzchak Yitzchaki

unread,
Jan 20, 2013, 3:42:41 AM1/20/13
to hibernate...@googlegroups.com
In order to better answer that, can you send us the File > Save to File file to support at hibernatingrhinos.com?

Oren Eini (Ayende Rahien)

unread,
Jan 21, 2013, 1:47:41 AM1/21/13
to hibernate-profiler
Select N+1 warning is raised when we see more than 4 queries with the same text (but might be different parameters).
For example, you might be doing something like:

List cls = session.query(MyClass.class).toList();
for(MyClass c : cls)
{
    c.getMaxItem()
}
Reply all
Reply to author
Forward
0 new messages