原文地址:http://www.codeproject.com/Tips/1023621/SQL-Performance-Improvement-Techniques

This article provides various options to improve the performance in database.

1) Re-Write Query:

If any query is taking much time to execute then the first step is to rewrite the query. Perform thorough analysis and move towards identifying the root cause. The below are few guidelines to improve the performance of a query.

  • Avoid * in SELECT and specify the column names when dealing with JOINs on multiple tables.
  • Avoid repeated logic, unnecessary subqueries and unnecessary JOINs
  • Some cases EXIST will benefit instead of JOIN
  • Use UNION ALL instead of UNION
  • Use EXISTS instead of IN when necessary
  • Use WITH clause (Oracle) or Common Table Expressions(Sql Server)
  • Order or position of the columns in WHERE clause would play vital role to improve the performance and ensure the proper index is being used by the query.
  • In Sql server, Adopt using SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED or WITH(NOLOCK).
  • Use hints if necessary. There are table hints, query hints and plan hints.

2) Create a Highly Selective Index:

Index helps to retrieve the data fast and basically to speed up the searches/queries. The below are the few guide lines to create index.

  • When the table is large and frequent selectivity of a table is less than 10%.
  • Do not create index on low cardinality columns and also the index is not required for small tables.
  • Frequently used columns in the WHERE clause and Columns used in joins for multiple tables.
  • Order or position of a column in an index also plays a vital role. In general, you should put the column expected to be used most often first in the index.
  • Limit the number of indexes on a table. The more indexes more overhead as the indexes need to be updated on every DML operation.

3) Limit the Number of Columns and Rows:

In some cases the applications may not use all the columns and rows fetched from the database. Means pull only the required columns and required rows.

Example if query (SELECT *) is pulling more than 100 column and the application may not use those 100 column in the application. And if any application is displaying data in page wise format then better to retrieve the corresponding records of the page instead of retrieving all the records.

4) Temporary tables:

The temporary tables should be used when there is a strong reason. Basically if any long running query is used in many places in procedure/function then better we store the results of long running query in a temporary table and reuse it later. Once it is completed then delete it to free the memory and do not wait for the table to be automatically deleted when the connection is ended. Using index on the temporary tables will help when you deal with very large tables. The temporary table can be used as the alternative for the cursors.

5) Pre-Stage data:

There are some applications do large imports from database and perform selectivity based on the information stored in flat file. The data import is done for every 1000/2000 records which would result in to perform SELECT with JOINs on large tables multiple times. These JOINs would be executed multiple times i.e. the same operation is executing several times. This can be improved by loading the flat file data into a stage table and then perform SELECT with JOINs only once based on the data exist in stage table. This would significantly improve the performance.

6) Indexed/Materialized Views:

This technique is very much helpful when there is a search operation on multiple large tables and on various columns. Obviously when you perform search on multiple tables would take lot of time to complete the search operation. This can be improved by creating single indexed/materialized view which would load and consolidate the key columns data into one or two columns. Here search is performed on one/two columns of large view instead of various columns on multiple large tables. In case of materialized views an index needs to be created on the key columns explicitly to get better performance.

7) Index Optimization:

Over a period of time the data size keep on increasing and at the same time the index size is keep on increasing. The index would become more fragmented and database engine would perform unnecessary data reads. So the heavy fragmentation of an index would lead to slow down the performance. There are two options to reduce the fragmentation of the index.

  • Rebuild: Rebuild would drop the existing index and create a new index with updated data in the columns. It takes more server resources to perform the rebuild.
  • Reorganize: Reorganize is more light weight and performs the defragmentation of the index. The existing index is used to update the leaf pages. It is better to do reorganize on periodic basis instead of rebuild.

8) Index Statistics:

The creation of statistics would enable the database engine to use a highly efficient execution plan for a query. Basically index statistics maintains the distribution of the values of an index column i.e. the cardinality of different column values. This information is used by the database engine to determine execution plan that can be used for processing a query. The statistics would need regular/periodical updates as the distribution of the values changes.

9) Archive key tables:

As you are already know, the data and index size will keep on increasing day by day. When the application is functioning since many years then index optimization may not be a good choice to improve the performance as each key table might have billions of records with different indexes on those tables. It is time to archive the key tables and this solution is suitable only if the application is not using the very old records. Create a new archive table for storing the very old records with the same structure of the key table and then move all the old records into the new archive table.

Better to rebuild/reorganize the indexes on all the key tables once the old records are moved to the newly created archive table as it would free lot of memory occupied for storing indexes. This activity can performed periodically (once/twice in year) during the off hours.

