为 SQLITE 指定 SELECT 超时 [英] Specify SELECT timeout for SQLITE

查看:65
本文介绍了为 SQLITE 指定 SELECT 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以指定 SELECT 查询在 SQLITE 中可能花费的最长时间?

Is it possible to specify the maximum amount of time a SELECT query may take with SQLITE?

当您有大表并且用户必须有可能输入免费搜索词时,这种情况会很有用.如果没有快速找到搜索词,则会扫描整个表,这可能需要很长时间,因为通常无法使用索引.

The situation would be useful where you have big tables and the users have to possibility to enter free search terms. If the searched for terms is not quickly found then the entire table is scanned which can take a very long time as indices cannot generally be used.

所以让 SQLITE 在几秒钟后放弃会很有用.

So having SQLITE give up after a few seconds would be useful.

我通过 System.Data.Sqlite 使用 SQLITE,似乎 SqliteCommand.CommandTimeout 将是我想要的,但由于某种原因,设置它似乎没有效果.也许我错过了什么.

I am using SQLITE via the System.Data.Sqlite and it seemed that SqliteCommand.CommandTimeout would be what I want, but setting this seem to have no effect for some reason. Perhaps I'm missing something.

推荐答案

对于简单的选择查询,不,似乎没有设置超时或最大时间的方法在 SQLite 本身上执行.文档中唯一提到的超时是忙超时.因此,如果您需要限制选择查询可以占用的最长时间,则需要在应用程序级别用超时包装您的连接,并在超过该超时时取消/关闭您的连接.如何做到这一点显然取决于应用程序/语言.

For a simple select query, no, there doesn't appear to be a way to set a timeout, or maximum time to execute, on SQLite itself. The only mention of timeout in the documentation is the busy timeout. So, if you need to limit the maximum amount of time a select query can take, you'll need to wrap your connection with a timeout in the application level, and cancel/close your connection if that timeout is exceeded. How to do that would obviously be application/language specific.

对于忙超时(连接停止等待锁定清除之前的超时)您可以通过 提供的 C 接口,或通过为您的应用程序提供的 SQLite 驱动程序.

For a busy timeout (the timeout before the connection stops waiting for locks to clear) you can do it through the provided C interface, or through the SQLite driver provided to your application.

这篇关于为 SQLITE 指定 SELECT 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