我遇到了一个查询的问题,该查询过去工作得很好,但现在它太慢了,所以它基本上永远不会完成(可能在几个小时内,超出任何超时)。
查询就像
select ky.issuer_id from T_MASTER_ISSUER_KEYS ky
inner join T_ISSUER_SECURITY_LIST lst on lst.ISSUER_ID_SIX = ky.ISSUER_ID_SIX AND lst.ISSUER_ID_BBG = ky.ISSUER_ID_BBG
WHERE ky.ISSUER_ID_RDE is not null
实时执行仍然停留在哈希匹配连接步骤。
但是,如果我强制使用嵌套循环连接
select ky.issuer_id from T_MASTER_ISSUER_KEYS ky
inner loop join T_ISSUER_SECURITY_LIST lst on lst.ISSUER_ID_SIX = ky.ISSUER_ID_SIX AND lst.ISSUER_ID_BBG = ky.ISSUER_ID_BBG
WHERE ky.ISSUER_ID_RDE is not null
我已经尝试创建一些覆盖索引来包含所有连接和选择属性,但没有任何帮助。对我来说,100k 和 300k 行的哈希连接对于服务器来说应该是小菜一碟。
此外,如果我删除两个连接条件中的任何一个,查询就可以正常工作:
select ky.issuer_id from T_MASTER_ISSUER_KEYS ky
inner join T_ISSUER_SECURITY_LIST lst on /*lst.ISSUER_ID_SIX = ky.ISSUER_ID_SIX AND*/ lst.ISSUER_ID_BBG = ky.ISSUER_ID_BBG
WHERE ky.ISSUER_ID_RDE is not null
连接列是VARCHAR(20),对连接有影响吗?
重写为:
select ky.issuer_id
from T_MASTER_ISSUER_KEYS AS ky
WHERE EXISTS(SELECT 1/0
FROM T_ISSUER_SECURITY_LIST AS lst
WHERE lst.ISSUER_ID_SIX = ky.ISSUER_ID_SIX AND
lst.ISSUER_ID_BBG = ky.ISSUER_ID_BBG AND
ky.ISSUER_ID_RDE is not null);
如果不存在则创建这些索引:
-- index for ky :
CREATE INDEX X_Jan_Drozen_T_MASTER_ISSUER_KEYS_001
ON T_MASTER_ISSUER_KEYS (ISSUER_ID_RDE, ISSUER_ID_SIX, ISSUER_ID_BBG) INCLUDE (issuer_id);
-- index for lst :
CREATE INDEX X_Jan_Drozen_T_ISSUER_SECURITY_LIST_001
ON T_ISSUER_SECURITY_LIST (ISSUER_ID_SIX, ISSUER_ID_BBG) ;