Archive for November 23rd, 2005

November 23rd, 2005

How “distinct” hides bad sql

by Tim Cull

So we’ve got a new guy in the group who’s fresh out of college. Most people fresh out of college don’t know much about sql and he’s no exception, although he’s catching on.

So one of the first things I taught him was to avoid one of my pet peeves: “fixing” a bad sql statement by slapping a “distinct” on it. If you find yourself using distinct and you don’t know why, then that’s a sure sign that you’re hiding an accidental cartesian product that will eventually kill the performance of your database.

The very next day he sent me a link to this old post, about the very same thing:
http://www.onlamp.com/pub/a/onlamp/2004/09/30/from_clauses.html

I guess I’m not the only one annoyed by it. And I learned something, too, about how to decide to use a correlated subquery or not.