Enjoy faster SQL!!

Please remember to evaluate each situation individually to see which method works best.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

SQL Performance Improvement Techniques(转)的更多相关文章

  1. 30 分钟快快乐乐学 SQL Performance Tuning

    转自:http://www.cnblogs.com/WizardWu/archive/2008/10/27/1320055.html 有些程序员在撰写数据库应用程序时,常专注于 OOP 及各种 fra ...

  2. 实战:ORACLE SQL Performance Analyzer

    通过 SPA,您能够依据各种更改类型(如初始化參数更改.优化器统计刷新和数据库升级)播放特定的 SQL 或整个 SQL 负载,然后生成比較报告,帮助您评估它们的影响. 在 Oracle Databas ...

  3. 11g SPA (sql Performance Analyze) 进行升级测试

    注;转自http://ju.outofmemory.cn/entry/77139 11G的新特性SPA(SQL Performance Analyze)现在被广泛的应用到升级和迁移的场景.当然还有一些 ...

  4. SQL Performance Analyzer

    SQL Performance Analyzer 系统发生变更,比如升级数据库.增加索引,都会可能导致sql的执行计划发生改变,从而影响sql的性能. 如果能预知系统变更会对sql的性能的影响,就可以 ...

  5. Inside Amazon's Kafkaesque "Performance Improvement Plans"

    Amazon CEO and brilliant prick Jeff Bezos seems to have lost his magic touch lately. Investors, empl ...

  6. A simple way to monitor SQL server SQL performance.

    This is all begins from a mail. ... Dear sir: This is liulei. Thanks for your help about last PM for ...

  7. 转://使用showplan.sql分析sql Performance

    在HelloDBA网站找到一个分析sql性能的工具—showplan,记录一下 showplan.sql下载路径:http://www.HelloDBA.com/Download/showplan.z ...

  8. 使用showplan.sql分析sql Performance

    在HelloDBA网站找到一个分析sql性能的工具-showplan,记录一下 showplan.sql下载路径:http://www.HelloDBA.com/Download/showplan.z ...

  9. PatentTips - Control register access virtualization performance improvement

    BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VMM) typically runs on a compute ...

随机推荐

  1. iOS_block内存分析

    ----------------------MRC情况下Block内存分析---------------------------- 1.如果在block中使用全局变量,他为了持有这个变量,会将对应的对 ...

  2. UIView的动画之初步学习

    animateWithDuration:<#(NSTimeInterval)#> delay:<#(NSTimeInterval)#> options:<#(UIView ...

  3. (转) launch failed.Binary not found in Linux/Ubuntu解决方案

    原地址: http://blog.csdn.net/abcjennifer/article/details/7573916 Linux下出现launch failed.Binary not found ...

  4. HTML5简单入门系列(四)

    前言 今天这篇内容主要讲述HTML 5 Web Worker(一种支持前端js多线程的技术). 工作线程(Web Worker) web worker介绍 W3C 在 HTML5 的规范中提出了工作线 ...

  5. [C++程序设计]用指针变量作函数参数接收数组地址

    #include <iostream> using namespace std; void select_sort(int *p, int n) { int i, j, k; ; i &l ...

  6. 重读LPTHW-Lesson18-21 函数

    1.def 定义函数,选取合适的函数名,原则是易于理解.阅读.函数名格式与变量命名格式相同,以字母开始,可以包含字母.数字.下划线.函数命名后,把参数放在()中,可以无参数.然后:结束函数命名,开始函 ...

  7. tomcat 项目部署问题

    我本地Tomcat版本:Apache Tomcat/8.0.3.0 服务器端:Apache Tomcat/6.0.37 JVM都是:1.7.0_40-b43 之前项目运行正常,在我更新了一些模块后,重 ...

  8. 图形性能(widgets的渲染性能太低,所以推出了QML,走硬件加速)和网络性能(对UPD性能有实测数据支持)

    作者:JasonWong链接:http://www.zhihu.com/question/37444226/answer/72007923来源:知乎著作权归作者所有,转载请联系作者获得授权. ---- ...

  9. JAVA的类加载器,详细解释

    JVM规范定义了两种类型的类装载器:启动内装载器(bootstrap)和用户自定义装载器(user-defined class loader). 一. ClassLoader基本概念 1.ClassL ...

  10. Linux系统编程(6)——文件系统

    计算机的文件系统是一种存储和组织计算机数据的方法,它使得对其访问和查找变得容易,文件系统使用文件和树形目录的抽象逻辑概念代替了硬盘和光盘等物理设备使用数据块的概念,用户使用文件系统来保存数据不必关心数 ...