`
mywebcode
  • 浏览: 1003823 次
文章分类
社区版块
存档分类
最新评论

SQL Server 设计规则

 
阅读更多

SQL Server 设计规则

如果你正在负责一个基于SQL Server的项目,或者你刚刚接触SQL Server,你都有可能要面临一些数据库性能的问题,这篇文章会为你提供一些有用的指导(其中大多数也可以用于其它的DBMS)。

一、了解你用的工具

不要轻视这一点,这是我在这篇文章中讲述的最关键的一条。也许你也看到有很多的SQL Server程序员没有掌握全部的T-SQL命令和SQL Server提供的那些有用的工具。

“什么?我要浪费一个月的时间来学习那些我永远也不会用到的SQL命令???”,你也许会这样说。对的,你不需要这样做。但是你应该用一个周末浏览所有的T- SQL命令。在这里,你的任务是了解,将来,当你设计一个查询时,你会记起来:“对了,这里有一个命令可以完全实现我需要的功能”,于是,到MSDN查看这个命令的确切语法。

二、不要使用游标

让我再重复一遍:不要使用游标。如果你想破坏整个系统的性能的话,它们倒是你最有效的首选办法。大多数的初学者都使用游标,而没有意识到它们对性能造成的影响。它们占用内存,还用它们那些不可思议的方式锁定表,另外,它们简直就像蜗牛。而最糟糕的是,它们可以使你的DBA所能做的一切性能优化等于没做。不知你是否知道每执行一次FETCH就等于执行一次SELECT命令?这意味着如果你的游标有 10000条记录,它将执行10000次SELECT!如果你使用一组SELECT、UPDATE或者DELETE来完成相应的工作,那将有效率的多。

初学者一般认为使用游标是一种比较熟悉和舒适的编程方式,可很不幸,这会导致糟糕的性能。显然,SQL的总体目的是你要实现什么,而不是怎样实现。

我曾经用T-SQL重写了一个基于游标的存储过程,那个表只有100,000条记录,原来的存储过程用了40分钟才执行完毕,而新的存储过程只用了10秒钟。在这里,我想你应该可以看到一个不称职的程序员究竟在干了什么!!!

我们可以写一个小程序来取得和处理数据并且更新数据库,这样做有时会更有效。记住:对于循环,T-SQL无能为力。

我再重新提醒一下:使用游标没有好处。除了DBA的工作外,我从来没有看到过使用游标可以有效的完成任何工作。

三、规范化你的数据表

为什么不规范化数据库?大概有两个借口:出于性能的考虑和纯粹因为懒惰。至于第二点,你迟早得为此付出代价。而关于性能的问题,你不需要优化根本就不慢的东西。我经常看到一些程序员“反规范化”数据库,他们的理由是“原来的设计太慢了”,可结果却常常是他们让系统更慢了。DBMS被设计用来处理规范数据库的,因此,记住:按照规范化的要求设计数据库。

四、不要使用SELECT *

这点不太容易做到,我太了解了,因为我自己就经常这样干。可是,如果在SELECT中指定你所需要的列,那将会带来以下的好处:

1 减少内存耗费和网络的带宽

2 你可以得到更安全的设计

3 给查询优化器机会从索引读取所有需要的列

五、了解你将要对数据进行的操作

为你的数据库创建一个健壮的索引,那可是功德一件。可要做到这一点简直就是一门艺术。每当你为一个表添加一个索引,SELECT会更快了,可INSERT和 DELETE却大大的变慢了,因为创建了维护索引需要许多额外的工作。显然,这里问题的关键是:你要对这张表进行什么样的操作。这个问题不太好把握,特别是涉及DELETE和UPDATE时,因为这些语句经常在WHERE部分包含SELECT命令。

六、不要给“性别”列创建索引

首先,我们必须了解索引是如何加速对表的访问的。你可以将索引理解为基于一定的标准上对表进行划分的一种方式。如果你给类似于“性别”这样的列创建了一个索引,你仅仅是将表划分为两部分:男和女。你在处理一个有1,000,000条记录的表,这样的划分有什么意义?记住:维护索引是比较费时的。当你设计索引时,请遵循这样的规则:根据列可能包含不同内容的数目从多到少排列,比如:姓名+省份+性别。

七、使用事务

请使用事务,特别是当查询比较耗时。如果系统出现问题,这样做会救你一命的。一般有些经验的程序员都有体会-----你经常会碰到一些不可预料的情况会导致存储过程崩溃。

八、小心死锁

按照一定的次序来访问你的表。如果你先锁住表A,再锁住表B,那么在所有的存储过程中都要按照这个顺序来锁定它们。如果你(不经意的)某个存储过程中先锁定表B,再锁定表A,这可能就会导致一个死锁。如果锁定顺序没有被预先详细的设计好,死锁是不太容易被发现的。

九、不要打开大的数据集

一个经常被提出的问题是:我怎样才能迅速的将100000条记录添加到ComboBox中?这是不对的,你不能也不需要这样做。很简单,你的用户要浏览100000条记录才能找到需要的记录,他一定会诅咒你的。在这里,你需要的是一个更好的UI,你需要为你的用户显示不超过100或200条记录。

十、不要使用服务器端游标

与服务器端游标比起来,客户端游标可以减少服务器和网络的系统开销,并且还减少锁定时间。

十一、使用参数查询

有时,我在CSDN技术论坛看到类似这样的问题:“SELECT * FROM a WHERE a.id='A'B,因为单引号查询发生异常,我该怎么办?”,而普遍的回答是:用两个单引号代替单引号。这是错误的。这样治标不治本,因为你还会在其他一些字符上遇到这样的问题,更何况这样会导致严重的bug,除此以外,这样做还会使SQLServer的缓冲系统无法发挥应有的作用。使用参数查询, 釜底抽薪,这些问题统统不存在了。

十二、在程序编码时使用大数据量的数据库

程序员在开发中使用的测试数据库一般数据量都不大,可经常的是最终用户的数据量都很大。我们通常的做法是不对的,原因很简单:现在硬盘不是很贵,可为什么性能问题却要等到已经无可挽回的时候才被注意呢?

十三、不要使用INSERT导入大批的数据

请不要这样做,除非那是必须的。使用UTS或者BCP,这样你可以一举而兼得灵活性和速度。

十四、注意超时问题

查询数据库时,一般数据库的缺省都比较小,比如15秒或者30秒。而有些查询运行时间要比这长,特别是当数据库的数据量不断变大时。

十五、不要忽略同时修改同一记录的问题

有时候,两个用户会同时修改同一记录,这样,后一个修改者修改了前一个修改者的操作,某些更新就会丢失。处理这种情况不是很难:创建一个timestamp字段,在写入前检查它,如果允许,就合并修改,如果存在冲突,提示用户。

十六、在细节表中插入纪录时,不要在主表执行SELECT MAX(ID)

这是一个普遍的错误,当两个用户在同一时间插入数据时,这会导致错误。你可以使用SCOPE_IDENTITY,IDENT_CURRENT和IDENTITY。如果可能,不要使用IDENTITY,因为在有触发器的情况下,它会引起一些问题(详见这里的讨论)。

十七、避免将列设为NULLable

如果可能的话,你应该避免将列设为NULLable。系统会为NULLable列的每一行分配一个额外的字节,查询时会带来更多的系统开销。另外,将列设为NULLable使编码变得复杂,因为每一次访问这些列时都必须先进行检查。

我并不是说NULLS是麻烦的根源,尽管有些人这样认为。我认为如果你的业务规则中允许“空数据”,那么,将列设为NULLable有时会发挥很好的作用,但是,如果在类似下面的情况中使用NULLable,那简直就是自讨苦吃。

CustomerName1

CustomerAddress1

CustomerEmail1

CustomerName2

CustomerAddress2

CustomerEmail3

CustomerName1

CustomerAddress2

CustomerEmail3

如果出现这种情况,你需要规范化你的表了。

十八、尽量不要使用TEXT数据类型

除非你使用TEXT处理一个很大的数据,否则不要使用它。因为它不易于查询,速度慢,用的不好还会浪费大量的空间。一般的,VARCHAR可以更好的处理你的数据。

十九、尽量不要使用临时表

尽量不要使用临时表,除非你必须这样做。一般使用子查询可以代替临时表。使用临时表会带来系统开销,如果你是用COM+进行编程,它还会给你带来很大的麻烦,因为COM+使用数据库连接池而临时表却自始至终都存在。SQL Server提供了一些替代方案,比如Table数据类型。

二十、学会分析查询

SQL Server查询分析器是你的好伙伴,通过它你可以了解查询和索引是如何影响性能的。

二十一、使用参照完整性

定义主健、唯一性约束和外键,这样做可以节约大量的时间。


SQLServer DO's and DON'Ts

So, you are nowthe leader of a SQL Server based project and this is your first one, perhapsmigrating from Access. Or maybe you have performance problems with your SQLServer and don't know what to do next. Or maybe you simply want to know of somedesign guidelines for solutions using SQL Server and designing Database AccessLayers (DAL): this article is for you.

Even if you arenot 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 maybenefit from this too. I won't show here how to use specific T-SQL tricks, norwon't give you miracle solutions for your SQL Server problem. This is by nomeans a complete, closed issue. What I intend to do is give you some advicesfor 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'tunderestimate this tip. This is the best of all of those you'll see in thisarticle. You'd be surprised of how many SQL Server programmers don't even knowall T-SQL commands and all of those effective tools SQL Server has.

"What? Ineed 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 browsethrough all T-SQL commands: the mission here is to learn a lot of what can andwhat 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 thenyou'll refer again to MSDN to see its exact syntax.

In this articleI'll assume that you already know the T-SQL syntax or can find about it onMSDN.

DON'T use cursors

Let me say itagain: DON'T use cursors. They should be your preferred way of killing theperformance of an entire system. Most beginners use cursors and don't realizethe 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 performanceoptimization your DBA can do. Did you know that every FETCH being executed hasabout the same performance of executing a SELECT? This means that if yourcursor has 10,000 records, it will execute about 10,000 SELECTs! If you can dothis in a couple of SELECT, UPDATE or DELETE, it will be much faster.

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

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

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

If you arereading this article, I need to mention: there is no good use for cursors; Ihave 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, youare not a DBA, right?

DO normalize your tables

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

DON'T SELECT *

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

1. Reduce memory consumption and networkbandwidth

2. Ease security design

3. Gives the query optimizer a chance toread all the needed columns from the indexes

DO know how your data willbe/is being acessed

A robust indexdesign is one of the good things you can do for your database. And doing thisis almost an art form. Everytime you add an index to a table, things get fasteron SELECT, but INSERT and DELETE will be much slower. There's a lot of work inbuilding and mantaining indexes. If you add several indexes to a table to speedyour SELECT, you'll soon notice locks being held for a long time while updatingindexes. So, the question is: what is being done with this table? Reading orUpdating data? This question is tricky, specially with the DELETE and UPDATE,because they often involve a SELECT for the WHERE part and after this theyupdate 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 indexesas a way of quickly partitioning a table based on a criteria. If you create anindex with a column like "Sex", you'll have only two partitions: Maleand 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 mostsparse columns first and the least sparse columns last, e.g, Name + Province +Sex.

DO use transactions

Specially onlong-running queries. This will save you when things get wrong. Working withdata for some time you'll soon discover some unexpected situation which willmake your stored procured crash.

DO beware of deadlocks

Always accessyour tables on the same order. When working with stored procedures andtransactions, 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, byaccident, lock the table B and then table A in another procedure some day you'llhave a deadlock. Deadlocks can be tricky to find if the lock sequence is notcarefully designed.

DON'T open large recordsets

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

DON'T use server side cursors

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

DO use parametrized queries

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

DO always test with largedatabases

It's a commonpattern programmers developing with a small test database, and the end userusing large databases. This is an error: disk is cheap, and performanceproblems will only be noticed when it's too late.

DON'T import bulk data withINSERT

Unless strictlynecessary. Use DTS or the BCP utility and you'll have both a flexible and fastsolution.

DO beware of timeouts

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

DON'T ignore simultaneousediting

Sometimes twousers will edit the same record at the same time. When writing, the last writerwins 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, mergechanges. If there is a conflict, prompt the user for some action.

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

This is anothercommon mistake, and will fail when two users are inserting data at the sametime. 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 moreoverhead 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 sayingthat NULLs are the evil incarnation, like some people say. I believe they canhave good uses and simplify coding when "missing data" is part ofyour business rules. But sometimes NULLable columns are used in situations likethis:

CustomerName1

CustomerAddress1

CustomerEmail1

CustomerName2

CustomerAddress2

CustomerEmail3

CustomerName1

CustomerAddress2

CustomerEmail3

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

DON'T use the TEXT datatype

Unless you areusing it for really large data. The TEXT datatype is not flexible to query, isslow and wastes a lot of space if used incorrectly. Sometimes a VARCHAR willhandle your data better.

DON'T use temporary tables

Unless strictlynecessary. Often a subquery can substitute a temporary table. They induceoverhead and will give you a big headache when programming under COM+ becauseit uses a database connection pool and temporary tables will last forever. InSQL Server 2000, there are alternatives like the TABLE data type which canprovide in-memory solutions for small tables inside stored procedures too.

DO learn how to read a queryexecution plan

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

DO use referential integrity

This can be agreat 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 saidbefore, this is by no means a complete SQL Server performance and bestpractices guide. This would take a complete book to cover. But I really believethat this is a good start, and if you follow these practices, surely you willhave much less trouble in the future.

分享到:
评论

相关推荐

    SQL Server 2008高级程序设计 2/6

    《SQL Server 2008高级程序设计》由世界顶尖SQL Server权威专家Robert Vieira编写,旨在指导您熟练运用一系列日趋复杂的功能,助您更高效地管理数据。  本书首先介绍SQL Server 2008的新功能,然后在更详实的示例...

    Microsoft SQL Server 2005技术内幕:存储引擎(中文).pdf

    SQL Server 2005微软官方权威参考书.  公球公认SQL Server 2005 经典著作..  数据库“铁人”、微软MVP胡百敬先生鼎力推荐  微软SQL Server 总部Principal Group 项目经理朱凌志鼎力推荐  本书详细介绍了数据...

    SQL Server 设计、命名、编码规范

    数据库设计和开发标准是使数据库系统的设计和开发正式化的标准。通过此标准,来规范数据库设计。

    SQL Server 2008高级程序设计 4/6

    SQL Server 2008高级程序设计 4/6 SQL Server 2008 2010 高级程序设计 作者:(美)维埃拉 著,杨华,腾灵灵 译 出版社:清华大学 出版日期:2010-4-1 ISBN:9787302222729 字数:1250000 页码:730 ----------------...

    SQL_Server2000案例教程

    第7章 Transact-SQL程序设计 第8章 使用默认值、规则和触发器 第9章 SQL Server安全管理 第10章 使用Access开发SQL Server应用程序 第11章 使用VB开发SQL Server应用程序 第12章 使用ASP开发SQL Server应用程序

    Inside SQL SERVER 2005 T-SQL Programming

    SQl SERVER 进阶 学习课件 学习E-R图的绘制,理解数据库范式,掌握如何规范地设计数据库。 数据的高级查询、子查询。 创建和使用索引、视图,实现高效的数据管理。 学习使用T-SQL进行数据库编程,实现多功能数据管理...

    SQLServer安全及性能优化

    SQLServer安全及性能优化 修补漏洞 安装程序补丁修补漏洞 随时关注微软官方网站补丁升级 关闭不必要的端口 关闭联必要的服务 数据库引擎 SQL Server Analysis Services SQL Server Reporting Services SQL ...

    SQL Server 2008编程入门经典(第3版)

    第1章 RDBMS基础:SQLServer数据库的构成 1.1 数据库对象概述 1.1.1 数据库对象 1.1.2 事务日志 1.1.3 最基本的数据库对象:表 1.1.4 文件组 1.1.5 数据库关系图 1.1.6 视图 1.1.7 存储过程 1.1.8 用户自定义函数 ...

    数据库编程期末答疑,卷子讲解,SQL server相关操作讲解,如有侵权请联系删除

    乐山师范学院数据库编程期末答疑,卷子讲解,SQL server相关 如下是一个简化的员工考勤应用E-R图,请在SQL Server中创建名为YQKG的数据库,包括两个数据文件,一个日志文件,文件名按SQL Server对象命名规范定义,...

    大学生毕业设计项目实训:学生信息管理系统(源代码,SQL Server数据库)

    大学生毕业设计项目实训:学生信息管理系统(源代码,SQL Server数据库) 工程源代码组成: Data DLL 事务规则服务器 学生信息管理系统 说明书

    SQL Server 2008 商业智能完美解决方案(3)

    利用Microsoft SQL Server 2008实现灵活的商业智能解决方案使用Microsoft 完善的BI工具构建B0解决方案的必备指南,使用SQLServer 2008设计、开发和部署更有效的数据集成、报表、分析解决方案所需的权威操作指南。...

    SQLServer数据库设计规范.txt

    SQLServer数据库设计规范 作者:佚名 厚朴教育来源:网络 点击数:1446 更新时间:2008-12-2 1 相关的设计规范: 1.1 采用有意义的字段名 尽可能地把字段描述的清楚些。当然,也别做过头了,比如...

    SQL Server 2008数据库设计与实现

    通过将理论融入数据库实践,清晰地讲解了关系型数据库的设计原则,完整地展示了如何进行良好的关系型数据库设计,深入揭示了SQL Server 2008的技术细节。  本书浓缩了作者作为SQL Server数据库架构师多年来丰富的...

    供求信息网(asp.net2.0 + sql Server 2005)

    SQL Server 2005实现) 教学视频:1小时38分 1.1 开发背景 1.2 系统分析 1.2.1 需求分析 1.2.2 可行性分析 1.2.3 编写项目计划书 1.3 系统设计 1.3.1 系统目标 1.3.2 业务流程图 1.3.3 网站功能结构 1.3.4...

    SQL Server 2000数据库开发从零开始

    内容涉及应用程序需求分析和模型设计,数据库数据处理、函数、存储过程、默认、规则等多种数据库对象操作,SQL Server 2000多种管理工具的使用方法,数据库完整性、一致性、可用性和安全性方面的知识,前端应用程序...

    SQL.Server.2008编程入门经典(第3版).part2.rar

    第1章 RDBMS基础:SQLServer数据库的构成 1.1 数据库对象概述 1.1.1 数据库对象 1.1.2 事务日志 1.1.3 最基本的数据库对象:表 1.1.4 文件组 1.1.5 数据库关系图 1.1.6 视图 1.1.7 存储过程 1.1.8 用户自定义函数 ...

    SQLServer大数据库教案设计.doc

    考核要求……(5分钟) 2、SQL Server的定义:是一个高性能的C/S结构的RDBMS,是为支持高容量的事务处理(如在线 订购录入、存货目录、记帐或支付)以及数据仓库和决策支持系统而设计的。 3、SQL Server 的发展、...

    SQL Server 2008高级程序设计 5/6

    SQL Server 2008高级程序设计 5/6 SQL Server 2008 2010 高级程序设计 作者:(美)维埃拉 著,杨华,腾灵灵 译 出版社:清华大学 出版日期:2010-4-1 ISBN:9787302222729 字数:1250000 页码:730 ----------------...

    SQL Server 2008高级程序设计 6/6

    SQL Server 2008高级程序设计 6/6 SQL Server 2008 2010 高级程序设计 作者:(美)维埃拉 著,杨华,腾灵灵 译 出版社:清华大学 出版日期:2010-4-1 ISBN:9787302222729 字数:1250000 页码:730 ----------------...

    SQL Server 2008高级程序设计 3/6

    《SQL Server 2008高级程序设计》由世界顶尖SQL Server权威专家Robert Vieira编写,旨在指导您熟练运用一系列日趋复杂的功能,助您更高效地管理数据。  本书首先介绍SQL Server 2008的新功能,然后在更详实的示例...

Global site tag (gtag.js) - Google Analytics