测试表行是否存在的有效方法 [英] an efficient way to test if a table row exists
问题描述
我正在尝试找到最有效的方法来确定表行是否存在.
I'm trying to find the most efficient way to determine if a table row exists.
我有3个选择:
-
SELECT EXISTS(在某些条件下从table1中选择SELECT 1);
SELECT EXISTS(SELECT 1 FROM table1 WHERE some_condition);
从表1中选择1,其中某些条件限制为0,1;
SELECT 1 FROM table1 WHERE some_condition LIMIT 0,1;
从table1中选择COUNT(1)个条件;
SELECT COUNT(1) FROM table1 WHERE some_condition;
对于MySQL来说,第一种方法似乎更有效: 测试行是否存在的最佳方法在MySQL表中
It seems that for MySQL the first approach is more efficient: Best way to test if a row exists in a MySQL table
任何数据库都一般吗?
Is it true in general for any database?
更新:
我添加了第三个选项.
UPDATE2:
我们假设数据库产品是mysql,oracle和sql-server.
Let's assume the database products are mysql, oracle and sql-server.
推荐答案
我会做
SELECT COUNT(1) FROM table 1 WHERE some_condition.
但是我认为除非您经常调用它,否则它不会带来重大的改变(在这种情况下,我可能会使用其他策略).
But I don't think it makes a significant difference unless you call it a lot (in which case, I'd probably use a different strategy).
这篇关于测试表行是否存在的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!