介绍 (Introduction)

tempdb is one of the 4 system databases that exists in all SQL Server instances. The other databases are master, model and msdb. In case of using Replication, a fifth system database named distribution will also exist. You can find all existing system databases in SQL Server Management Studio (SSMS) under the Databases / System Databases folder:

tempdb是所有SQL Server实例中存在的4个系统数据库之一。 其他数据库是mastermodelmsdb 。 如果使用复制 ,则还将存在第五个名为distribution的系统数据库。 您可以在SQL Server Management Studio(SSMS)中的“数据库/系统数据库”文件夹下找到所有现有系统数据库:

tempdb的用法 (tempdb usage)

SQL Server uses tempdb database to store user objects, internal objects and version stores.

SQL Server使用tempdb数据库存储用户对象,内部对象和版本存储。

版本商店 (Version stores)

Version stores are used to store row versions generated from operations as online reindex, triggers or snapshot isolation.

版本存储用于将操作生成的行版本存储为在线重新索引,触发器或快照隔离。

内部物件 (Internal objects)

As the name indicates, internal objects are created internally by the SQL Server engine and are stored in tempdb database. These internal objects are:

顾名思义,内部对象由SQL Server引擎在内部创建,并存储在tempdb数据库中。 这些内部对象是:

  • Intermediate results for sorting process; 中间结果用于排序过程;
  • Intermediate results for hash joins and aggregates; 哈希联接和聚合的中间结果;
  • Intermediate results from queries that need to spool; 需要假脱机的查询的中间结果;
  • XML variables and large object (LOB) variables; XML变量和大对象(LOB)变量;
  • Keys from keyset cursors; 来自键集游标的键;
  • Query results from static cursors; 从静态游标查询结果;
  • Messages in transit from Service Brokers; 服务中介发送的邮件;
  • Data for internal processing from INSTEAD OF triggers; 来自INSTEAD OF触发器的内部处理数据;
  • Internal use from DBCC CHECK command; 从DBCC CHECK命令内部使用;
  • Database mail. 数据库邮件。

用户对象 (User objects)

User temporary objects can be tables, stored procedures, table variables, the value returned from a table valued function or the mapping index for online clustered index build with the SORT_IN_TEMPDB option.

用户临时对象可以是表,存储过程,表变量,从表值函数返回的值或使用SORT_IN_TEMPDB选项构建的在线聚集索引的映射索引。

User objects can be defined as local temporary object by using a ‘#’ as prefix in the object name (e.g. #MyTempTable) or can be defined as global temporary objects by using the prefix ‘##’ before the object name (e.g. ##MyTempTable).

可以通过在对象名称中使用前缀“#”将用户对象定义为本地临时对象(例如,#MyTempTable),也可以通过在对象名称之前使用前缀“ ##”将用户对象定义为全局临时对象(例如,## MyTempTable)。

A local temporary object is destroyed when the scope where it was created, expires or terminates. The scope can be a stored procedure or a session.

当本地临时对象的作用域在创建,过期或终止时会被销毁。 范围可以是存储过程或会话。

A global temporary object is only destroyed when all sessions that are using it expires or terminates.

全局临时对象仅在使用它的所有会话都到期或终止时才被销毁。

组态 (Configuration)

A tempdb database was deemed to be so important that in SQL Server 2016 it has his own configuration setup screen during the SQL Server engine installation:

tempdb数据库被认为非常重要,以至于在SQL Server 2016中,在SQL Server引擎安装期间,它具有自己的配置设置屏幕:

As can be seen, the number of files is filled automatically with the number of logical processors and it is requiring own location for the data and log files. I will next describe these configuration settings.

可以看出,文件的数量自动用逻辑处理器的数量填充,并且它需要数据和日志文件自己的位置。 接下来,我将描述这些配置设置。

资料档案 (Data files)

It is recommended to create a data file per logical processor for the tempdb database, until a maximum of 8 data files, meaning that if your server has more than 8 processors, you should not create more than 8 data files. This is only a best practice recommendation, though, and can be tweaked if necessary. Keep in mind that too many files may increase the cost of file switching, meaning increase the overhead and that is why recommendation is not to start with more than 8 data files.

建议为tempdb数据库的每个逻辑处理器创建一个数据文件,直到最多8个数据文件为止;这意味着,如果您的服务器具有8个以上的处理器,则不应创建8个以上的数据文件。 但是,这只是最佳实践建议,如有必要,可以进行调整。 请记住,太多文件可能会增加文件切换的成本,这意味着增加了开销,这就是为什么建议不要从8个以上的数据文件开始的原因。

