3

I usually use SELECT 1 as my preferred validation-query from tomcat jdbc pools, because it returns only one row with the result 1 that is very faster, but today I've found one terrible mistake:

My database only has one table with its primary key and is not nullable. This table sometimes is dropped, and then appears again by application circumstances. And that's the problem, SELECT 1 validate connection to database because it's already up but the table is missing so I get a terrible exception.

So, the solutions passes by finding one validation-query against the only table that exists in the database. And also, I need the query to be as fast as possible, because the performance in the application is one of the main objetives.

You can answer than an obvius query may be SELECT 1 FROM THE_TABLE but this query returns 1 for each row that the table has, and that's not very quick.

So, what could be the faster validation-query to this table??

EDIT

If I need to return at least one result, How should be the validation-query?
I ask this because some pool implementations, like commons-dbcp doesn't accept a query without results as a validated query.

4

1 回答 1

4

这个怎么样,它应该验证表是否存在,而无需通过拉取0行而不实际列来实际加载任何数据。

SELECT TOP 0 1 FROM THE_TABLE

演示:http ://www.sqlfiddle.com/#!3/c670b/3


还有一些内置方法可以检查 SQL SERVER 中是否存在对象。这里有两个例子可以有效地做同样的事情。

select count(1) from information_schema.tables where table_name = 'THE_TABLE'
select OBJECT_ID('THE_TABLE') is not null
于 2012-05-22T19:20:30.947 回答