作者:fly-fox | 来源:互联网 | 2023-09-25 19:54
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
但是,我有成千上万的数据,因此速度非常慢(小时)。有更快的算法吗?
谢谢。
===已删除,因为OP没有添加索引的权限===
继续前进,我将确保您在表中具有正确的索引。将两者都添加,然后让引擎选择最佳的:
create index ix1 t1 (a,b,c,d);
create index ix2 t2 (a,d);
引擎应该在一张桌子上执行“完全扫描”,而在另一张桌子上进行“索引扫描/搜索”。
引擎应根据表的基数,谓词的选择性(表统计信息/直方图)选择那些索引之一。确保表格统计信息是最新的。