临时数据库如何工作?

发布于 2024-08-08 19:32:59 字数 483 浏览 10 评论 0原文

我试图理解 tempDB,以下是我脑海中出现的疑问。

  1. tempDB 中数据的生命周期是多长?假设一个查询正在执行一些 Order By 操作,并使用 tempDB 来执行该操作。此查询完成后,其他人也会执行使用 tempDB 的查询。第二个查询会在 tempDB 中找到第一个查询写入的记录还是将其删除?
  2. Sql 引擎在 tempDB 中是否创建了任何可见表?我如何知道由于该查询而创建了哪个临时表? Sql引擎是否遵循任何命名约定来命名这些临时表?

我是 tempDB 的新手,所以请原谅我问这样愚蠢的(如果有的话)问题:-)

如果有人能给我指出一个可以帮助我的好资源,那就太好了了解 tempDB。

I am trying to understand the tempDB and following are the doubts popping in my mind.

  1. What is the lifetime of data in tempDB? Say a query is doing some Order By and uses tempDB for performing that. After this query finishes, someone else also executes a query which utilizes the tempDB. Will the second query find records written by first query in the tempDB or will they be deleted?
  2. Are there any visible tables created inside the tempDB by the Sql Engine? How can I know which temporary table is created because of this query? Is there any naming convention followed by the Sql engine for naming these temporary tables?

I am new to tempDB so please pardon me for asking such silly (if at all) questions :-)

It will be very nice if someone can point me to a good resource which can help me learn about tempDB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

智商已欠费 2024-08-15 19:32:59

临时表存储在 tempdb 中,直到连接被删除(或者对于全局临时表,当最后一个使用它的连接被删除时)。当您使用完表后,您还可以(这是一个很好的做法)通过 drop table 语句手动删除该表。

不,如果临时表是本地临时表,其他人无法看到您的临时表(他们可以查看和使用全局临时表)多个人可以运行使用相同临时表名称的命令,但它们不会在本地临时表中重叠,因此您可以有一个名为 #test 的表,其他 10,000 个用户也可以,但每个用户都有自己的结构和数据。

您通常不想在 tempdb 中查找临时表。可以检查是否存在,但这是我唯一一次直接引用 tempdb。只需使用您的临时表名称即可。下面检查是否存在的示例

  IF OBJECT_ID('TempDB.dbo.#DuplicateAssignments') IS NOT NULL 
  BEGIN 
  DROP TABLE #DuplicateAssignments 
  END  

您可以通过在名称前面加上 #(对于本地表,您 999.9% 的时间都会使用的表)和 ##(对于全局临时表)来命名临时表,然后是您想要的名称的其余部分。

Temp table is stored in tempdb until the connection is dropped (or in the case of a global temp tables when the last connection using it is dropped). You can also (and it is a good practice to do so) manually drop the table when you are finished using it with a drop table statement.

No, others cannot see your temp tables if they are local temp tables (They can see and use global temp tables) Multiple people can run commands which use the same temp table name but they will not be overlapping in a local temp table and so you can have a table named #test and so can 10,000 other users, but each one has its own structure and data.

You don't want to generally look up temp tables in tempdb. It is possible to check for existence, but that is the only time I have ever referenced tempdb directly. Simply use your temp table name. Example below of checking for existence

  IF OBJECT_ID('TempDB.dbo.#DuplicateAssignments') IS NOT NULL 
  BEGIN 
  DROP TABLE #DuplicateAssignments 
  END  

You name temp tables by prefacing the name with # (for local tables the ones you would use 999.9% of the time) and ## for global temp tables, then the rest of the name you want.

喜爱纠缠 2024-08-15 19:32:59

有几篇 MSDN 文章可能是有关 SQL Server 中 tempDB 数据库的最佳信息来源。

tempdb 数据库

tempdb系统数据库是一个全局的
所有人都可以使用的资源
连接到 SQL 实例的用户
服务器并用于保存
以下:

  • 显式创建的临时用户对象,例如:全局或
    本地临时表,临时表
    存储过程、表变量或
    光标。
  • 由 SQL Server 数据库引擎创建的内部对象,用于
    例如,要存储的工作表
    线轴的中间结果或
    排序。
  • 由数据修改事务生成的行版本
    使用已提交读的数据库
    使用行版本控制隔离或
    快照隔离事务。
  • 由数据修改事务生成的行版本
    功能,例如:在线索引
    操作,多个活动结果
    设置 (MARS) 和 AFTER 触发器。

tempdb 内的操作最少
已记录。这使得交易能够
被回滚。 tempdb 被重新创建
每次 SQL Server 启动时
系统总是以
数据库的干净副本。暂时的
表和存储过程是
断开连接时自动丢弃,
并且没有连接处于活动状态时
系统已关闭。因此,有
tempdb 中从来没有任何内容
从 SQL Server 的一个会话中保存
到另一个。备份和恢复
不允许对 tempdb 进行操作。

还有tempdb 和索引创建此博文 以及在 SQL Server 2005 中使用 tempdb 其中状态:

SQL Server系统数据库tempdb在SQL Server 2005中发生了一些变化。SQL Server 2005中有新的tempdb用法和内部优化;自 SQL Server 2000 以来,tempdb 架构基本没有变化。

tempdb 系统数据库与用户数据库非常相似。主要区别在于 tempdb 中的数据在 SQL Server 关闭后不会保留。

There's a few MSDN articles that are probably the best source of information on the tempDB database in SQL Server.

tempdb Database

The tempdb system database is a global
resource that is available to all
users connected to the instance of SQL
Server and is used to hold the
following:

  • Temporary user objects that are explicitly created, such as: global or
    local temporary tables, temporary
    stored procedures, table variables, or
    cursors.
  • Internal objects that are created by the SQL Server Database Engine, for
    example, work tables to store
    intermediate results for spools or
    sorting.
  • Row versions that are generated by data modification transactions in a
    database that uses read-committed
    using row versioning isolation or
    snapshot isolation transactions.
  • Row versions that are generated by data modification transactions for
    features, such as: online index
    operations, Multiple Active Result
    Sets (MARS), and AFTER triggers.

Operations within tempdb are minimally
logged. This enables transactions to
be rolled back. tempdb is re-created
every time SQL Server is started so
that the system always starts with a
clean copy of the database. Temporary
tables and stored procedures are
dropped automatically on disconnect,
and no connections are active when the
system is shut down. Therefore, there
is never anything in tempdb to be
saved from one session of SQL Server
to another. Backup and restore
operations are not allowed on tempdb.

There's also tempdb and Index Creation, this blog post along with Working with tempdb in SQL Server 2005 which states:

The SQL Server system database, tempdb, has undergone a number of changes in SQL Server 2005. There are new tempdb usages and internal optimizations in SQL Server 2005; tempdb architecture is mostly unchanged since SQL Server 2000.

The tempdb system database is very similar to a user database. The main difference is that data in tempdb does not persist after SQL Server shuts down.

卷耳 2024-08-15 19:32:59
  1. 查询完成后,TempDB 中创建的临时表将被删除。

  2. 我对此不确定(我必须尝试一下),但我认为理论上在 TempDB 中创建的所有表都是可见的,尽管只有创建该表的用户有权访问它。

  1. The temporary tables created in TempDB are dropped when the query is completed.

  2. I'm not sure on this (I would have to try it), but I think theoretically ALL tables created in TempDB are visible, although only the user that created the table has permission to access it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文