Optimization of greater than operator

ぐ巨炮叔叔 提交于 2020-01-11 11:22:28

问题


credit table and validtransaction table have million record data from year 2008 onwards.

We are doing a migration. So I need to find out the credittypeids which are no longer in use after 2017 (periodseq=1055), so that these need not be migrated.

This is the query and the >= part is resulting in huge cost. Please suggest an alternative.

SELECT CREDITTYPEID
FROM CREDITTYPE ct
WHERE NOT EXISTS
  (SELECT 1
  FROM CREDIT C
  WHERE C.PERIODSEQ>=1055
  AND C.CREDITTYPEID=CT.CREDITTYPEID
  ); 


回答1:


The following index on the CREDIT table should help:

CREATE INDEX idx ON CREDIT (PERIODSEQ, CREDITTYPEID);

This should allow the EXISTS clause lookup to evaluate more quickly. You could also try the following variant index, which reverses the order of the columns:

CREATE INDEX idx ON CREDIT (CREDITTYPEID, PERIODSEQ);



回答2:


I'm thinking aggregation:

SELECT C.CREDITTYPEID
FROM CREDIT C
GROUP BY C.CREDITTYPEID
HAVING MAX(C.PERIODSEQ) < 1055;

This assumes that the credit type is used in at least one credit record.

Otherwise, for your version of the query, you specifically want an index on CREDIT(CREDITTYPEID, PERIODSEQ). The ordering of the keys matters, and this is the correct order for your query.




回答3:


This should return your (distinct!) list of CREDITTYPEID that were used in the past, but are not used curretnly (after PERIODSEQ 1055)

SELECT CREDITTYPEID  /* used before 1055 */
FROM CREDITTYPE ct
WHERE PERIODSEQ < 1055
MINUS
SELECT CREDITTYPEID /* used after 1055 */
FROM CREDITTYPE ct
WHERE PERIODSEQ>=1055;

As the column name suggest CREDITTYPEID is a type so there are several rows in the table with the same typeId.

The query above return only the distinct list and uses no hash anti join.

You may add parallel option (with the PARALLEL hint) if your HW allows it.



来源:https://stackoverflow.com/questions/59680369/optimization-of-greater-than-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!