改进 SQL select 查询执行时间

Improve SQL select query execution time

我有以下 SQL 查询会引发数据库超时错误。

select distinct payment_gateway_id 
from tblPaymentGatewayLog 
where entity_id in (select entity_id from tblentityList)

tblPaymentGatewayLog 只有大约 10 条记录,但 tblentityList 可以有超过 20000 条记录,因此使查询变慢。

我尝试使用内部联接而不是 IN 查询,但仍然没有区别。有什么可能改进查询执行时间的方法吗?

select distinct payment_gateway_id 
from tblPaymentGatewayLog pgl
where exists (select 1 from tblentityList el where el.entity_id = pgl.entity_id)

ExistsIN 快并且 join 将 return 重复记录。

我的偏好是 Exists:

select distinct payment_gateway_id 
from tblPaymentGatewayLog T1
where exists (select 1 from tblentityList T2 where T2.entity_id = T1.entity_id)

我也是exists的粉丝:

select distinct gl.payment_gateway_id 
from tblPaymentGatewayLog gl
where exists (select 1 from tblentityList el where el.entity_id  = gl.entity_id);

这仍然很棘手,因为 select distinct。所以,我也推荐 (tblPaymentGatewayLog, entity_id) 上的索引。 tblentityList 太小了,我认为索引没有帮助。