如果部分子集排除所有结果的最佳方法?

时间:2016-06-06 18:37:26

标签: mysql

我正在试图弄清楚如何找出在某些没有某些应用程序的部门中查询一组工作站的最佳方法。工作站甚至不能有一个问题应用程序来制作列表。如果它有一个问题应用程序我需要完全省略它。

对于数据集,问题应用程序将是111,555和888,因此只应选择mach 2。应省略Mach 1和3。查询必须使用IN或能够查询许多应用程序以省略。

查询此内容的最快方法是什么?

Mach_name    App_id
mach 1        111
mach 1        222
mach 1        333
mach 2        333
mach 2        222
mach 3        111
mach 3        333


select distinct mach_name from tablename
where app_id not IN('111', '555', '888')

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

可能最快(但数据和索引的数量很重要) 如果app_ID被编入索引并且A.Mach_name被编入索引,我无法看到如何对其进行大大改进。因为一旦找到一个匹配就会短路。

Select A.mach_name 
from tableName A
where not exists (Select 1 from tableName B where A.Mach_name = B.Mach_name
and B.App_ID in ('111','555','888')
Group By A.Mach_name)

可能下一次最快必须每次产生完全匹配;但仍然比注意快。

Select A.mach_name 
from tableName A
LEFT JOIn tableName B 
 on A.Mach_name = b.Mach_name
and B.App_ID in ('111','555','888')
where B.Mach_name is null
Group by A.Mach_name
相关问题