Where Are The Wise Men?

Mike's Ramblings

Don't Fear the SQL

| Comments

When I hear people talk about the SQL problems, it's because someone wrote some hairy query that breaks and someone else has to debug. On our project, our problem is the opposite -- our SQL is too simple.

We have foreign keys, but never actually join tables together. If we have X and Y that we know is in Table1 and want the corresponding row in Table2 we query col1=X and col2=Y and get the whole object. Then when query Table2 for "table1_id="+table1Obj.getID() and then, finally, we have the object we were looking for, after two transactions on the DB and a worthless object later.

Madness!!

The first time I had a joined table (which was the right thing do to for that problem!) I heard a gasp in the room. No one made me change it, which was sort of surprising.

You may think that this simple SQL practice would keep our queries small. Not so -- I still get calls at 7:30am from the Production DBA saying that we have a query running for hours and that I need to kill the process. The problem that day? No index on that table, of course. So it's not a purposeful performance decision -- it comes from a lack of database knowledge.