MySQL数据插入了订单(种族条件?)

发布于 2025-02-05 01:20:06 字数 1289 浏览 3 评论 0原文

我们有一个带有以下DDL的表格:

create table test
(
    a           VARCHAR(36) CHARSET utf8                  NOT NULL,
    b           TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) NOT NULL,
    number      BIGINT UNSIGNED AUTO_INCREMENT,
    updated_at  TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
    PRIMARY KEY (a, b)
)

还有Hibernate映射:

public class TestTable {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "number")
    private Long number;

    @Column(name = "a")
    private String a;

    @Column(name = "b")
    private Date b;

    @Column(name = "updated_at")
    private Date updated_at;
}

我们有一个弹簧启动应用程序,可以公开两个端点。一个端点从该表中读取数据,另一个端点保存了数据。写作比阅读要多得多。

选择查询如下:

SELECT * FROM TestTable where number >= ?

我们面临的问题是,有时我们有两个记录具有以下数据:

  • 记录#1号= 100,updated_at = 2022-01-01-01 00:00:00:00.200000
  • 记录#2号= 101 ,Updated_at = 2022-01-01 00:00:00:00.000000

对于记录#2,auto_increment列'数字'的值高于记录#1'数字'列,但其列的时间戳'updateed_at''记录#1。 记录#1给定的vice-vice-vish vister

以下选择查询将执行:

SELECT * FROM TestTable where number >= 100

执行选择查询时(大概在时间戳记2022-01-01-01-01 00:00:00.100000附近。

如何将这些价值脱离顺序,我们该怎么做才能避免这种情况发生?

We have a table with the following DDL:

create table test
(
    a           VARCHAR(36) CHARSET utf8                  NOT NULL,
    b           TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) NOT NULL,
    number      BIGINT UNSIGNED AUTO_INCREMENT,
    updated_at  TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
    PRIMARY KEY (a, b)
)

And the hibernate mapping:

public class TestTable {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "number")
    private Long number;

    @Column(name = "a")
    private String a;

    @Column(name = "b")
    private Date b;

    @Column(name = "updated_at")
    private Date updated_at;
}

We have a spring-boot app that exposes two endpoints. One endpoint reads data from this table and another one saves the data. There are a lot more writes than reads.

The SELECT query is the following:

SELECT * FROM TestTable where number >= ?

The problem that we are facing is that sometimes we have two records with the following data:

  • record #1 number = 100, updated_at = 2022-01-01 00:00:00.200000
  • record #2 number = 101, updated_at = 2022-01-01 00:00:00.000000

For record #2, the auto_increment column 'number' has a higher value than Record #1 'number' column, but it has a lower timestamp value for column 'updated_at' than Record #1.
Vice-versa for Record #1

Given the following select query is executed:

SELECT * FROM TestTable where number >= 100

When the SELECT query is executed (probably sometime around timestamp 2022-01-01 00:00:00.100000, so between the two timestamps) it will not return record #1.

How is it possible to have these values out of order and what should we do to avoid this from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文