如何实现多维序列

2023-11-29

例如,这是每年的序列。这no增量与year:

| no | year |
+----+------+
|  1 | 2016 |
|  2 | 2016 |
|  3 | 2016 |
|  1 | 2017 |
|  2 | 2017 |
|  4 | 2016 |

现在我已经为每年创建了序列
但问题是Oracle不会在明年自动创建新的序列。

另一个问题是如果我想使用 3D 序列,在year and type:

| no | year | type |
+----+------+------+
|  1 | 2016 |    a |
|  2 | 2016 |    a |
|  1 | 2016 |    b |
|  1 | 2017 |    b |
|  2 | 2017 |    b |
|  1 | 2017 |    c |

数据库中的序列太多

我不推荐max(no)由于并行访问问题。 我尝试在获取之前锁定表max(no)在触发器中,但它导致了死锁。


做到这一点的唯一方法是使用代码控制表......

create table code_control
    (year number(4,0) not null
     , type varchar2(1) not null
     , last_number number(38,0) default 1 not null
     , primary key (year,type)
    )
organization index
/   

...这样维护...

create or replace function get_next_number
    (p_year in number, p_type in varchar2)
    return number
is
    pragma autonomous_transaction;
    cursor cur_cc is
        select last_number + 1
        from code_control cc
        where cc.year= p_year
        and cc.type = p_type
        for update of last_number;
    next_number number;
begin
    open cur_cc;
    fetch cur_cc into next_number;
    if cur_cc%found then
        update code_control
        set last_number = next_number
        where current of cur_cc;
    else
        insert into code_control (year,type)
        values (p_year, p_type)
        returning last_number into next_number;
    end if;    
    commit;
    return next_number;
end;
/

重要的是 SELECT ... FOR UPDATE。悲观锁定保证多用户环境中的唯一性。 PRAGMA 确保维护code_control不会污染更广泛的交易。它允许我们在触发器中调用函数而不会出现死锁。

这是一张带有与您类似的密钥的表:

create table t42
     (year number(4,0) not null
     , type varchar2(1) not null
     , id number(38,0) 
     , primary key (year,type, id)
)
/
create or replace trigger t42_trg
    before insert on t42 for each row
begin
    :new.id := get_next_number(:new.year, :new.type);
end;
/

在我入住之前我没什么可做的t42:

SQL> select * from code_control;

no rows selected

SQL> select * from t42;

no rows selected

SQL> insert into t42 (year, type) values (2016, 'A');

1 row created.

SQL> insert into t42 (year, type) values (2016, 'A');

1 row created.

SQL> insert into t42 (year, type) values (2016, 'A');

1 row created.

SQL> insert into t42 (year, type) values (2016, 'B');

1 row created.

SQL> insert into t42 (year, type) values (2016, 'A');

1 row created.

SQL> insert into t42 (year, type) values (2017, 'A');

1 row created.

SQL> select * from t42;

      YEAR T         ID
---------- - ----------
      2016 A          1
      2016 A          2
      2016 A          3
      2016 A          4
      2016 B          1
      2017 A          1

6 rows selected.

SQL> select * from code_control;

      YEAR T LAST_NUMBER
---------- - -----------
      2016 A           4
      2016 B           1
      2017 A           1

SQL> 

因此,对这种实现的明显反对是可扩展性。插入事务是序列化的code_control桌子。这绝对是真的。然而,锁定的持有时间尽可能短,因此即使t42表每秒填充多次。

但是,如果表受到大量并发插入,锁定可能会成为问题。该表具有足够的感兴趣事务槽(INITRANS、MAXTRANS)来应对并发需求,这一点至关重要。但非常繁忙的系统可能需要更智能的实现(可能批量生成 ID);否则放弃复合键,转而使用序列(因为序列在多用户环境中确实可以扩展)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何实现多维序列 的相关文章