Each of the multiple data files should have the same size so the SQL Server engine can apply the proportional fill optimization procedure to reduce UP latch contention. This procedure is a mechanism that will guarantee that each data file is filled in proportion to the free space that is available in the file so that all of the files fill up at about the same time.

多个数据文件中的每个文件都应具有相同的大小,以便SQL Server引擎可以应用比例填充优化过程来减少UP闩锁争用。 此过程是一种机制,可以确保每个数据文件都按文件中可用空间的比例进行填充,以使所有文件几乎同时填充。

Microsoft recommends to set an auto grow value for tempdb, but based on my own experience I will not recommend it, myself. My personal recommendation is to initially set the maximum size possible for the tempdb data files (divide the disk size by the number of data files) and immediately fill the disk so you can disable the auto grow to avoid any negative impact during the file grow process.

Microsoft建议为tempdb设置一个自动增长的值,但是根据我自己的经验,我本人不推荐这样做。 我个人的建议是首先为tempdb数据文件设置最大可能的大小(将磁盘大小除以数据文件的数量),然后立即填充磁盘,以便可以禁用自动增长,以避免在文件增长过程中产生任何负面影响。

Shrinking files is also not a recommended operation for a tempdb database since it might change database files sizes and impact negatively on the proportional fill optimization procedure because this mechanism needs to have the same size for all data files.

对于tempdb数据库,也不建议缩小文件,因为它可能会更改数据库文件的大小,并对比例填充优化过程产生负面影响,因为此机制需要所有数据文件的大小相同。

磁盘空间 (Disk space)

tempdb is one particular database that is more difficult to estimate the needed disk space. As showed in the tempdb usage section, it stores many kinds of objects and they are not so easy to estimate. Unfortunately, during all these years working with SQL Server I couldn’t yet find the magic formula but I would recommend to provide as much disk space you can with the minimum disk size being the same size as the largest table in the SQL Server instance. It is also wise to add some safety factor to prevent the database growth.

tempdb是一个特定的数据库,很难估计所需的磁盘空间。 如tempdb用法部分中所示,它存储了许多类型的对象,而且估计起来并不容易。 不幸的是,在使用SQL Server的所有这些年来,我一直找不到神奇的公式,但我建议您提供尽可能多的磁盘空间,最小磁盘大小应与SQL Server实例中最大表的大小相同。 添加一些安全因素以防止数据库增长也是明智的。

Fortunately, for the tempdb transaction log file, it is easier to estimate the necessary size. The tempdb, being a database with the recovery model set to Simple, it will be minimally logged. You can use a rule of thumb to create the transaction log file based on 20%-30% of the data size and tweak it later if needed.

幸运的是,对于tempdb事务日志文件,可以更容易地估计所需的大小。 tempdb是恢复模型设置为Simple的数据库,它将被最小化记录。 您可以使用经验法则基于20%-30%的数据大小创建事务日志文件,并在需要时对其进行调整。

The space in tempdb is vital for the health of the SQL Server instance. When running out of space the SQL Server instance may become unresponsive so it is always better to reserve more space than give less for the tempdb data and log files.

tempdb中的空间对于SQL Server实例的运行状况至关重要。 当空间不足时,SQL Server实例可能会变得无响应,因此,与为tempdb数据和日志文件提供较少的空间相比,保留更多的空间总是更好。

It is also good to have dedicated disks for a tempdb database to avoid a situation where other database files to consume space that might be needed later for tempdb. SSD is supported since SQL Server 2012 and can be used to store database files and because of the particularities of tempdb, as such, it is, in my opinion, the best database candidate to be stored in a SSD drive to achieve better performance results.

tempdb数据库提供专用磁盘也很好,以避免其他数据库文件占用tempdb以后可能需要的空间的情况。 从SQL Server 2012开始就支持SSD,并且由于tempdb的特殊性, SSD可以用于存储数据库文件,因此,在我看来,它是存储在SSD驱动器中以获得最佳性能结果的最佳数据库候选者。

运作方式 (Operations)

数据库创建 (Database creation)

tempdb is created every time the SQL Server instance starts, meaning that any existing object will be lost after the database recreation.

每次SQL Server实例启动时都会创建tempdb ,这意味着在重新创建数据库后,任何现有对象都将丢失。

权限 (Permissions)

By default, all users that have access to the SQL Server instance can create objects and perform queries in tempdb database, although the connect permission can be revoked from an user as it is for a regular database.

默认情况下,尽管可以像常规数据库一样撤消用户的连接权限,但是所有有权访问SQL Server实例的用户都可以在tempdb数据库中创建对象并执行查询。

移动文件位置 (Move file locations)

As in any regular database, tempdb data and log files can be moved to another location if and when needed to. The following is an example for moving 2 data files and 1 transaction log file to a new location in disk T:

