SQL保证开始/结束索引?

发布于 2024-08-06 02:55:11 字数 383 浏览 7 评论 0原文

我(将)有数十万条记录,我插入一次,永远不会更新许多行持有相同的 previousId。我可以保证开始/结束索引吗?我在哪里用事务在 table_c 中插入 X 行并将开始和结束(或开始和长度或结束和长度)写入 table_b 而不是让每行保存 table_b ID?

如果是的话我该如何编写SQL?我在想

begin transaction
insert XYZ rows into tbl_c
c_rowId = last_insert_rowid
insert table_b with data + start=c_rowId-lengthOfInsert, end=c_rowId;
commit; end transaction

这会像我预期的那样工作吗?

I (will) have hundreds of thousand of records where i insert once, never update with many rows holding the same previousId. Can i guaranteed a start/end index? where i insert X number of rows in table_c with a transaction and write the start and end (or start and length or end and length) into table_b instead of having each row hold table_b ID?

if so how do i write the SQL? i was thinking

begin transaction
insert XYZ rows into tbl_c
c_rowId = last_insert_rowid
insert table_b with data + start=c_rowId-lengthOfInsert, end=c_rowId;
commit; end transaction

would this work as i expect?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-08-13 02:55:11

看来您想要的是一个自动编号列。在大多数 DBMS 中,您可以将此列定义为表定义的一部分,并且它将自动对行进行编号。您可以使用函数来获取最后插入的行的ID;在 SQL Server (T-SQL) 中,您可以使用 SCOPE_IDENTITY() 函数。如果 ID 必须是特定值或值范围,您可能需要手动执行此操作并使用锁定提示来防止另一个并发事务修改您的标识符信息。

It seems like what you want is an autonumber column. In most DBMS, you can define this column as part of the table definition, and it will number the rows automatically. You can use a function to get the ID of the last row inserted; in SQL Server (T-SQL), you can use the SCOPE_IDENTITY() function. If the IDs -have- to be a certain value or range of values, you may need to do it manually and use a locking hint to prevent another concurrent transaction from modifying your identifier information.

毁梦 2024-08-13 02:55:11

您可以使用 tabblockx 锁定 sql server 中的全表。

所以:

begin the transaction, 
select max(txnid) from table with (tablockx)  -- now the table is locked

--figure out how many more you are going to insert 
lastId = maxNum + numToInsert

set allow insert auto_increment on insert -- not sure what this is

while (moreToInsert)
  insert int table (id,x,y) values (id+1,'xx','yy')

commit transaction

这样做的问题是它完全锁定了表。自动递增列(auto_increment (mysql) 或identity mssql)可能是您真正想要的,并且不会限制对整个表的访问。 (这个在另一个答案里有提到)

You can lock the full table in sql server by using tablockx.

so:

begin the transaction, 
select max(txnid) from table with (tablockx)  -- now the table is locked

--figure out how many more you are going to insert 
lastId = maxNum + numToInsert

set allow insert auto_increment on insert -- not sure what this is

while (moreToInsert)
  insert int table (id,x,y) values (id+1,'xx','yy')

commit transaction

The problem with this though is that it totally locks the table. An auto incrementing column (auto_increment (mysql) or identity mssql) might be what you really want and would not limit access to the whole table. (This was noted in another answer)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文