Learning to rephrase equivalent queries?

From: Jay Levitt <jay(dot)levitt(at)gmail(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Learning to rephrase equivalent queries?
Date: 2011-11-10 13:04:53
Message-ID: 4EBBCBF5.3090704@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sometimes the planner can't find the most efficient way to execute your
query. Thanks to relational algebra, there may be other, logically
equivalent queries that it DOES know how to optimize.

But I don't know relational algebra. yet. (Date/Codd is a sleeping pill.)
I need more experience first.

Are there blogs, guides, rules of thumb, common refactoring patterns out
there somewhere? I'm looking for a list of basic equalities, the SQL
equivalent of:

a^2 - b^2 = (a + b)(a - b)

Such as:

SELECT l.*
FROM t_left l
LEFT JOIN
t_right r
ON r.value = l.value
WHERE r.value IS NULL

=

SELECT l.*
FROM t_left l
WHERE NOT EXISTS
(
SELECT NULL
FROM t_right r
WHERE r.value = l.value
)

All my searches for "SQL Refactoring" seem to lead to either (a) discussions
about how many characters an alias should be and how you should indent
things, or (b) tutorials on normalization. This isn't that. I want to
learn ways to restate my queries.

Any tips?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2011-11-10 13:07:16 Re: How to list installed pl languages
Previous Message Alexander Burbello 2011-11-10 11:25:42 Exp/Imp data with blobs