与在任何常规数据库中一样,如果需要,可以将tempdb数据和日志文件移动到另一个位置。 以下是将2个数据文件和1个事务日志文件移动到磁盘T中的新位置的示例:


ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'T:\TempDB\Data\tempdb.mdf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = temp2, FILENAME = 'T:\TempDB\Data\tempdb_mssql_2.ndf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'T:\TempDB\Log\templog.ldf');

Since tempdb is recreated every time the SQL Server instance restarts, there is no need to copy the current files to the new location. The only thing that it needs to be done to have these changes implemented is to perform a SQL Server service restart and then the new files will be created in the new location (folder structure should exist). After that, old tempdb data and log files can be deleted from old location.

由于tempdb每次重新启动SQL Server实例时都会重新创建,因此无需将当前文件复制到新位置。 要实现这些更改,唯一需要做的就是重启SQL Server服务,然后将在新位置创建新文件(文件夹结构应该存在)。 之后,可以从旧位置删除旧的tempdb数据和日志文件。

限制条件 (Restrictions)

tempdb has many restrictions and some are explained below.

tempdb有很多限制,下面将对一些限制进行说明。

后备 (Backup)

tempdb is the only database that cannot be backed up, meaning of course, that it cannot be restored as well.

tempdb是唯一无法备份的数据库,这当然意味着它也无法还原。

In SSMS you will not find the context menu for Backup and Restore on tempdb and if you try to execute the backup command you will receive the error ‘Backup and restore operations are not allowed on database tempdb’:

在SSMS中,您将无法在tempdb上找到“备份和还原”的上下文菜单,如果尝试执行backup命令,则会收到错误消息“ 数据库tempdb上不允许进行备份和还原操作 ”:

恢复模式 (Recovery model)

tempdb recovery model is set to Simple and cannot be changed. If you try to change it, you will receive the error ‘Option ‘RECOVERY’ cannot be set in database ‘tempdb’’:

tempdb恢复模型设置为“简单”,无法更改。 如果尝试更改它,将收到错误“ 无法在数据库'tempdb'中设置选项'RECOVERY' ':

删除数据库 (Drop database)

tempdb cannot be deleted. If you try to delete it, you will receive the error ‘Cannot drop the database ‘tempdb’ because it is a system database’:

tempdb无法删除。 如果尝试删除它,将会收到错误“由于系统数据库数据库'tempdb'而无法删除 ':

设为离线 (Set offline)

tempdb cannot be set to offline. If you try to do it you will receive the error ‘Option ‘OFFLINE’ cannot be set in database ‘tempdb’’:

tempdb无法设置为脱机。 如果尝试这样做,将会收到错误“ 无法在数据库'tempdb'中设置选项'OFFLINE' ':

DBCC CHECKCATALOG和CHECKALLOC (DBCC CHECKCATALOG and CHECKALLOC)

A DBCC CHECKALLOC or DBCC CHECKCATALOG cannot be run on a tempdb database:

不能在tempdb数据库上运行DBCC CHECKALLOC或DBCC CHECKCATALOG:

其他限制 (Other restrictions)

There are some more restrictions that are good to be known:

还有其他一些众所周知的限制:

  • The tempdb database cannot be renamed; tempdb数据库无法重命名;
  • The tempdb database owner is dbo and cannot be changed; tempdb数据库所有者为dbo,无法更改;
  • The tempdb database and its primary filegroup cannot be set to READ_ONLY status; 不能将tempdb数据库及其主文件组设置为READ_ONLY状态。
  • The tempdb does not allow for adding more filegroups for the database nor rename the primary filegroup; tempdb不允许为数据库添加更多文件组,也不能重命名主文件组。
  • The primary filegroup, primary data file, primary log file and the guest user of tempdb cannot be deleted; 不能删除tempdb的主文件组,主数据文件,主日志文件和guest用户。
  • The default collation for the tempdb is the SQL Server instance collation and cannot be changed; tempdb的默认排序规则是SQL Server实例排序规则,无法更改;
  • Change Data Capture (CDC) cannot be enabled for tempdb; 无法为tempdb启用更改数据捕获(CDC);
  • The tempdb database cannot be part of a database mirroring solution. tempdb数据库不能成为数据库镜像解决方案的一部分。

Next articles in this series:

本系列的下一篇文章:

  • SQL Server system databases – the master databaseSQL Server系统数据库–主数据库
  • SQL Server system databases – the msdb databaseSQL Server系统数据库– msdb数据库
  • SQL Server system databases – the model databaseSQL Server系统数据库–模型数据库

