0

我一直在重写几十年前的 DB z/OS 查询,共有三个表,如下所示:

顾客

+-----------+----------+---------+
|customer_id|state_code|city_code|
+-----------+----------+---------+

客户地址

+-----------+-------------+
|customer_id|facility_name|
+-----------+-------------+

贷款

+-----------+----------+---------+
|loan_code  |state_code|city_code|
+-----------+----------+---------+

customer = customer_address是一对一的,customer = loan也是一对多的。

我们曾经有两个不同的查询来按州和城市获取客户列表,他们有贷款并且“活跃于业务”(通过在 customer_address 中有记录!)如下:

SELECT CUST.STATE_CODE, CUST.CITY_CODE, CUST_ADRS.FAC_NAME 
FROM  CUSTOMER CUST, CUST_ADDRESS WHERE CUST_ADRS.ADR_ID <> 0 AND      
CUST_ADRS.CUST_ID = CUST.CUST_ID

收集上述查询的结果,并将每个州和城市从 PreparedStatement 传递给下面的查询。如果有一个 loan_id 存在,那么我们收集州、城市和设施名称。

SELECT CL.LOAN_ID FROM CUSTOMER_LOAN CL WHERE
CL.LOAN_CODE IN ('A1', 'BC') AND CL.STATE_CODE = ? AND CL.CITY_CODE = ?

我已将这两个查询重写为一个查询。(customer.cust_id、customer_loan.loan_id 有索引)。我没有在修改后的查询中包含loan_id。

SELECT DISTINCT CUST.STATE_CODE, CUST.CITY_CODE, CUST_ADRS.FAC_NAME
FROM CUSTOMER CUST INNER JOIN CUST_ADDRESS CUST_ADRS ON 
CUST_ADRS.ADR_ID <> 0 AND CUST.CUST_ID = CUST_ADRS.CUST_ID
INNER JOIN CUSTOMER_LOAN CL
ON 
CL.LOAN_CODE IN ('A1', 'BC') and
CL.STATE_CODE = CUST.STATE_CODE and
CL.CITY_CODE = CUST.CITY_CODE

现在我可以看到 Web 应用程序的性能显着提高,查询执行时间大约需要 700 毫秒。但我想知道我是否可以做些什么来改进或修改这个查询。非常感谢任何输入或想法。

4

1 回答 1

1

一种可能更快的选择是使用EXISTS

select c.state_code, c.city_code, ca.fac_name
from customer c 
    join customer_address ca on c.customer_id = ca.customer_id 
where exists (
    select 1
    from loan l
    where l.state_code = c.state_code and
          l.city_code = c.city_code and
          l.loan_code in ('A1','BC')
    )

不过,与往常一样,您需要自己测试每个查询,看看哪个执行得最好。

于 2014-12-01T04:02:43.107 回答