2014-12-23 104 views
0

在一个大的数据库,是它更快地执行查询如查询速度

select * from table where a = 1 and b = 2 or b = 3 or b = 4; 

select * from table where a = 1 and b = 2; 
select * from table where a = 1 and b = 3; 
select * from table where a = 1 and b = 4; 
+0

我认为它取决于如果'a'是关键或索引或不... –

+0

只是一个挑剔的笔记,第一个查询将返回比你想象的要多得多,因为缺少括号变量b。 'select * from table where a = 1 and(b = 2 or b = 3 or b = 4)' – Tommy

回答

1

正如其他人指出的那样,查询并不等同。我想第一个查询应为:

select * from table where a = 1 and (b = 2 or b = 3 or b = 4); 

为清楚起见,我建议:

select * from table where a = 1 and b in (2, 3, 4); 

一般来说,这将执行比要求三个不同的查询作为第二选择好。

2

您应该依靠数据库管理系统做了优化你。如果第二个更快,那么DBMS无论如何都会这样做。

与第一个一起去(但是像Tommy所说的那样在圆括号b的条件下)。