系统在一次插入太多数据后陷入瘫痪

问题描述 投票:0回答:2

我开发了一个库存系统,当用户决定注册项目时,需要跟踪每个序列号的项目。

系统将根据给定的初始序列号和结束序列号生成序列号。因此,假设一盒物品有1000个物品,每个物品都有自己的序列号,从初始序列号1开始。根据上下文,系统将逐个生成每个序列号,例如;当用户提交表单时,db中的每行1,2,3 ... 1000行

现在,当项目的数量很小时,实现完全正常,但在处理大量数据时它开始使系统瘫痪。高达100万。

除了每次用户注册项目时逐行存储序列号以外,还有其他问题吗?

有人帮我吗?

php oracle laravel database-design
2个回答
1
投票

您基本上可以获得您编码的性能......例如

SQL> create table t ( x int );

Table created.

--
-- 100k unprepared statements, each with a commit
--
SQL>
SQL> set timing on
SQL> begin
  2    for i in 1 .. 100000 loop
  3      execute immediate 'insert into t values ('||i||')';
  4      commit;
  5    end loop;
  6  end;
  7  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:56.91

--
-- 100k prepared statements, each with a commit
--

SQL>
SQL> set timing on
SQL> begin
  2    for i in 1 .. 100000 loop
  3      insert into t values (i);
  4      commit;
  5    end loop;
  6  end;
  7  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:06.81

--
-- 100k prepared statements, one commit
--

SQL>
SQL> set timing on
SQL> begin
  2    for i in 1 .. 100000 loop
  3      insert into t values (i);
  4    end loop;
  5    commit;
  6  end;
  7  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:01.73


--
-- 1 array-based insert prepared statements, one commit
--

SQL> set timing on
SQL> declare
  2    type numlist is table of number
  3      index by pls_integer;
  4    n numlist;
  5  begin
  6    for i in 1 .. 100000 loop
  7      n(i) := i;
  8    end loop;
  9    forall i in 1 .. 100000
 10      insert into t values (n(i));
 11    commit;
 12  end;
 13  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.07
SQL>
SQL>

--
-- or we can bump those number up a bit
--
SQL> set timing on
SQL> declare
  2    type numlist is table of number
  3      index by pls_integer;
  4    n numlist;
  5  begin
  6    for i in 1 .. 10000000 loop
  7      n(i) := i;
  8    end loop;
  9    forall i in 1 .. 10000000
 10      insert into t values (n(i));
 11    commit;
 12  end;
 13  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:05.15

which is around 2million rows per second (on my laptop)

0
投票

我不确定我是否明白你是怎么做到的。

但是,如果你看一下这个Oracle演示(因为我不知道Laravel),你会看到插入一百万行没有花费时间和没有(重要)资源。希望你可以在你的系统上做类似的事情。

SQL> create table serial (id number);

Table created.

SQL> create or replace procedure p_serial (par_start in number, par_end in number)
  2  is
  3  begin
  4    insert into serial (id)
  5      select level + par_start - 1
  6      from dual
  7      connect by level <= par_end - par_start + 1;
  8  end;
  9  /

Procedure created.

SQL> set timing on
SQL> begin
  2    p_serial(1, 1000000);  -- 1 million
  3  end;
  4  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.83                         --> 83 miliseconds
SQL> set timing off
SQL> select count(*), min(id) min_id, max(id) max_id from serial;

  COUNT(*)     MIN_ID     MAX_ID
---------- ---------- ----------
   1000000          1    1000000             --> 1 million values inserted

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.