问题描述
确定一个项目是否存在的最佳性能是什么,特别是如果表包含超过 700,000 行
What's the best in performance to determined if an item exist or not specially if the table contain more than 700,000 row
if (Select count(id) from Registeration where email='[email protected]') > 0
print 'Exist'
else
print 'Not Exist'
或
if Exists(Select id from Registeration where email='[email protected]')
print 'Exist'
else
print 'Not Exist'
推荐答案
EXISTS, always
EXISTS, always
- COUNT 将遍历表或索引:您要求 COUNT
- EXISTS 会在找到一行后立即停止
编辑,要清楚
当然,在这种情况下,如果 email 列是唯一的并且已编入索引,它将关闭.
Of course, in this case if the email column is unique and indexed it will be close.
一般来说,EXISTS 会使用更少的资源,也更正确.您正在寻找一行的存在,即使它们相同,也不会超过零"
Generally, EXISTS will use less resources and is more correct too. You are looking for existence of a row, not "more than zero" even if they are the same
Edit2:在 EXISTS 中,你可以使用 NULL、1、ID,甚至 1/0:它没有被检查......
In the EXISTS, you can use NULL, 1, ID, or even 1/0: it isn't checked...
2011 年 5 月 21 日
21 May 2011 edit:
看起来这在 SQL Server 2005+ 中进行了优化,因此在这种情况下 COUNT 现在与 EXISTS 相同
It looks like this was optimised in SQL Server 2005+ so COUNT is now the same as EXISTS in this case
这篇关于检查项目是否存在的最佳方法是:选择 Count(ID)OR Exist(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!