PL / SQL拆分表

时间:2018-12-16 11:42:25

标签: oracle performance plsql

我在Oracle中有一张表。我要制作几个表,每个表包含该表的1/10数据(实际上我只需要一列)。我能够编写以下代码,但由于每次运行在整个原始表中时,它似乎并不高效。

declare
  baseObjid  Number := 100;
  chunkSize  Number;
  totalCount Number;
begin
  select count(1) into totalCount from table_person;
  chunkSize := trunc(totalCount / 10) + 1;
  for i in 1 .. 10 loop
    execute immediate 'create table table_person_' || i ||
                      ' AS (select sel.r + ' || baseObjid ||
                      ' objid,  sel.objid oldId from 
                      (select rownum r, objid from table_person order by objid) sel 
                      where sel.r > ' || (i - 1) * chunkSize || 
                      ' and sel.r <= ' || i * chunkSize || ')';
    commit;
  end loop;
end;

是否有一种方法可以使它只访问原始表一次?任何其他建议也欢迎。

1 个答案:

答案 0 :(得分:5)

从单个查询填充多个表的最简单方法是INSERT ALL语句。

此查询通过将PERSON的模数应用于驱动PERSON_1中的PERSON_10,在十个预先创建的目标表rownum .. select中拆分insert all when rn = 1 then into person_1 (id) values (id) when rn = 2 then into person_2 (id) values (id) when rn = 3 then into person_3 (id) values (id) when rn = 4 then into person_4 (id) values (id) when rn = 5 then into person_5 (id) values (id) when rn = 6 then into person_6 (id) values (id) when rn = 7 then into person_7 (id) values (id) when rn = 8 then into person_8 (id) values (id) when rn = 9 then into person_9 (id) values (id) else into person_10 values (id) select id, mod(rownum,10) as rn from person; 的ID }。您可以修改查询的投影以提供其他条件。

insert all
    when id < 1000 then into person_1 (id) values (id) 
    when id < 2000 then into person_2 (id) values (id) 
    when id < 3000 then into person_3 (id) values (id) 
    when id < 4000 then into person_4 (id) values (id) 
    when id < 5000 then into person_5 (id) values (id) 
    when id < 6000 then into person_6 (id) values (id) 
    when id < 7000 then into person_7 (id) values (id) 
    when id < 8000 then into person_8 (id) values (id) 
    when id < 9000 then into person_9 (id) values (id) 
    else into person_10 values (id) 
select id from person; 

或者,您可以通过更改WHEN标准(例如将其更改为存储桶)来推动分配:

rownum

很明显,存储桶可能取决于您发布的代码中的2018/12/16 08:43:54 Starting overwatch 2018/12/16 08:43:54 Using in-cluster config to connect to apiserver 2018/12/16 08:43:54 Using service account token for csrf signing 2018/12/16 08:43:54 No request provided. Skipping authorization 2018/12/16 08:43:54 Successful initial request to the apiserver, version: v1.12.1 2018/12/16 08:43:54 Generating JWE encryption key 2018/12/16 08:43:54 New synchronizer has been registered: kubernetes-dashboard-key-holder-kube-system. Starting 2018/12/16 08:43:54 Starting secret synchronizer for kubernetes-dashboard-key-holder in namespace kube-system 2018/12/16 08:43:55 Initializing JWE encryption key from synchronized object 2018/12/16 08:43:55 Creating in-cluster Heapster client 2018/12/16 08:43:55 Successful request to heapster 2018/12/16 08:43:55 Auto-generating certificates 2018/12/16 08:43:55 Successfully created certificates 2018/12/16 08:43:55 Serving securely on HTTPS port: 8443 2018/12/16 08:44:19 Getting application global configuration 2018/12/16 08:44:19 Application configuration {"serverTime":1544949859551} 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 GET /api/v1/settings/global request from 10.32.0.1:53200: {} 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 GET /api/v1/login/status request from 10.32.0.1:53200: {} 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 GET /api/v1/systembanner request from 10.32.0.1:53200: {} 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 GET /api/v1/login/status request from 10.32.0.1:53200: {} 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 GET /api/v1/rbac/status request from 10.32.0.1:53200: {} 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 GET /api/v1/csrftoken/token request from 10.32.0.1:53200: {} 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 POST /api/v1/token/refresh request from 10.32.0.1:53200: { contents hidden } 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Incoming HTTP/2.0 GET /api/v1/overview/default?filterBy=&itemsPerPage=10&name=&page=1&sortBy=d,creationTimestamp request from 10.32.0.1:53200: {} 2018/12/16 08:44:20 Getting config category 2018/12/16 08:44:20 Getting discovery and load balancing category 2018/12/16 08:44:20 Getting lists of all workloads 2018/12/16 08:44:20 No metric client provided. Skipping metrics. 2018/12/16 08:44:20 No metric client provided. Skipping metrics. 2018/12/16 08:44:20 No metric client provided. Skipping metrics. 2018/12/16 08:44:20 No metric client provided. Skipping metrics. 2018/12/16 08:44:20 No metric client provided. Skipping metrics. 2018/12/16 08:44:20 No metric client provided. Skipping metrics. 2018/12/16 08:44:20 No metric client provided. Skipping metrics. 2018/12/16 08:44:20 Getting pod metrics 2018/12/16 08:44:20 [2018-12-16T08:44:20Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 Getting application global configuration 2018/12/16 08:44:24 Application configuration {"serverTime":1544949864040} 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 GET /api/v1/settings/global request from 10.32.0.1:53200: {} 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 GET /api/v1/login/status request from 10.32.0.1:53200: {} 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 GET /api/v1/systembanner request from 10.32.0.1:53200: {} 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 GET /api/v1/login/status request from 10.32.0.1:53200: {} 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 GET /api/v1/rbac/status request from 10.32.0.1:53200: {} 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 GET /api/v1/csrftoken/token request from 10.32.0.1:53200: {} 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 POST /api/v1/token/refresh request from 10.32.0.1:53200: { contents hidden } 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Outcoming response to 10.32.0.1:53200 with 200 status code 2018/12/16 08:44:24 [2018-12-16T08:44:24Z] Incoming HTTP/2.0 GET /api/v1/overview/default?filterBy=&itemsPerPage=10&name=&page=1&sortBy=d,creationTimestamp request from 10.32.0.1:53200: {} . . . 列。


值得考虑将表创建与数据填充分离的好处。

在发布的代码中,如果例程在中途失败(例如无法扩展表空间),则将填充并访问某些表(因为DDL问题已提交,因此没有回滚)。修复错误源之后,您将必须清除(即删除)这些表,然后重新运行例程。或者,您可以更改代码并跳过这些表,但这总是有问题的,尤其是因为两次运行之间可能会对源表进行更改,因此最终状态可能会不一致。

首先创建空表将提供可恢复的位置。 INSERT ALL是单个语句,它不仅比十个选择具有更高的执行性能,而且还意味着填充了所有表或没有表。