Oracle 中临时表与全局临时表有什么区别?

发布于 2024-07-11 12:01:42 字数 61 浏览 11 评论 0原文

我听说过“临时表”和“全局临时表”这两个术语几乎在类似的上下文中使用。

两者有什么区别?

I have heard these two terms "temporary table" and "global temporary table" used pretty much in similar context.

What is the difference between the two?

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

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

发布评论

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

评论(6

装纯掩盖桑 2024-07-18 12:01:42

在 Oracle 中没有任何区别。 当您在Oracle数据库中创建临时表时,它自动是全局的,并且您需要包含“Global”关键字。

SQL 标准定义了术语“GLOBAL TEMPORARY TABLE”的解释方式,允许 LOCAL 或 GLOBAL 范围。 这将允许特定于用户的表(本地)或每个人(全局)。 Oracle仅实现GLOBAL版本。

您放入 Oracle 临时表的数据特定于您的会话。 也就是说,即使有 100 个用户都使用同一个表,也只有您可以看到您的数据,并且根据表设置,当您断开连接(或提交当前事务时)时,您的数据将从表中删除。

将此与 MS SQL-Server 进行对比,其中临时表位于本地。 如果您创建一个临时表,除了您之外没有人知道您的临时表的存在。 在 Oracle 中,创建临时表允许每个人(以及有权访问您的模式的每个人)查看该表。 当您注销会话时,SQL-Server 表将被删除,并且需要为下一个会话重新创建。 在 Oracle 中,临时表现在是架构的永久部分,即使数据不是。

In Oracle there isn't any difference. When you create a temporary table in an Oracle database, it is automatically global, and you are required to include the "Global" key word.

The SQL standard, which defines how the term "GLOBAL TEMPORARY TABLE" is interpreted, allows for either a LOCAL or GLOBAL scope. This would allow for either a user specific table (LOCAL) or everyone (GLOBAL). Oracle implements only the GLOBAL version.

The data you put into an Oracle Temporary table is specific to your session. That is, only you can see your data even if there are 100 users all using the same table, and your data is deleted from the table when you disconnect (or when you commit the current transaction) depending upon table settings.

Contrast this with MS SQL-Server, where temporary tables are local. If you create one, no one besides you knows that your temporary table exists. In Oracle, creating the temporary table allows everyone (well everyone with access to your schema) to see the table. When you log out of your session, the SQL-Server table is deleted and will need to be recreated for the next session. In Oracle, the temporary table is now a permanent part of your schema, even if the data isn't.

本宫微胖 2024-07-18 12:01:42

将此与 MS SQL-Server 进行对比,其中临时表位于本地。 如果您创建一个临时表,除了您之外没有人知道您的临时表的存在。 在 Oracle 中,创建临时表允许每个人(以及有权访问您的模式的每个人)查看该表。 当您注销会话时,SQL-Server 表将被删除,并且需要为下一个会话重新创建。 在 Oracle 中,临时表现在是架构的永久部分,即使数据不是(如果不是,那么您可以决定是否保留它)。
Oracle仅支持全局临时表,无需在每个会话中创建表; 它“存在”,但它是空的,并且它的内容在每个会话中都是唯一的(并且是私有的)。

Contrast this with MS SQL-Server, where temporary tables are local. If you create one, no one besides you knows that your temporary table exists. In Oracle, creating the temporary table allows everyone (well everyone with access to your schema) to see the table. When you log out of your session, the SQL-Server table is deleted and will need to be recreated for the next session. In Oracle, the temporary table is now a permanent part of your schema, even if the data isn't (if not so you can decide whether to preserve it).
The Oracle supports only global temporary table saves you from having to create the table in each session; it 'exists' but it is empty, and its content is unique (and private) per session.

吝吻 2024-07-18 12:01:42

请注意,全局临时表没有与之关联的统计信息,因此请检查是否应设置实例的动态采样级别以确保在解析时对未分析的表进行采样。 否则,启发式方法可能会导致糟糕的执行计划。

Be aware that a global temporary table has no statistics associated with it, so look into whether the dynamic sampling level for the instance should be set to ensure that unanalyzed tables are sampled at parse time. Otherwise the heuristics can lead to a nasty execution plan.

妞丶爷亲个 2024-07-18 12:01:42

只是为了添加有关本地和全局临时表的现有答案,从 Oracle 18c 将会真正"私有临时表"

私有临时表是在事务或会话结束时自动删除的临时数据库对象。私有临时表存储在内存中,并且仅对创建它的会话可见。

私有临时表将临时表的范围限制为会话或事务,从而为应用程序编码提供更大的灵活性,从而使代码维护更容易并提供更好的即用型功能。

来自的演示Oracle Live SQL:18c 私有临时表

-- Private temporary tables must be prefixed as per the database parameter 
-- 'private_temp_table_prefix' 

create private temporary table ORA$PTT_MY_TT ( x int );

-- The table is truly private. 
-- It does not even exist in the the data dictionary, only your session 

-- By default, the moment you commit, the table ceases to exist 
commit;

select * from ORA$PTT_MY_TT;
-- ORA-00942: table or view does not exist

-- This can be changed by specifying that the definition should be preserved 
create private temporary table ORA$PTT_MY_TT ( x int )  
on commit preserve definition;

insert into ORA$PTT_MY_TT  
select rownum from dual  
connect by level <= 30;

commit;

select count(*) from ORA$PTT_MY_TT;
-- 30 

db<>fiddle 演示

Just to add to existing answers about local and global temporary tables, from Oracle 18c there will be trully "Private Temporary Tables":

Private temporary tables are temporary database objects that are automatically dropped at the end of a transaction or a session. A private temporary table is stored in memory and is visible only to the session that created it.

A private temporary table confines the scope of a temporary table to a session or a transaction, thus providing more flexibility in application coding, leading to easier code maintenance and a better ready-to-use functionality.

Demo from Oracle Live SQL: 18c private temporary tables:

-- Private temporary tables must be prefixed as per the database parameter 
-- 'private_temp_table_prefix' 

create private temporary table ORA$PTT_MY_TT ( x int );

-- The table is truly private. 
-- It does not even exist in the the data dictionary, only your session 

-- By default, the moment you commit, the table ceases to exist 
commit;

select * from ORA$PTT_MY_TT;
-- ORA-00942: table or view does not exist

-- This can be changed by specifying that the definition should be preserved 
create private temporary table ORA$PTT_MY_TT ( x int )  
on commit preserve definition;

insert into ORA$PTT_MY_TT  
select rownum from dual  
connect by level <= 30;

commit;

select count(*) from ORA$PTT_MY_TT;
-- 30 

db<>fiddle demo

喜爱纠缠 2024-07-18 12:01:42

此外,当每个用户/会话都需要查看不同的数据集时,Oracle(全局)临时表非常非常有用。 只需将记录插入到全局临时表中,然后让 Oracle 管理将一个用户的集与另一个用户的集保持一致以及进行清理。 您不需要使用用户 ID、会话 ID 或其他任何内容来查询它们。

我们发现它们非常方便。

Additionally, Oracle (global) temp tables are very useful when each of your users/sessions need to each see a different set of data. Just INSERT the records to your global temp table and let Oracle manage keeping one user's set from another's, as well as the cleanup. You don't need to query them with the user's ID, a session id or whatever.

We find them very handy.

方圜几里 2024-07-18 12:01:42

没有临时表,只有全局临时表。
全局临时表的想法是定义存在并且可以被所有人看到,但数据对于每个会话来说是私有的。 您还可以配置是在提交时清理数据还是仅在会话结束时清理数据。

There is no temporary table, only global temporary table.
The idea of a global temporary table is that the definition exists and can be seen by all, but data is private for each session. You can also configure if the data is cleaned upon commit or only when the session ends.

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