21

Are there any formal techniques for refactoring SQL similar to this list here that is for code?

I am currently working on a massive query for a particular report and I'm sure there's plenty of scope for refactoring here which I'm just stumbling through myself bit by bit.

3

4 Answers 4

8

I have never seen an exhaustive list like the sample you provided.

The most effective way to refactor sql that I have seen is to use the with statement. It allows you to break the sql up into manageable parts, which frequently can be tested independently. In addition it can enable the reuse of query results, sometimes by the use of a system temporary table. It is well worth the effort to examine.

Here is a silly example

WITH 
mnssnInfo AS
(
    SELECT SSN, 
           UPPER(LAST_NAME), 
           UPPER(FIRST_NAME), 
           TAXABLE_INCOME,          
           CHARITABLE_DONATIONS
    FROM IRS_MASTER_FILE
    WHERE STATE = 'MN'                 AND -- limit to Minne-so-tah
          TAXABLE_INCOME > 250000      AND -- is rich 
          CHARITABLE_DONATIONS > 5000      -- might donate too
),
doltishApplicants AS
(
    SELECT SSN, SAT_SCORE, SUBMISSION_DATE
    FROM COLLEGE_ADMISSIONS
    WHERE SAT_SCORE < 100          -- Not as smart as the average moose.
),
todaysAdmissions AS
(
    SELECT doltishApplicants.SSN, 
           TRUNC(SUBMISSION_DATE)  SUBMIT_DATE, 
           LAST_NAME, FIRST_NAME, 
           TAXABLE_INCOME
    FROM mnssnInfo,
         doltishApplicants
    WHERE mnssnInfo.SSN = doltishApplicants.SSN
)
SELECT 'Dear ' || FIRST_NAME || 
       ' your admission to WhatsaMattaU has been accepted.'
FROM todaysAdmissions
WHERE SUBMIT_DATE = TRUNC(SYSDATE)    -- For stuff received today only

One of the other things I like about it, is that this form allows you to separate the filtering from the joining. As a result, you can frequently copy out the subqueries, and execute them stand alone to view the result set associated with them.

4
  • 2
    Good, if they're on SQL 2005+. Pre then you can't use With statements so temp tables are your friend. (Note that for testing, it can be best to start it off as temp and convert to withs when you're happy with them so you can leave your built-up tables in memory and waiting rather than having to rebuild them separately each time you want to check something.)
    – eftpotrm
    Mar 24, 2010 at 14:27
  • You can also use views and inline table-valued functions instead of CTEs.
    – Sam
    Dec 11, 2014 at 4:45
  • In addition it can enable the reuse of query results, sometimes by the use of a system temporary table - This is false. In every SQL implementation I know, CTEs are inlined; They are not cached.
    – J. Mini
    Dec 10, 2023 at 14:57
  • @j. Oracle can store the results in a temp table.
    – EvilTeach
    Dec 11, 2023 at 14:42
3

There is a book on the subject: "Refactoring Databases". I haven't read it, but it got 4.5/5 stars on Amazon and is co-authored by Scott Ambler, which are both good signs.

1
  • 1
    It's also from the Martin Fowler signature series, which is another good sign - I don't know of anyone who has done more for popularising refactoring as a technique for clean code than Fowler has.
    – Myles
    Dec 17, 2020 at 11:04
1

Not that I've ever found. I've mostly done SQL Server work and the standard techniques are:

  • Parameterise hard-coded values that might change (so the query can be cached)
  • Review the execution plan, check where the big monsters are and try changing them
  • Index tuning wizard (but beware you don't cause chaos elsewhere from any changes you make for this)

If you're still stuck, many reports don't depend on 100% live data - try precalculating portions of the data (or the whole lot) on a schedule such as overnight.

1
  • 6
    Sounds like you're talking about optimising (improving performance) rather than refactoring (improving design).
    – gkrogers
    Jun 28, 2012 at 22:57
1

Not about techniques as much, but this question might help you find SQL refactoring tools:

Is there a tool for refactoring SQL, a bit like a ReSharper for SQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.