参考资料 (References)

  • tempdb database tempdb数据库
  • Working with tempdb in SQL Server 2005 在SQL Server 2005中使用tempdb
  • Capacity Planning for tempdb tempdb的容量规划
  • Recommendations to reduce allocation contention in SQL Server tempdb database 减少SQL Server tempdb数据库中分配争用的建议

翻译自: https://www.sqlshack.com/configuration-operations-restrictions-tempdb-sql-server-system-database/

tempdb SQL Server系统数据库的配置,操作和限制相关推荐

  1. SQL Server系统数据库– msdb数据库

    介绍 (Introduction) This article is the third I am writing about Microsoft SQL system databases. 本文是我正 ...

  2. SQL Server系统数据库–模型数据库

    介绍 (Introduction) This is my fourth article about SQL Server system databases. In previous articles ...

  3. SQL Server系统数据库–主数据库

    介绍 (Introduction) There are at least 4 system databases in any SQL Server instance as shown by the f ...

  4. SQL Server 2012 数据库镜像配置完整篇

    "数据库镜像"是一种提高 SQL Server 数据库的可用性的解决方案. 镜像基于每个数据库实现,并且只适用于使用完整恢复模式的数据库.数据库镜像维护一个数据库的两个副本,这两个 ...

  5. Sql Server系统数据库的作用

    Sql Server系统数据库的作用 一.  系统数据库 Sql Server的系统数据库分为:master.model.msdb和tempdb,这四个数据库在SQL Server中各司其职,作为研发 ...

  6. SQL Server系统数据库介绍

    文章目录 一.基本介绍 1.1 数据库组成 1.2 数据文件 1.数据文件 2.日志文件 1.3 五大系统数据库 二.master数据库 2.1 基本信息 2.2 限制 2.3 使用建议 三.msdb ...

  7. SQL Server 2008数据库的配置及连接

    在连接数据库之前必须保证SQL Server 2008是采用SQL Server身份验证方式而不是windows身份验证方式.如果在安装时选用了后者,则重新设置如下: http://blog.163. ...

  8. DBA基础系列SQL Server 2014:2. SQL Server用户数据库初始化配置

    前言 开始前先黑微软一把:Microsoft秉承一贯的简单易用作风(Next.Next.Next- )这点是它吸引用户的地方,但是这个优点如果用在数据库上将是一场灾难,如我们上一章讲到的SQL Ser ...

  9. SQL Server系统数据库损坏时的快速恢复使用

    1.假设data文件夹中master,masterlog,model,modellog,msdbdata,msdblog的系统数据库文件已损坏,复制Binn/template中的同名文件替换. 2.管 ...

最新文章

  1. boost::hana::append用法的测试程序
  2. 马尔科夫链蒙特卡洛_蒙特卡洛·马可夫链
  3. python list tuple 消耗_Python的List与Tuple
  4. 2022年上半年系统分析师上午真题及答案解析
  5. 从 C10K 到 DPDK
  6. 紧急 抢救mysql 数据库 恢复到指定时间点
  7. 如何在互联网上寻找免费电子书?(其二)
  8. linux bash 自动补全,bash中的自动补全机制
  9. DNS主域名服务器、从域名服务器和缓存域名服务器的构建和原理
  10. 《西部世界》与《头号玩家》:哪个才是人类与人工智能相处的正确方式?
  11. java的StadIn函数_Java經典算法案例---Java深入
  12. 微信开放平台开发——网页微信扫码登录(OAuth2.0)
  13. Nodelist转化问题。
  14. PHP孟加拉钢厂_昆钢推进孟加拉国、柬埔寨、缅甸钢铁国际产能合作示范园区建设...
  15. HTML中字体属性、文本属性使用说明
  16. 伊利诺理工大学计算机专业,美国伊利诺伊理工大学世界排名_专业_申请条件-PSONE品思...
  17. 夯实算力底座,“中原计算”开创河南AI产业新局面
  18. python卸载没卸载干净导致再次安装失败
  19. CS:APP archlab 记录
  20. 基于低代码开发平台实现政务OA系统

热门文章

  1. 【C#】使用DWM实现无边框窗体阴影或全透窗体
  2. React Native按钮详解|Touchable系列组件使用详解
  3. 从头认识Spring-3.4 简单的AOP日志实现-扩展添加检查订单功能,以便记录并检測输入的參数...
  4. CSS定位 position
  5. Eclipse:An internal error occurred during: Building workspace. GC overhead limit exceeded
  6. 使用nodejs爬前程无忧前端技能排行(半半成品)
  7. 动态生成的DOM不会触发onclick事件的原因及解决方法
  8. android:ellipsize省略文字用法(转载)
  9. iOS开发之oc(二十)--Foundation(5)NSDictionary
  10. 再谈JavaScript时钟中的16ms精度问题.