0

几个 Sybase 数据库查询问题:

  1. 如果我进行连接并有 where 子句,是否会在实际连接之前应用过滤器?换句话说,它是否比没有任何 where 条件的 join 更快?

  2. 我有一个涉及 3 个表的示例(列在下面):

    A: O1,....
    B: E1,E2,...
    C: O1, E2, E2
    

所以我的加入看起来像:

select A.*, B* from B,C,A
where 
C.E1=B.E1 and C.E2=B.E2 and C.O1=A.O1 
and A.O2 in (...)
and B.E3 in (...)

如果我消除了 C 并将 O1 添加到表 B 中,我的连接会明显更快吗?

B:E1,E2,O1....
4

2 回答 2

0

First, you should use proper join syntax:

select A.*, B.*
from B join C
     on C.E1=B.E1 and C.E2=B.E2 join
     A
     on C.O1=A.O1
where B.E3 in (...)

The "," means cross join and it is prone to problems since it is easily missed. Your query becomes much different if you say "from B C, A". Also, it gives you access to outer joins, and makes the statement much more readable.

The answer to your question is "yes". Your query will run faster if there are fewer tables being joined. If you are doing a join on a primary key, and the tables fit into memory, then the join is not expensive. However, it is still faster to just find the data in the record in B.

As I say this, there could be some boundary cases in some databases where this is not necessarily true. For instance, if there is only one value in the column, and the column is a long string, then adding the column onto pages could increase the number of pages needed for B. Such extreme cases are unlikely, and you should see performance improvements.

于 2012-08-15T15:13:48.953 回答
0

速度通常取决于 SQL Server 必须读取的行数。

  1. 我认为使用 where 子句或 join 没有任何区别
  2. 取决于.. 消除表中有多少行

它可能取决于您添加连接或 where 子句的顺序,例如,如果 C 中只有几行,并且您首先将其添加为表或位置,它会立即减少可能的匹配数. 但是,如果 C 中有数百万行,那么您必须阅读数百万行才能找到匹配项。

现代优化器可以重新排列您的查询以提高效率,但不要依赖它。

真正可以减少读取的行数是在连接列中添加索引 - 如果您在 A.O1 和 C.O1 上有索引,那么它可以减少大量读取次数。

于 2012-08-15T15:14:48.280 回答