PostgreSQL 相当于 SQLServer 的 NoLock 提示

发布于 2024-08-24 07:16:51 字数 395 浏览 11 评论 0原文

在 SQLServer 中,您可以使用语法“(nolock)”来确保查询不会锁定表或不会被锁定同一表的其他查询阻塞。 例如

SELECT * FROM mytable (nolock) WHERE id = blah

Postgres 中的等效语法是什么?我在 PG 中找到了一些关于表锁定的文档 (http://www.postgresql .org/docs/8.1/interactive/sql-lock.html),但这一切似乎都是针对如何锁定表,而不是确保它未锁定 >。

In SQLServer, you can use syntax "(nolock)" to ensure the query doesn't lock the table or isn't blocked by other queries locking the same table.
e.g.

SELECT * FROM mytable (nolock) WHERE id = blah

What's the equivalent syntax in Postgres? I found some documentation on table locking in PG (http://www.postgresql.org/docs/8.1/interactive/sql-lock.html), but it all seems geared at how to lock a table, not ensure it's not locked.

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

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

发布评论

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

评论(5

嘴硬脾气大 2024-08-31 07:16:51

SELECT 不会锁定 PostgreSQL 中的任何表,除非您想要锁定:

SELECT * FROM tablename FOR UPDATE;

PostgreSQL 使用 MVCC 最大限度地减少锁争用,以便在多用户环境中实现合理的性能。读者不会与作者或其他读者发生冲突。

A SELECT doesn't lock any table in PostgreSQL, unless you want a lock:

SELECT * FROM tablename FOR UPDATE;

PostgreSQL uses MVCC to minimize lock contention in order to allow for reasonable performance in multiuser environments. Readers do not conflict with writers nor other readers.

我做了一些研究,发现 SQL Server 中的 NOLOCK 提示与 READ UNCOMMITTED 事务隔离级别大致相同。在 PostgreSQL 中,您可以设置 READ UNCOMMITTED,但它会默默地将级别升级到 READ COMMITTED。不支持READ UNCOMMITTED

PostgreSQL 8.4 事务隔离文档: http://www.postgresql.org/ docs/8.4/static/transaction-iso.html

I've done some research and it appears that the NOLOCK hint in SQL Server is roughly the same as READ UNCOMMITTED transaction isolation level. In PostgreSQL, you can set READ UNCOMMITTED, but it silently upgrades the level to READ COMMITTED. READ UNCOMMITTED is not supported.

PostgreSQL 8.4 documentation for Transaction Isolation: http://www.postgresql.org/docs/8.4/static/transaction-iso.html

逆流 2024-08-31 07:16:51

这是一个老问题,但我认为实际问题还没有得到解答。

SELECT 查询(不包含 for update 子句)永远不会锁定任何行(或表),也不会阻止对表的并发访问。并发 DML(INSERT、UPDATE、DELETE)也不会阻塞 SELECT 语句。

简而言之:Postgres 中不需要 (nolock)
读者永远不会阻止作家,作家也永远不会阻止读者

This is an old question, but I think the actual question has not been answered.

A SELECT query (that does not contain an for update clause) will never lock any rows (or the table) nor will it block concurrent access to the table. Concurrent DML (INSERT, UPDATE, DELETE) also will not block a SELECT statement.

Simply put: there is no need for (nolock) in Postgres.
Readers never block writers and writers never block readers

疏忽 2024-08-31 07:16:51

nolock 或 readpast 的目的是查看记录当前是否被锁定。用户可以在更新中使用它来查看标识的记录是否已更改(受影响的行);如果记录没有被锁定,那么therowsaffected将为1;如果 o,则记录被锁定

根据该结果,用户可以使用选择更新来锁定它以供自己使用。

The purpose of the nolock or readpast is to see if the record is currenlty locked. The user can use this in an update to see if the record identified was changed (rowsaffected); if the record was not locked, then therowsaffected would be 1; if o, then the record is locked

Based upon that outcome, then the user can use a select for update to lock it for their own use.

っ左 2024-08-31 07:16:51

每个 SQL 语句都是一个隐式事务。 NOLOCK 提示对应于 READ UNCOMMITTED (DIRTY READ) 事务隔离级别

BEGIN TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT COUNT(1) FROM my_table;
END;

实际上,此代码的作用与 BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED 相同,但进一步保证了预期的行为。

还要避免使用 COUNT(*) 除非你确实需要它

Every SQL statement is an implicit transaction. The NOLOCK hint corresponds to READ UNCOMMITTED (DIRTY READ) transaction isolation level.

BEGIN TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT COUNT(1) FROM my_table;
END;

Actually, this code do the same that BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED but guarantees the expected behavior further.

Also avoid to use COUNT(*) except you really need it

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