简体   繁体   English

重构 SQL

[英]Refactoring SQL

Are there any formal techniques for refactoring SQL similar to this list here that is for code?是否有类似的重构这份名单SQL任何正式的技术在这里也就是代码?

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.我目前正在对特定报告进行大量查询,我确信这里有很多重构空间,我只是一点一点地绊倒自己。

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 .我见过的最有效的 sql 重构方法是使用with 语句 It allows you to break the sql up into manageable parts, which frequently can be tested independently.它允许您将 sql 分解为可管理的部分,这些部分通常可以独立测试。 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.因此,您可以经常复制子查询,并单独执行它们以查看与它们关联的结果集。

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.我还没读过,但它在亚马逊上获得了 4.5/5 星,并且是由 Scott Ambler 合着的,这两个都是好兆头。

Not that I've ever found.不是我曾经发现过。 I've mostly done SQL Server work and the standard techniques are:我主要完成了 SQL Server 工作,标准技术是:

  • 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.如果您仍然遇到问题,许多报告并不依赖于 100% 的实时数据 - 尝试按计划(例如隔夜)预先计算部分数据(或全部)。

Not about techniques as much, but this question might help you find SQL refactoring tools:与技术无关,但这个问题可能会帮助您找到 SQL 重构工具:

Is there a tool for refactoring SQL, a bit like a ReSharper for SQL 有没有重构SQL的工具,有点像SQL的ReSharper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM