简体   繁体   English

SQL 服务器 SELECT 带有 READONLY 子句

[英]SQL Server SELECT with READONLY clause

I wanted to read some data from a table in the read-only mode.我想以只读模式从表中读取一些数据。

Having worked on DB2 earlier, I got confused and tried to use the FOR READONLY clause with my SELECT statement:P之前在 DB2 上工作过后,我感到困惑并尝试在我的 SELECT 语句中使用 FOR READONLY 子句:P

After tinkering with it, I realized the following statement works:修修补补后,我意识到以下语句有效:

SELECT * FROM dbo.Users READONLY

It took less than half the time to run the query with the READONLY clause than without it.使用 READONLY 子句运行查询所花的时间比没有它的少一半。

So I decided to google for some documentation about the same, but I couldn't find any documentation of using READONLY with the SELECT clause.所以我决定用谷歌搜索一些关于相同的文档,但我找不到任何使用 READONLY 和 SELECT 子句的文档。

Our DBA has asked us not to use the READONLY clause if there is no documentation, as it can get obsolete in the newer updates/ versions.如果没有文档,我们的 DBA 要求我们不要使用 READONLY 子句,因为它可能在较新的更新/版本中过时。

I'd appreciate if someone can point to some useful documentation and let me know if this can be safely used in SQL Server stored procs.如果有人可以指出一些有用的文档并让我知道这是否可以安全地用于 SQL 服务器存储过程,我将不胜感激。

I am using SQL Server version 11.0.3000.0我正在使用 SQL 服务器版本 11.0.3000.0

Old question, but it deserves a new answer in case someone thinks they've stumbled on a magic go-faster switch. 这是个老问题,但是如果有人认为自己偶然发现了一种神奇的快速开关,那么它就应该得到一个新的答案。

This has the same semantics as SELECT * FROM dbo.Users PLEASE_BUFF_MY_PYLONS , and SELECT * FROM Users AS U . 这与SELECT * FROM dbo.Users PLEASE_BUFF_MY_PYLONSSELECT * FROM Users AS U具有相同的语义。 It assigns the alias READONLY to the table Users and has no effect whatsoever on execution plans or query execution time on its own, and anyone stating otherwise better have some damn good evidence to back it up (as in, an execution plan that does not show READONLY is just an alias). 它为表Users分配了READONLY别名,它本身对执行计划或查询执行时间没有任何影响 ,并且任何表示更好的人都有一些可靠的证据来备份它(例如,执行计划显示READONLY只是一个别名)。 READONLY is a T-SQL keyword, but only in the context of passing table variables to stored procedures. READONLY T-SQL关键字,但仅在将表变量传递到存储过程的上下文中。 In the context of this query, it's just another identifier. 在此查询的上下文中,它只是另一个标识符。

There are various reasons why a query can be slower in one case, faster in another (cached execution plans are one big reason); 有多种原因使查询在一种情况下变慢,而在另一种情况下变快(缓存的执行计划是一个重要原因)。 aliasing your table to the name READONLY is not one of them. 将表别名为READONLY的名称不是其中之一。

While there's no such thing as "readonly" mode for a query, there is such a thing as reading from a table without blocking writers by using snapshot isolation . 虽然没有这样的东西作为一个查询“只读”模式, 这样的事,作为从表中读取,而不使用阻断作家快照隔离 This is not a magic go-faster switch either, but it is useful in many OLTP workloads. 这也不是一种快速的魔术开关,但是在许多OLT​​P工作负载中很有用。

This is a sample script to highlight what the READONLY means in the context you used it: 这是一个示例脚本,以突出显示READONLY在您所使用的上下文中的含义:

DECLARE @t TABLE(id INT);
INSERT INTO @t(id)VALUES(1),(2),(3);

SELECT READONLY.id FROM @t READONLY;

Resulting in: 导致:

id
1
2
3

In other words, you assigned an alias named READONLY to the table dbo.users (in the sample, to the table variable @t ). 换句话说,您为表dbo.users (在示例中,表变量@t )分配了一个名为READONLY的别名。

I would advise against using this as an alias. 我建议不要使用它作为别名。 It may not result in an error, but it can be used in another context in SQL Server (declaring a table valued parameter in a stored procedure). 它可能不会导致错误,但是可以在SQL Server的另一个上下文中使用(在存储过程中声明表值参数)。 Perhaps READONLY should have been a reserved keyword for this reason, as it may be confusing to see this word used as a keyword in one case, and as a table alias in another. 出于这个原因,也许READONLY应该是保留关键字,因为在某种情况下将此单词用作关键字,而在另一情况下用作表别名可能会造成混淆。

Normally, you do not need to include the FOR READ ONLY clause in a SELECT statement. 通常,您不需要在SELECT语句中包括FOR READ ONLY子句。 SELECT is a read-only operation by definition, so the FOR READ ONLY clause is usually unnecessary. 根据定义,SELECT是只读操作,因此通常不需要FOR READ ONLY子句。 In certain circumstances, however, you must include the FOR READ ONLY keywords in a SELECT statement. 但是,在某些情况下,必须在SELECT语句中包括FOR READ ONLY关键字。

see this article for more information. 有关更多信息, 请参见本文

Updated link: 更新的链接:

The link in bumbumpaw's reply is no longer valid. bumbumpaw的回复中的链接不再有效。 I found this in the IBM Knowledge Center regarding the use of the "FOR READ ONLY" clause when querying against an Informix database. 我在IBM知识中心找到有关在查询Informix数据库时使用“ FOR READ ONLY”子句的问题。

https://www.ibm.com/support/knowledgecenter/en/SSGU8G_11.50.0/com.ibm.sqls.doc/ids_sqs_1062.htm https://www.ibm.com/support/knowledgecenter/zh-CN/SSGU8G_11.50.0/com.ibm.sqls.doc/ids_sqs_1062.htm

As Jeroen Mostert points out, Microsoft SQL Server does not contain a "READONLY" clause, even though it is a reserved keyword. 正如Jeroen Mostert指出的那样,即使Microsoft SQL Server是保留关键字,它也不包含“ READONLY”子句。 It does, however, have an option to set the database level to "READ_ONLY", which is outside the scope of this question. 但是,它确实可以选择将数据库级别设置为“ READ_ONLY”,这不在此问题的范围内。

https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-set-options?view=sql-server-2017 https://docs.microsoft.com/zh-cn/sql/t-sql/statements/alter-database-transact-sql-set-options?view=sql-server-2017

See the HINTS you can use here https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table?view=sql-server-ver16请参阅此处可以使用的提示https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table?view=sql-server-ver16

The NOLOCK is the "READONLY" you are searching for NOLOCK 是您正在搜索的“READONLY”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM