What generic techniques can be applied to optimize SQL queries?

后端 未结 11 1713
礼貌的吻别
礼貌的吻别 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:29

    There are a couple of things you can look at to optimize your query performance.

    1. Ensure that you just have the minimum of data. Make sure you select only the columns you need. Reduce field sizes to a minimum.

    2. Consider de-normalising your database to reduce joins

    3. Avoid loops (i.e. fetch cursors), stick to set operations.

    4. Implement the query as a stored procedure as this is pre-compiled and will execute faster.

    5. Make sure that you have the correct indexes set up. If your database is used mostly for searching then consider more indexes.

    6. Use the execution plan to see how the processing is done. What you want to avoid is a table scan as this is costly.

    7. Make sure that the Auto Statistics is set to on. SQL needs this to help decide the optimal execution. See Mike Gunderloy's great post for more info. Basics of Statistics in SQL Server 2005

    8. Make sure your indexes are not fragmented. Reducing SQL Server Index Fragmentation

    9. Make sure your tables are not fragmented. How to Detect Table Fragmentation in SQL Server 2000 and 2005
    0 讨论(0)
  • 2020-12-13 22:29

    In Oracle you can look at the explain plan to compare variations on your query

    0 讨论(0)
  • 2020-12-13 22:32
    • Use primary keys
    • Avoid select *
    • Be as specific as you can when building your conditional statements
    • De-normalisation can often be more efficient
    • Table variables and temporary tables (where available) will often be better than using a large source table
    • Partitioned views
    • Employ indices and constraints
    0 讨论(0)
  • 2020-12-13 22:32

    The obvious optimization for SELECT queries is ensuring you have indexes on columns used for joins or in WHERE clauses.

    Since adding indexes can slow down data writes you do need to monitor performance to ensure you don't kill the DB's write performance, but that's where using a good query analysis tool can help you balanace things accordingly.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题