What generic techniques can be applied to optimize SQL queries?

后端 未结 11 1720
礼貌的吻别
礼貌的吻别 2020-12-13 21:28

What techniques can be applied effectively to improve the performance of SQL queries? Are there any general rules that apply?

11条回答
  •  温柔的废话
    2020-12-13 22:33

    Use a with statment to handle query filtering. Limit each subquery to the minimum number of rows possible. then join the subqueries.

    WITH
    master AS
    (
        SELECT SSN, FIRST_NAME, LAST_NAME
        FROM MASTER_SSN
        WHERE STATE = 'PA' AND
              GENDER = 'M'
    ),
    taxReturns AS
    (
        SELECT SSN, RETURN_ID, GROSS_PAY
        FROM MASTER_RETURNS
        WHERE YEAR < 2003 AND
              YEAR > 2000
    )
    SELECT *
    FROM master,
         taxReturns
    WHERE master.ssn = taxReturns.ssn
    

    A subqueries within a with statement may end up as being the same as inline views, or automatically generated temp tables. I find in the work I do, retail data, that about 70-80% of the time, there is a performance benefit.

    100% of the time, there is a maintenance benefit.

提交回复
热议问题