views:

131

answers:

5

I'm primarily a Java developer who works with Hibernate, and in some of my use cases I have queries which perform very slowly compared to what I expect. I've talked to the local DBAs and in many cases they claim the performance can't be improved because of the nature of the query.

However, I'm somewhat hesitant to take them at their word. What resources can I use to learn when I have to suck it up and find a different way of getting the information I want or learn to live with the speed and when I can call bullshit on the DBAs.

+3  A: 

What is the basis for your query performance expectations? Gut feel? If you're going to argue with the DBA's, you'll need to know (at a minimum) what your queries are, understand EXPLAIN PLAN, and be able to point out how to improve things. And, as @OMG Ponies points out, Hibernate may be doing a poor job of constructing queries - then what do you do?

Not easy? Perhaps a better approach would be to take a bit less adversarial approach with the DBA staff and ask politely what it is about the queries that inhibits performance improvements and if there's any suggestions they might have about how you could refactor them to perform better.

DCookie
EXPLAIN PLAN is the way to go. It tells you how the query is being processed, and how much data it expects to process. You compare that to the gut feel of 'how much data should I need to process for this query'. If they differ, ask why.
Gary
A good ORM usually does a much better job than the average developer and can be finely tuned (and I consider this to be true about Hibernate).
Pascal Thivent
@Pascal, I would hope so, else they would be next to worthless.
DCookie
A: 

it also maybe a table structure (normalization) issue. Mostly, this is exactly why i don't go with hybernate - you should always be able to write your own queries that are optimal.

Randy
+3  A: 

Posting my comment as an answer:

That's the trade-off with using an ORM - you're at it's mercy for how it constructs the query that is shipped to the database. LINQ is the only one that interested me, because you can use it in tandem with stored procedures for situations like these. I'm surprised the DBAs don't tell you to ditch ORM if you want better speed...

The EXPLAIN plan will give you an idea of the efficiency, but not really a perspective on speed. For Oracle, you'd need to use tkprof (assuming available to you) to analyze what is going on.

OMG Ponies
+1 might as well get credit for a good observation ;-)
DCookie
And what makes you think Hibernate doesn't support stored procedure? Hint: it does.
Pascal Thivent
OMG Ponies
That's why I find *"I'm surprised the DBAs don't tell you to ditch ORM if you want better speed"* excessive.
Pascal Thivent
@Pascal Thivent: Sorry, without the distinction of using stored procedures via ORM, the assumption will be that such isn't the case. The OP doesn't given any indication (to me) that they are using stored procedures via ORM.
OMG Ponies
+8  A: 

You are at an interesting juncture. Most Java developers use ORM tools because they don't know anything about databases, they don't want to learn anything about databases and especially they don't want to learn anything about the peculiarities of a specific proprietary DBMS. ORMs ostensibly shield us from all this.

But if you really want to understand how a query ought to perform you are going to have to understand how the Oracle database works. This is a good thing: you will definitely build better Hibernate apps if you work with the grain of the database.

Oracle's documentation set includes a volume on Performance Tuning. This is the place to start. Find out more. As others have said, the entry level tool is EXPLAIN PLAN. Another essential read is Oracle's Database Concepts Guide. A vital aspect of tuning is understanding the logical and physical architecture of the database. I also agree with DCooke's recommendation of Tom Kyte's book.

Bear in mind that there are contractors and consultants who make a fine living out of Oracle performance tuning. If it was easy they would be a lot poorer. But you can certainly give yourself enough knowledge to force those pesky DBAs to engage with you properly.

APC
+1 for the self-accountability approach. Another good resource is Tom Kyte's "Expert Oracle Database Architecture", which despite it's title, is written for developers to give them an understanding of the Oracle architecture and how to write effective apps.
DCookie
+5  A: 

DCookie, OMG, and APC all get +1 for their answers. I've been on the DBA side of Hibernate apps and as Tom Kyte puts it, there is no "fast=true" parameter that can take inefficient SQL and make it run faster. Once in a while I've been able to capture a problematic Hibernate query and use the Oracle SQL analyzer to generate a SQL Profile for that statement that improves performance. This profile is a set of hints that "intercept" the optimizer's generation of an execution plan and force it (with no change to the app) to a better one that would normally be overlooked by the optimizer for some reason. Still, these findings are the exception rather than the rule for poorly-performing SQL.

One thing you can do as a developer who presumably understands the data better than the ORM layer is to write efficient views to address specific query problems and present these views to Hibernate.

A very specific issue to watch out for is Oracle DATE (not TIMESTAMP) columns that end up in the Hibernate-generated queries and are compared to bind variables in a WHERE clause - the type mismatch with Java timestamp data types will prevent the use of indexes on these columns.

dpbradley
+1 for the DBA perspective. I liked the reference to Tom Kyte's "fast=true" parameter - always got a chuckle out of that. Another Kyte-ism is that nearly all (80-100%) performance issues are application and design issues, not database issues. Poorly structured queries, not using bind variables, etc. all contribute.
DCookie