随机推荐

  • 在处理程序中创建了一个 Promise,但没有从中返回

    我刚刚开始使用 Bluebird Promise 但遇到了一个令人困惑的错误 代码摘要 var jQueryPostJSON function jQueryPostJSON url data return Promise resolve j
  • D3 Y 轴似乎颠倒了

    我正在尝试根据 CSV 文件中的一些数据生成图表 My code
  • 如何检测 Azure 网站上的 HTTPS 重定向?

    根据标题 我有一个 Node js 应用程序 我希望能够检测请求是通过 HTTPS 还是 HTTP 发出 到目前为止我的重定向看起来像这样 Ensure the page is secure or that we are running a
  • 列表中的数据表

    我已经下载了包含在List
  • Java中如何处理关闭应用程序事件?

    拥有一个控制台应用程序 一个接受来自客户端的多个连接的服务器 是否可以在关闭的应用程序上拥有侦听器或事件 在这种情况下 我希望告诉所有连接的客户端在应用程序真正自行关闭之前轻轻断开连接 有什么解决办法吗 谢谢你 您想使用关闭挂钩 Runti
  • 在Win32中,如何改变STATIC文本的颜色?

    我的窗口上有一些黑色的静态文本 我想在运行时使用颜色选择器对话框更改它 我已经成功获得了颜色 现在我只需要更改文字即可 我读过WM CTLCOLORSTATICmessage 是要处理的消息 尽管这似乎仅适用于控件最初绘制到屏幕上时 可以W
  • 更新 Firebase 多位置并出现错误:路径是路径的祖先。迅速

    当我尝试使用以下命令在多个位置更新数据库时 updateChildValues 我收到下面列出的错误 我怎么解决这个问题 由于未捕获的异常 InvalidFirebaseData 而终止应用程序 原因 updateChildValues 对
  • 自定义单元格模板中带有按钮的 UI-Grid - 如何取消行选择事件?

    我正在使用 ui Grid v 3 0 1 我有一个特定列的自定义单元格模板 它在每行中显示一个按钮 我附加了一个 ng click 属性 它调用 appScope 来触发某些操作 一切都很顺利 但是 单击自定义模板按钮也会导致切换相关行的
  • 未找到基表或视图错误

    产品表 public function up Schema create product function Blueprint table table gt increments id table gt string name table
  • 使用 ionic 3 中的 Angularfire2 从 Firestore 获取集合文档的 ID

    这个问题在这里已经有答案了 我正在尝试获取集合的 id 以在离子页面中使用它 这是我的界面 export interface Item categoryOfPost string imageUrl string nameOfPost str
  • 如何使用 Python 3.x 读取和编辑 Google 电子表格?

    我知道我可以使用以下命令阅读和编辑 Google 电子表格gdata 但没有适用于 Python 3 的 gdata 版本 即使我想使用 Python 3 是否有一种 不是很复杂 的方法来编辑 Google 电子表格 我编写了一个替代方案G
  • 如何在 ffmpeg HLS 中启用 cookie

    有人知道如何在 ffmpeg 中启用 cookie 交互吗 我有一个 HLS 流 需要从服务器保存 cookie 但实际上这不会发生 你可以发送标头与 FFmpeg Linux ffmpeg i INPUT headers Cookie l
  • PyQt QLineEdit 与 QValidator

    我的项目中有一个 QLineEdit 我想在 qlineEdit 上使用验证 Create lineEdit itemValue QtWidgets QLineEdit Create objValidator QtGui QDoubleVa
  • 如何逐步安装和使用 joomla rest api

    请帮忙 我想在 joomla 2 5 中使用 REST api 我在 Github 和 techjoomla 上进行了很多搜索并花了很多天 我不明白如何安装 API 扩展或插件并使用它 即使我没有找到任何正确的文档来使用 API 以及从哪里
  • 使用 XSLT 减少 XML 输出

    如何使用 XSLT 仅从输入 xml 中选择一些 xml 标签到输出 XML 输入示例
  • 设置属性logging.pattern.console或logging.pattern.level时忽略覆盖模式

    使用 spring boot 1 3 2 我试图覆盖 application properties 文件中的 logback 模式 但无论我放入什么模式 总是会调用 spring boot jar xml 中的默认模式 我可以成功调整日志记
  • 如何在docker机器中挂载本地卷

    我正在尝试将 docker machine 与 docker compose 一起使用 文件 docker compose yml 的定义如下 web build command run web sh volumes app ports 8
  • 将标量和数组元素传递给需要数组的过程

    我有一些遗留的 Fortran 77 代码 我试图至少在不发出警告的情况下进行编译 不禁用警告 有些子例程调用会传递一个标量 而子例程需要一个数组 因为标量被用作大小为 1 的数组 所以这不会导致任何问题 但是使用英特尔编译器 如果我启用接
  • Python3、Selenium 和 Chrome 可移植

    也许有人可以帮助使用 Windows Python Selenium 以及使用 Chrome Webdriver 和 ChromePortable 我定义了一个新文件夹 c 我的项目 在此文件夹中 网络驱动程序位于 c myproject
  • 如何实现多维序列

    例如 这是每年的序列 这no增量与year no year 1 2016 2 2016 3 2016 1 2017 2 2017 4 2016 现在我已经为每年创建了序列但问题是Oracle不会在明年自动创建新的序列 另一个问题是如果我想使