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.D
T2.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
但是,我有大量的数据,所以它真的很慢(几个小时)。有没有更快的算法呢?
谢谢。