简体   繁体   English

如果查询超时,会发生什么情况?

[英]What Happens To a Query If It Times Out?

Let's say I have a query that is sent to my SQL-Server database, it takes more than 30 seconds, and my program throws an SQL Query Timeout exception.假设我有一个查询发送到我的 SQL-Server 数据库,它需要 30 多秒,并且我的程序抛出 SQL 查询超时异常。 Is the query still chugging along on my database or does it get terminated as soon as the exception is thrown?查询是否仍在我的数据库中运行,还是在抛出异常后立即终止?

A client signals a query timeout to the server using an attention event.客户端使用注意事件向服务器发送查询超时信号。 An attention event is simply a distinct type of TDS packet a SQL Server client can send to it.注意事件只是 SQL 服务器客户端可以发送给它的不同类型的 TDS 数据包。 In addition to connect/disconnect, T-SQL batch, and RPC events, a client can signal an attention to the server.除了连接/断开连接、T-SQL 批处理和 RPC 事件之外,客户端还可以向服务器发出注意信号。 An attention tells the server to cancel the connection's currently executing query (if there is one) as soon as possible.注意告诉服务器尽快取消连接当前正在执行的查询(如果有的话)。 An attention doesn't rollback open transactions, and it doesn't stop the currently executing query on a dime -- the server aborts whatever it was doing for the connection at the next available opportunity.注意不会回滚打开的事务,也不会立即停止当前正在执行的查询——服务器会在下一个可用机会时中止它为连接所做的一切。 Usually, this happens pretty quickly, but not always.通常,这会很快发生,但并非总是如此。

SourceThere's no such thing as a query timeout...来源没有查询超时之类的东西......

When the client decides that the command has run long enough, it issues an "Abort".当客户端决定该命令运行的时间足够长时,它会发出“Abort”。 The query simply stops running in the database.查询只是停止在数据库中运行。

Any CATCH block won't be hit, transactions will be left open and locks can still remain allocated after this, even if the connection is closed because "close" means "return to connection pool".任何 CATCH 块都不会被命中,事务将保持打开状态,并且在此之后仍然可以分配锁,即使连接已关闭,因为“关闭”意味着“返回连接池”。

If you expect a lot of Command Timeouts then consider using SET XACT_ABORT ON ( and this too ) that will release locks and rollback transactions.如果您预计会有很多命令超时,那么请考虑使用SET XACT_ABORT ON以及 this )这释放锁定和回滚事务。 or fix the code...或修复代码...

Before executing a query, SQL Server estimates how much memory it needs to run and tries to reserve this amount of memory from the buffer pool.在执行查询之前,SQL 服务器估计它需要运行多少 memory 并尝试从缓冲池中保留此数量的 memory。 If the reservation succeeds the query is executed immediately.如果预订成功,则立即执行查询。 If there is not enough memory readily available from the buffer pool, then the query is put into a queue with a timeout value, where the timeout value is guided by the query cost.如果没有足够的 memory 从缓冲池中随时可用,则将查询放入具有超时值的队列中,其中超时值由查询成本指导。 The basic rule is: higher the estimated cost is, larger the time out value is.基本规则是:估计成本越高,超时值越大。 When the waiting time of this query exceeds the timeout value, a time out error is thrown and the query is removed from the queue.当此查询的等待时间超过超时值时,将抛出超时错误并将查询从队列中删除。

Source 资源

If you get a SQL timeout then SQL has stopped, however web applications can time out and the SQL query can continue. If you get a SQL timeout then SQL has stopped, however web applications can time out and the SQL query can continue.

Usually when it times out it means the connection has died, meaning the query has not been sent to the database, most database support Transactions where you can start a transaction, run your queries, and if your happy you can commit them.通常,超时意味着连接已断开,这意味着查询尚未发送到数据库,大多数数据库都支持事务,您可以在其中启动事务,运行查询,如果您满意,您可以提交它们。

Example:例子:

BEGIN TRAN

UPDATE  authors
   SET  au_fname = 'John'
WHERE   au_id = '172-32-1176'

UPDATE  authors
   SET  au_fname = 'Marg'
WHERE   au_id = '213-46-8915'

COMMIT TRAN

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

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