返回查询的第一结果而不搜索所有数据库以节省时间? [英] return first result of query without search all db for save time?

查看:112
本文介绍了返回查询的第一结果而不搜索所有数据库以节省时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
查询是:

hi all
a query is:

if exists (select TOP 1 fname from names where fname like 'Sadra')
return 1
return 0


1:正确吗?
2:我不希望引擎搜索所有数据库,只是希望调查表是否具有这样的实体以节省时间?
谢谢所有人


1 : is that correct?
2 : i don''t want the engine to search all of database and just it investigate if table has an entity like that or no for save time?
thanks all

推荐答案

实际上,这可能会让您感到惊讶:

Actually, it may surprise you:

if exists (select TOP 1 fname from names where fname like 'Sadra')
return 1
return 0
-- is less efficient than:
if exists (select * from names where fname like 'Sadra')
return 1
return 0



这确实取决于您使用的SQL Server的版本.如果您使用的是2005之前的版本,请不要^ _ ^

第一个版本实际上要求查询执行某项操作.它应该自动忽略它,但我会担心.

通过编译器可以更轻松地优化第二个版本.它不会搜索整个表,因为它知道满足"EXISTS"运算符所需的最低要求.

另外,您的赞"是错误的.您需要包含通配符(%")或效率较低的''=''

希望对您有所帮助^ _ ^
安迪



This does depend on the version of SQL Server you''re using. If you''re using pre 2005 then, well just don''t ^_^

The first version actually asks the query to do something. It should automatically ignore that, but I would worry.

The second version can be optimised by the compiler easier. It won''t search the whole table because it know the minimum required to fulfill the "EXISTS" operator.

also, your "LIKE" is wrong. you need to include wildcards (''%'') or it is just a less efficient ''=''

Hope that helps ^_^
Andy


这篇关于返回查询的第一结果而不搜索所有数据库以节省时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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