What generic techniques can be applied to optimize SQL queries?

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

提交回复
热议问题