简体   繁体   English

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

[英]What is the difference between a temporary table vs global temporary table in Oracle?

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? 两者有什么区别?

In Oracle there isn't any difference. 在Oracle中没有任何区别。 When you create a temporary table in an Oracle database, it is automatically global, and you are required to include the "Global" key word. 在Oracle数据库中创建临时表时,它将自动为全局表,并且您需要包含“全局”关键字。

The SQL standard, which defines how the term "GLOBAL TEMPORARY TABLE" is interpreted, allows for either a LOCAL or GLOBAL scope. SQL标准定义了术语“GLOBAL TEMPORARY TABLE”的解释方式,它允许LOCAL或GLOBAL范围。 This would allow for either a user specific table (LOCAL) or everyone (GLOBAL). 这将允许用户特定表(LOCAL)或每个人(GLOBAL)。 Oracle implements only the GLOBAL version. Oracle仅实现GLOBAL版本。

The data you put into an Oracle Temporary table is specific to your session. 您放入Oracle临时表的数据特定于您的会话。 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. 也就是说,即使有100个用户都使用同一个表,也只能看到您的数据,并且当您断开连接时(或提交当前事务时),您的数据将从表中删除,具体取决于表设置。

Contrast this with MS SQL-Server, where temporary tables are local. 将其与MS SQL-Server进行对比,其中临时表是本地的。 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. 在Oracle中,创建临时表允许每个人(每个人都可以访问您的模式)查看该表。 When you log out of your session, the SQL-Server table is deleted and will need to be recreated for the next session. 当您退出会话时,将删除SQL-Server表,并且需要为下一个会话重新创建。 In Oracle, the temporary table is now a permanent part of your schema, even if the data isn't. 在Oracle中,临时表现在是模式的永久部分,即使数据不是。

Contrast this with MS SQL-Server, where temporary tables are local. 将其与MS SQL-Server进行对比,其中临时表是本地的。 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. 在Oracle中,创建临时表允许每个人(每个人都可以访问您的模式)查看该表。 When you log out of your session, the SQL-Server table is deleted and will need to be recreated for the next session. 当您退出会话时,将删除SQL-Server表,并且需要为下一个会话重新创建。 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). 在Oracle中,临时表现在是模式的永久部分,即使数据不是(如果不是这样,您可以决定是否保留它)。 The Oracle supports only global temporary table saves you from having to create the table in each session; Oracle仅支持全局临时表,使您不必在每个会话中创建表; it 'exists' but it is empty, and its content is unique (and private) per session. 它“存在”但它是空的,其内容在每个会话中是唯一的(和私有的)。

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. 否则,启发式方法可能会导致恶劣的执行计划。

Just to add to existing answers about local and global temporary tables, from Oracle 18c there will be trully "Private Temporary Tables" : 只是为了添加关于本地和全局临时表的现有答案,从Oracle 18c中将会出现“私人临时表”

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 : 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 demo db <>小提琴演示

Additionally, Oracle (global) temp tables are very useful when each of your users/sessions need to each see a different set of data. 此外,当每个用户/会话需要每个用户/会话看到不同的数据集时,Oracle(全局)临时表非常有用。 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. 只需将记录插入到全局临时表中,让Oracle管理一个用户的集合,以及清理。 You don't need to query them with the user's ID, a session id or whatever. 您不需要使用用户的ID,会话ID或其他任何内容来查询它们。

We find them very handy. 我们发现它们非常方便。

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. 您还可以配置是在提交时还是仅在会话结束时清除数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM