Home Database Mysql Tutorial SQLServer 数据库开发顶级技巧

SQLServer 数据库开发顶级技巧

Jun 07, 2016 pm 05:58 PM
sqlserver Database development

无论你的专业水平如何,从其他IT专家那里学习新的技巧与最佳实践常常都是有益的。本文包含了我遇到过的SQL Server开发的高级技巧。希望其中的一些技巧能够对您的数据库开发及管理工作有所帮助。

确保代码中的数据类型与数据库中的列类型保持一致
  确保您的应用程序各层数据类型保持一致是非常重要的。例如,如果一列的数据类型为NVARCHAR(50),那么,您应该在代码查询与存储过程中使用相同类型的局部变量。
  同样,数据层中的ADO.NET代码也应该指定相同的数据类型与长度。为什么这很重要呢?因为如果数据类型与查询匹配,SQL Server需要先进行数据类型的隐式转换,以使它们能够匹配。
  也有一些情况,即使为参照列设置了索引,SQL Server却不能使用此索引。因此,变量与列类型一致的情况下,您的查询可能会使用Index Scan而不是Index Seeking,这样需要执行的时间就更长了。
在中进行大规模更新
  开发人员有时需要对一张表中的一列或多列中的全部或大部分列进行数据修改。通常,对小表而言这并不是一个什么问题。
  然而,如果表很大的话,您的更新语句将锁定整张表,使它无法使用,甚至都不能读取。更有甚者,对一张频繁变化的表进行更新可能使整个应用程序或网站瘫痪。有时,在极端情况下,一个大的、单个事务将导致事务日志急剧增长,并最终耗尽数据库服务器磁盘空间。
  因此,好的策略是进行分批大规模更新,并结合频繁的事务日志备份。以我的经验看,最好一批10,000至50,000工作量。当您开始考虑应用批量处理时,确定阈值很困难,因为这取决于诸多因素比方说如何使I/O更快,如何使表高效利用等等。
  您可以考虑一个准则。在ADO.NET中,典型的命令超时时间是30秒左右。当开始更新时,其他进程一直处于等待状态直到更新结束。因此如果期望更新时间超过20-25秒,您最好进行一个更新。否则,将以应用程序超时而结束。
  下面这段简单的代码展示了如何更新表中的一列,应用的批量大小为10,000:
  WHILE ( 0 = 0 )
  BEGIN
  UPDATE TOP ( 10000 )
  Person
  SET Status = 2
  WHERE Status = 1
  IF @@ROWCOUNT = 0
  BREAK
  END
  应用FOR-EACH存储过程
  有些时候您可能需要对某一特定类型的所有对象执行相同的操作。例如,您可能需要对数据库中的所有表分配特定的权限。开发人员经常通过指针设置这样的任务,但是SQL Server中两个简单的存储过程可以更容易实现:sp_msForEachTable 与 sp_msForEachDB。
  每个存储过程作为一个参数执行命令。在命令中,您把表名或数据库名作为一个问号标志占位符嵌入到参数中。命令运行时,SQL Server把问号标志替换为表名或数据库名,并执行。
  例如,下面的代码在Server上除TempDB外,对每个数据库进行全备份:
  EXEC sp_msforeachdb 'IF ''?'' ''tempdb'' BACKUP DATABASE ?
  TO DISK=''c:\backups\?.bak'' WITH INIT'
  这是另外一个如何应用这些存储过程的例子。下面的代码在禁用外键后,删除数据库所有表中的数据。当然了,当使用这些代码时,您需要谨慎的练习。
  EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
  EXEC sp_MSForEachTable '
  IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
  DELETE FROM ?
  else
  TRUNCATE TABLE ?
  '
  EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
  建立数据库版本
  对开发人员而言,如同对您的应用程序版本化一样,对数据库执行数字版本化是一个很好的方法。
  执行版本化并不需要很大的工作量,您只需创建一个包含版本号列及时间戳列的版本表即可。当部署那些脚本时,您将更好的分配每个脚本集合的版本号,并对版本表进行更新,检查错误与数据库对比将变得更加容易。您甚至可以对脚本进行编号,这样一来如果数据库中建立的编号不比脚本中建立的编号高的话,脚本就不执行。样例数据库AdventureWorks中的AWBuildVersion就是一个很好的例子,可以看看。
  尽量减少网络会话
  这个技巧主要针对从数据库取数据的网络应用程序。缺乏经验的开发人员常常意识不到数据库调用是代价很高的操作。对于小应用程序而言,这不是什么大问题。但是,由于很多网站变得非常火爆导致数以千计的用户同时在线,那么您就有必要提前考虑它的可扩展性与网页加载时间的优化问题了。
  我曾经看到过的网页有多达15个数据库调用,而大多数正在执行的存储过程就是为了返回单独的一行或一个值。需要牢记的是在SQL Server中一个单独的存储过程能够返回多个结果集。在一个存储过程中,您可以使用ADO.NET中的DataSet对象以及把DataTable对象组成一个集合。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

How to solve Java connection SqlServer error How to solve Java connection SqlServer error May 01, 2023 am 09:22 AM

The problem was found that this time I was using the SqlServer database, which I had not used before, but the problem was not serious. After I connected the SqlServer according to the steps in the requirements document, I started the SpringBoot project and found an error, as follows: At first I thought it was a SqlServer connection. There was a problem, so I went to check the database and found that everything was normal. I first asked my colleagues if they had such a problem, and found that they did not, so I started my best part, facing Baidu. programming. The specific error message I started to solve was this, so I started Baidu error reporting: ERRORc.a.d.p.DruidDataSource$CreateCo

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

See all articles