我应该使用哪个查询来提高性能? [英] Which Query Should i Use for Better Performance?

查看:76
本文介绍了我应该使用哪个查询来提高性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......

我正在尝试优化存储过程...在Sp中有一些4-5 If exists语句。

Hi All...
I Am Trying to Optimize the Stored Procedure... in the Sp there are some 4-5 If exists Statements.

If exists(Select * From TableName)
 --Do Something
Else
 -- Do SomeThimg



我可以替换那些如果存在

If (Select Count(*) From TableName)>0
 -- Do Something
Else
 -- Do Something



我的问题是哪个会执行得更快..?

我使用此代码由一些人编写否则检查查询


My Question is Which Will execute faster..?
I Used This Code Written by Some one Else to Check the Queries

Declare @cpu_ int
Declare @lreads_ int
Declare @eMsec_ int

Select @cpu_ = cpu_time
, @lreads_ = logical_reads
, @eMsec_ = total_elapsed_time
 From sys.dm_exec_requests
 Where session_id = @@spid

--======
-- Your Test Code Goes Here
--======

Select cpu_time-@cpu_ as CpuMs
, logical_reads- @lreads_ as LogRds
, total_elapsed_time - @eMsec_ as Elapsed
 From sys.dm_exec_requests
 Where session_id = @@spid



但我得到相同的结果(即相同的LogRds,CpuMs,Elapsed对于 IF存在如果(选择)> 0

如果我使用 SET STATISTICS IO ON

我得到输出


But I get the Same Results(i.e. Same LogRds,CpuMs,Elapsed For both IF exists and If (Select)>0
If I Use SET STATISTICS IO ON
I get the Output

Table 'TableName'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'TableName'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.



可以任何人建议我应该使用哪一个..?



谢谢

Raj ..


Can Any one Suggest which one Should i Use..?

Thanks
Raj..

推荐答案

试试这个:



Try this:

If exists(Select 1 From TableName)

If exists(Select TOP(1) 1 From TableName)


这篇关于我应该使用哪个查询来提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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