`
ah_fu
  • 浏览: 227862 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

转:SQL Server DO's and DON'Ts

阅读更多
原文转自:http://www.codeproject.com/cs/database/sqldodont.asp



SQL Server DO's and DON'Ts

So, you are now the leader of a SQL Server based project and this is your first one, perhaps migrating from Access. Or maybe you have performance problems with your SQL Server and don't know what to do next. Or maybe you simply want to know of some design guidelines for solutions using SQL Server and designing Database Access Layers (DAL): this article is for you.

Even if you are not using SQL Server, most of these design guidelines apply to other DBMS, too: Sybase is a very similar environment for the programmer, and Oracle designs may benefit from this too. I won't show here how to use specific T-SQL tricks, nor won't give you miracle solutions for your SQL Server problem. This is by no means a complete, closed issue. What I intend to do is give you some advices for a sound design, with lessons learned through the last years of my life, seeing the same design errors being done again and again.

DO know your tools.

Please, don't underestimate this tip. This is the best of all of those you'll see in this article. You'd be surprised of how many SQL Server programmers don't even know all T-SQL commands and all of those effective tools SQL Server has.

"What? I need to spend a month learning all those SQL commands I'll never use???" you might say. No, you don't need to. But spend a weekend at MSDN and browse through all T-SQL commands: the mission here is to learn a lot of what can and what can't be done. And, in the future, when designing a query, you'll remember "Hey, there's this command that does exactly what I need", and then you'll refer again to MSDN to see its exact syntax.

In this article I'll assume that you already know the T-SQL syntax or can find about it on MSDN.

DON'T use cursors

Let me say it again: DON'T use cursors. They should be your preferred way of killing the performance of an entire system. Most beginners use cursors and don't realize the performance hit they have. They use memory; they lock tables in weird ways, and they are slow. Worst of all, they defeat most of the performance optimization your DBA can do. Did you know that every FETCH being executed has about the same performance of executing a SELECT? This means that if your cursor has 10,000 records, it will execute about 10,000 SELECTs! If you can do this in a couple of SELECT, UPDATE or DELETE, it will be much faster.

Beginner SQL programmers find in cursors a comfortable and familiar way of coding. Well, unfortunately this lead to bad performance. The whole purpose of SQL is specifying what you want, not how it should be done.

I've once rewritten a cursor-based stored procedure and substituted some code for a pair of traditional SQL queries. The table had only 100,000 records and the stored procedure used to take 40 minutes to process. You should see the face of the poor programmer when the new stored procedure took 10 seconds to run!

Sometimes it's even faster to create a small application that gets all the data, proccess it and update the server. T-SQL was not done with loop performance in mind.

If you are reading this article, I need to mention: there is no good use for cursors; I have never seen cursors being well used, except for DBA work. And good DBAs, most of the time, know what they are doing. But, if you are reading this, you are not a DBA, right?

DO normalize your tables

There are two common excuses for not normalizing databases: performance and pure laziness. You'll pay for the second one sooner or later; and, about performance, don't optimize what's not slow. Often I see programmers de-normalizing databases because "this will be slow". And, more frequent than the inverse, the resulting design is slower. DBMSs were designed to be used with normalized databases, so design with normalization in mind.

DON'T SELECT *

This is hard to get used, I know. And I confess: often I use it; but try to specify only the columns you'll need. This will:

  1. Reduce memory consumption and network bandwidth
  2. Ease security design
  3. Gives the query optimizer a chance to read all the needed columns from the indexes

DO know how your data will be/is being acessed

A robust index design is one of the good things you can do for your database. And doing this is almost an art form. Everytime you add an index to a table, things get faster on SELECT, but INSERT and DELETE will be much slower. There's a lot of work in building and mantaining indexes. If you add several indexes to a table to speed your SELECT, you'll soon notice locks being held for a long time while updating indexes. So, the question is: what is being done with this table? Reading or Updating data? This question is tricky, specially with the DELETE and UPDATE, because they often involve a SELECT for the WHERE part and after this they update the table.

DON'T create an index on the "Sex" column

This is useless. First, let's understand how indexes speed up table access. You can see indexes as a way of quickly partitioning a table based on a criteria. If you create an index with a column like "Sex", you'll have only two partitions: Male and Female. What optimization will you have on a table with 1,000,000 rows? Remember, mantaining an index is slow. Always design your indexes with the most sparse columns first and the least sparse columns last, e.g, Name + Province + Sex.

DO use transactions

Specially on long-running queries. This will save you when things get wrong. Working with data for some time you'll soon discover some unexpected situation which will make your stored procured crash.

DO beware of deadlocks

Always access your tables on the same order. When working with stored procedures and transactions, you may find this soon. If you lock the table A then table B, always lock them in this very same order in all stored procedures. If you, by accident, lock the table B and then table A in another procedure some day you'll have a deadlock. Deadlocks can be tricky to find if the lock sequence is not carefully designed.

DON'T open large recordsets

A common request on programming forums is: "How can I quickly fill this combo with 100,00 items?". Well, this is an error. You can't and you shouldn't. First, your user will hate browsing through 100,000 records to find the right one. A better UI is needed here, because you should ideally show no more that 100 or 200 records to your users.

DON'T use server side cursors

Unless you know what your are doing. Client side cursors often (not always) put less overhead on the network and on the server, and reduce locking time.

DO use parametrized queries

Sometimes I see in programming forums, questions like: "My queries are failing with some chars, e.g. quotes. How can I avoid it?". And a common answer is: "Replace it by double quotes". Wrong. This is only a workaround and will still fail with other chars, and will introduce serious security bugs. Besides this, it will trash the SQL Server caching system, which will cache several similar queries, instead of caching only one. Learn how to use parameterized queries (in ADO, through the use of the Command Object, or in ADO.NET the SqlCommand) and never have these problems again.

DO always test with large databases

It's a common pattern programmers developing with a small test database, and the end user using large databases. This is an error: disk is cheap, and performance problems will only be noticed when it's too late.

DON'T import bulk data with INSERT

Unless strictly necessary. Use DTS or the BCP utility and you'll have both a flexible and fast solution.

DO beware of timeouts

When querying a database, the default timeout is often low, like 15 seconds or 30 seconds. Remember that report queries may run longer than this, specially when your database grows.

DON'T ignore simultaneous editing

Sometimes two users will edit the same record at the same time. When writing, the last writer wins and some of the updates will be lost. It's easy to detect this situation: create a timestamp column and check it before you write. If possible, merge changes. If there is a conflict, prompt the user for some action.

DON'T do SELECT max(ID) from Master when inserting in a Detail table.

This is another common mistake, and will fail when two users are inserting data at the same time. Use one of SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY. Avoid @@IDENTITY if possible because it can introduce some nasty bugs with triggers.

DO Avoid NULLable columns

When possible. They consume an extra byte on each NULLable column in each row and have more overhead associated when querying data. The DAL will be harder to code, too, because everytime you access this column you'll need to check

I'm not saying that NULLs are the evil incarnation, like some people say. I believe they can have good uses and simplify coding when "missing data" is part of your business rules. But sometimes NULLable columns are used in situations like this:

CustomerName1
CustomerAddress1
CustomerEmail1
CustomerName2
CustomerAddress2
CustomerEmail3
CustomerName1
CustomerAddress2
CustomerEmail3

This is horrible. Please, don't do this, normalize your table. It will be more flexible and faster, and will reduce the NULLable columns.

DON'T use the TEXT datatype

Unless you are using it for really large data. The TEXT datatype is not flexible to query, is slow and wastes a lot of space if used incorrectly. Sometimes a VARCHAR will handle your data better.

DON'T use temporary tables

Unless strictly necessary. Often a subquery can substitute a temporary table. They induce overhead and will give you a big headache when programming under COM+ because it uses a database connection pool and temporary tables will last forever. In SQL Server 2000, there are alternatives like the TABLE data type which can provide in-memory solutions for small tables inside stored procedures too.

DO learn how to read a query execution plan

The SQL Server query analyzer is your friend, and you'll learn a lot of how it works and how the query and index design can affect performance through it.

DO use referential integrity

This can be a great time saver. Define all your keys, unique constraints and foreign keys. Every validation you create on the server will save you time in the future.

Conclusion

As I've said before, this is by no means a complete SQL Server performance and best practices guide. This would take a complete book to cover. But I really believe that this is a good start, and if you follow these practices, surely you will have much less trouble in the future.

分享到:
评论

相关推荐

    sqlserver驱动包:sqljdbc4.jar

    SQL Server驱动包`sqljdbc4.jar`是微软官方提供的Java数据库连接器(JDBC),用于在Java应用程序中与Microsoft SQL Server进行通信。JDBC是Java编程语言中的一个标准API,它使得开发人员能够以标准化的方式访问各种...

    sqlserver自动生成sql语句工具sqlserver转oracle

    描述中提到的"sqlserver转oracle"则是指将SQL Server数据库的内容迁移到Oracle数据库的过程。这个过程涉及到SQL语法的转换,因为SQL Server和Oracle虽然都遵循SQL标准,但各自的语法特性和函数库有所不同。例如,SQL...

    mysql数据库转换成sql server

    MySQL数据库和SQL Server是两种广泛使用的数据库管理系统,它们在语法、功能和管理上存在显著差异。将MySQL数据库转换成SQL Server是一项常见的需求,特别是在企业级应用迁移或整合项目中。以下是一些关于如何进行...

    sql server 2008数据库转sql server 2005数据库

    "sql server 2008数据库转sql server 2005数据库" 本文将详细介绍四种将 SQL Server 2008 数据库转换为 SQL Server 2005 数据库的方法。这些方法都是通过实践和总结得出的,旨在帮助读者快速、可靠地将 SQL Server ...

    mysql转换为sqlserver

    MySQL到SQL Server的转换是一个常见的数据库迁移任务,尤其在企业级应用中,可能因为业务需求、性能优化或统一管理等原因需要进行这样的迁移。这个过程涉及多个步骤,包括数据的导出、格式转换以及导入到新的SQL ...

    sql server2019安装包

    6. **SQL Server on Linux and Docker**:SQL Server 2019继续支持在Linux操作系统和Docker容器上运行,增强了跨平台兼容性。 7. **SQL Server Management Studio (SSMS)**:SSMS是用于管理SQL Server实例的重要...

    kettle连接sqlserver连不上-jtds.jar包

    对于SQL Server,一个常用的驱动是jTDS,这是一个开源、纯Java实现的JDBC驱动,特别适合与Microsoft SQL Server和Sybase Adaptive Server Anywhere交互。 描述中提到,用户需要自行下载jTDS驱动(jtds.jar),这是...

    mysql转化成sql server sql转化成mysql工具

    MySQL和SQL Server是两种非常流行的DBMS,分别由Oracle公司和Microsoft公司开发。当需要在两者之间进行数据迁移时,就需要用到专门的转换工具。标题提到的“mysql转化成sql server”和“sql转化成mysql工具”就是...

    SQL Server Native Client 10.0 驱动

    这个驱动程序是SQL Server 2008的一部分,支持ODBC(Open Database Connectivity)和OLE DB(Object Linking and Embedding, Database)两种数据访问接口,使得开发者可以选择最适合他们应用的技术来连接和操作SQL ...

    这是sql server各个版本的下载地址

    以下是 SQL Server 各个版本的下载地址,包括 SQL Server 2000、SQL Server 2005、SQL Server 2008、SQL Server 2008R2、SQL Server 2012、SQL Server 2014、SQL Server 2016 等版本的下载链接和密码。 SQL Server ...

    Sql Server 2014 安装包

    SQL Server 2014是微软推出的一款关系型数据库管理系统,它在企业级数据管理和分析领域扮演着重要的角色。此安装包包含两个主要组件:SQL Management Studio和SQL Server Express。 1. **SQL Management Studio ...

    sqlserver2019.rar

    SQL Server 2019是微软公司发布的一款企业级数据库管理系统,它在SQL Server 2017的基础上进一步增强了性能、安全性和可扩展性。在这个压缩包“sqlserver2019.rar”中,包含了两个重要的安装文件:SSMS-Setup-CHS....

    SQL server2005(百度云下载哦)包含64位和32位的安装包

    SQL Server 2005是微软公司推出的一款关系型数据库管理系统,它在企业级数据管理、分析和报告方面具有广泛的应用。SQL Server 2005的发布为开发者提供了更强大的工具和更丰富的功能,使其在数据库管理和开发领域中...

    oracle到sqlserver存储过程语法转换

    ### Oracle到SQL Server存储过程语法转换详解 在数据库迁移项目中,从Oracle迁移到SQL Server是一种常见的场景。本文旨在提供一份详细的指南,帮助开发者更好地理解这两种数据库系统在存储过程方面的语法差异,并...

    数据库转换工具MySQL 转为SqlServer 脚本

    本主题聚焦于如何使用特定工具或脚本将MySQL数据库转换为SQL Server,这一过程通常称为“数据库迁移”。以下是关于这个过程的一些关键知识点: 1. **数据库迁移的目的**:迁移可能出于多种原因,如业务增长、性能...

    SQL Server Native Client 11-sqlncli-11驱动

    它整合了ODBC(Open Database Connectivity)和OLE DB(Object Linking and Embedding, Database)接口,提供了对SQL Server 2008 R2及更高版本的全面支持。这个驱动程序允许应用程序连接到SQL Server,执行查询,...

    SQL Server 2000帮助(中文)

    SQL Server 2000是微软公司推出的一款关系型数据库管理系统,它在20世纪末至21世纪初广泛应用于企业级数据管理。这个“SQL Server 2000帮助(中文)”压缩包文件提供了关于该系统全面的中文参考资料,非常适合初学者...

    error:40-无法打开到SQL server的连接

    在连接到Sql Server 2005时,在默认的设置下Sql Server不允许进行远程连接可能会导致此失败。 (provider:命名管道提供程序,error:40-无法打开到SQL server的连接) 处理:1、在配置工具->Sql server 外围应用...

    kettle链接SQL server驱动 sqljdbc

    SQL Server是微软公司推出的一款关系型数据库管理系统,广泛应用于企业级数据存储与管理。在Kettle中配置SQL Server数据库连接时,我们需要依赖特定的数据库驱动,这就是SQL JDBC驱动。 SQL JDBC驱动是微软提供的...

Global site tag (gtag.js) - Google Analytics