欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在 Oracle 中同时使用插入选择和选择插入

最编程 2024-06-16 19:42:58
...

Insert Into select 与 Select Into 哪个更快?

1在平常数据库操作的时候,我们有时候会遇到表之间数据复制的情况,可能会用到INSERT INTO SELECT 或者 SELECT INTO ;
2
3那么二者语法上有什么区别?性能上又如何呢?
4
5围绕着这两个问题,今天就来总结对比下:
6
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">

一:语法区别

1: INSERT INTO SELECT 的语法

 1INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。
2如果两个表的结构一致,字段类型一致:
3
4INSERT INTO table2
5SELECT * FROM table1;
6如果两个表结构不一致,只有某几列一致:
7
8INSERT INTO table2 (column_name)
9SELECT column_name FROM table1;
10
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">

2:SELECT INTO 的语法

 1SELECT INTO 语句从一个表复制数据,然后把数据插入到另一个新表中。
2如果想要复制所有的列:
3
4SELECT *
5INTO newtable
6FROM table1;
7如果想要复制部分的列:
8
9SELECT column_name
10INTO newtable
11FROM table1;
12提示:新表将会使用 SELECT 语句中定义的列名称和类型进行创建。如果想要修改字段的名称,可以使用 AS 子句来应用新名称。
13
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">
 class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: #f82375; word-wrap: inherit !important; word-break: inherit !important;">

二:性能区别

 1----创建一个基础数据库
2CREATE TABLE TestDataTable(
3    id int not null,
4    userName varchar(20),
5    remark varchar(100)
6)
7----填充100W测试数据
8DECLARE @id INT,@userName NVARCHAR(50),@remark NVARCHAR(50);
9DECLARE @i INT;
10
11SET @id=0;
12SET @userName ='';
13SET @remark='';
14SET @i=0;
15
16WHILE @i<1000000
17BEGIN
18    SET @id=@i;
19    IF(@i%2=0)
20        begin
21            set @userName='二狗子';
22            set @remark='SELECT INTO 最快!'
23        end
24    else
25        begin
26            set @userName='李四';
27            set @remark='快你妹,INSERT INTO 最快'
28        end
29    INSERT INTO TestDataTable(id,username,remark)
30    VALUES (@id,@username,@remark )
31    SET @i=@i+1
32END
33<br>--查询填充后的数据
34SELECT * FROM TestDataTable<br>--删除表结构以及数据【慎用】
35DROP Table TestDataTable
36耗时1分钟58秒,数据填充好了,真是不给力,竟然用了快2分钟;
37
38
39
40好了,在去创建一个目标数据库;
41
42
43--创建一个目标数据库,把100W数据复制到这个里面取

上一篇: 用 Javascript 实现简单的冒泡排序、插入排序 - 代码实现

下一篇: 史上最完整的 itext7 真实世界摘要

推荐阅读