简体   繁体   English

SQL Server功能效率(如果存在)

[英]SQL Server Function Efficiency If Exists

I have the following function which returns a bit: 我有以下函数返回一点:

Declare @Ret bit
SET @Ret = 0
IF EXISTS ( Select * from tblExclusion where StatusID = 1 and AccountID = @AccountID )
Begin
SET @Ret = 1
End

Return @Ret

Now there can be multiple entries for the same AccountID in the table or none at all but only one entry will ever have a "1" status if it exists. 现在,表中可以有多个条目用于同一AccountID,或者根本没有,但只有一个条目的状态为“ 1”(如果存在)。

I have to be honest I'm not very knowledgeable when it comes to SQL but when called the function seems to take a long time to return. 老实说,我在SQL方面不是很了解,但是在调用该函数时,返回该函数似乎需要很长时间。 I'm wondering if there is a more efficient way of writing the above. 我想知道是否有更有效的方式编写以上内容。

Thanks in advance. 提前致谢。

An index may be necessary, reviewing a sample execution plan will reveal what index would improve. 索引可能是必要的,查看样本执行计划将发现哪些索引会有所改进。

If you were to modify your query to: 如果要将查询修改为:

Declare @Ret bit
SET @Ret = 0
IF EXISTS ( Select 1 from tblExclusion where StatusID = 1 and AccountID = @AccountID )
Begin
SET @Ret = 1
End

Return @Ret

An NONCLUSTERED INDEX would be of the format: NONCLUSTERED INDEX的格式为:

USE [DatabaseName]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[tblExclusion] ([StatusID],[AccountID])
<optional, INCLUDE ([columns within the select,]) >
GO

Types of indexes and how to create them: Create Index 索引的类型以及如何创建它们: 创建索引

If it takes a long time to run, then I would suspect that there is no index on the column "AccountID". 如果运行时间很长,那么我会怀疑“ AccountID”列上没有索引。 Adding an index on that column will probably significantly improve performance. 在该列上添加索引可能会大大提高性能。 However, without knowing how tblExclusion is defined, there is no way to be certain of this answer. 但是,在不知道如何定义tblExclusion的情况下,无法确定该答案。 Also, adding an index to StatusID will help as well, assuming there are a large number of entries for different StatusIDs. 同样,假设为不同的StatusID提供大量条目,则向StatusID添加索引也将有所帮助。

Also, since you only need to test the existence of the record, you don't need to select every column in tblExclusion. 同样,由于只需要测试记录的存在,因此不需要选择tblExclusion中的每一列。 You could change "*" to "1" or something, though this will not improve performance significantly. 您可以将“ *”更改为“ 1”或其他内容,尽管这不会显着提高性能。

Try this form 试试这个表格

Declare @Ret bitSET @Ret = 0
IF EXISTS ( Select top 1 * from tblExclusion(nolock) where StatusID = 1 and AccountID = @AccountID )
Begin
SET @Ret = 1
End

Return @Ret

Remember the index's and maintenance can make this work slow. 请记住,索引和维护会使此工作变慢。

I suggest using select top 1 1 from instead of select * from as in: 我建议使用select top 1 1 from来代替select * from如下所示:

Declare @Ret bit
SET @Ret = 0
IF EXISTS (Select top 1 1 from tblExclusion where StatusID = 1 and AccountID = @AccountID)
SET @Ret = 1

Return @Ret

This way you avoid getting unneeded and probably large data. 这样,您可以避免获取不必要的数据以及可能的大数据。

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

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