-2

SQL 以最有效的方式从连接多个列的两个表中获取结果:

T1:

+---+-----+---+----+
| A | B   | C | D  |
+---+-----+---+----+
| 1 | 111 | W | 11 |
+---+-----+---+----+
| 2 | 222 | X | 22 |
+---+-----+---+----+
| 3 | 333 | Y | 33 |
+---+-----+---+----+
| 4 | 444 | Z | 44 |
+---+-----+---+----+

T2:

+---+-----+---+----+
| A | B   | C | D  |
+---+-----+---+----+
| 1 | 112 | W | 11 |
+---+-----+---+----+
| 2 | 222 | X | 99 |
+---+-----+---+----+
| 3 | 333 | Y | 88 |
+---+-----+---+----+
| 4 | 444 | W | 44 |
+---+-----+---+----+

我想匹配三列:T1.A = T2.A, T1.B = T2.B, , 并得到和T1.C = T2.C的最高勾股定理结果T1.DT2.D

所以对于 T1 和 T2,只有这些行匹配:

 T1                    T2                
+---+-----+---+----+  +---+-----+---+----+
| A | B   | C | D  |  | A | B   | C | D  |
+---+-----+---+----+  +---+-----+---+----+
| 2 | 222 | X | 22 |  | 2 | 222 | X | 99 |
+---+-----+---+----+  +---+-----+---+----+
| 3 | 333 | Y | 33 |  | 3 | 333 | Y | 88 |
+---+-----+---+----+  +---+-----+---+----+

但是

所以唯一应该作为结果返回的行是:

+---+-----+---+------+------+--------+
| A | B   | C | T1.D | T2.D | E      |
+---+-----+---+------+------+--------+
| 2 | 222 | X | 22   | 99   | 101.41 |
+---+-----+---+------+------+--------+

我有这样的事情:

select top 1 t1.A, t1.B, t1.C, t1.D, t2.D, sqrt(square(case t1.D when null then 0 else t1.D end) + square(case t2.D when null then 0 else t2.D end)) as E
from t1
left join t2 on t1.A=t2.A and t1.B=t2.B and t1.C=t2.C
order by E desc

但是,我有大量的数据,所以它真的很慢(几个小时)。有没有更快的算法呢?

谢谢。

4

1 回答 1

0

=== 删除,因为 OP 没有添加索引的权限 ===

继续前进,我会确保您在表格中有适当的索引。添加这两个,让引擎选择最好的一个:

create index ix1 t1 (a, b, c, d);

create index ix2 t2 (a, b, c, d);

引擎应该对一个表执行“完全扫描”,对另一个表执行“索引扫描/搜索”。

引擎应根据表的基数、谓词的选择性(表统计信息/直方图)选择其中一个索引。确保表格统计信息是最新的。

于 2019-12-10T17:06:57.893 回答