Skip to content

Commit

Permalink
Use 2 instead of 1 nested query to lookup the historic entity links w…
Browse files Browse the repository at this point in the history
…ith same root scope by scopeIds

The reason for using 2 queries is due to the fact that some DBs are going to do a full table scan if we nest the queries into a single query
  • Loading branch information
filiphr committed Apr 24, 2023
1 parent d002b0b commit 24fff39
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.flowable.entitylink.service.impl.persistence.entity.data.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -76,12 +77,21 @@ public List<HistoricEntityLink> findHistoricEntityLinksWithSameRootScopeForScope
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<HistoricEntityLink> findHistoricEntityLinksWithSameRootScopeForScopeIdsAndScopeType(Collection<String> scopeIds, String scopeType, String linkType) {
// We are using 2 queries here (first find all the root scope ids and then find all the entity links for those root scope ids)
// The reason for using 2 queries is due to the fact that some DBs are going to do a full table scan if we nest the queries into a single query
Map<String, Object> parameters = new HashMap<>();
parameters.put("scopeIds", createSafeInValuesList(scopeIds));
parameters.put("scopeType", scopeType);
parameters.put("linkType", linkType);

return (List) getList("selectHistoricEntityLinksWithSameRootScopeByScopeIdsAndType", parameters);
List rootScopeIds = getDbSqlSession().selectList("selectRootScopeIdsByScopeIdsAndType", parameters);
if (rootScopeIds.isEmpty()) {
return new ArrayList<>();
}

parameters.put("rootScopeIds", createSafeInValuesList(rootScopeIds));
parameters.remove("scopeIds");

return (List) getList("selectHistoricEntityLinksByRootScopeIdsAndType", parameters);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,36 @@
where LINK_TYPE_ = #{parameter.linkType}
</select>

<select id="selectHistoricEntityLinksWithSameRootScopeByScopeIdsAndType" resultMap="historicEntityLinkResultMap">
<select id="selectHistoricEntityLinksByRootScopeIdsAndType" resultMap="historicEntityLinkResultMap">
select RES.*
from ${prefix}ACT_HI_ENTITYLINK RES where ROOT_SCOPE_ID_ in (
select ROOT_SCOPE_ID_ from ${prefix}ACT_HI_ENTITYLINK
where
(<foreach item="listItem" index="listIndex" collection="parameter.scopeIds">
<if test="listIndex &gt; 0">
or
</if>
SCOPE_ID_ in
<foreach item="item" index="index" collection="listItem" open="(" separator="," close=")">
#{item}
</foreach>
</foreach>)
and SCOPE_TYPE_ = #{parameter.scopeType} and LINK_TYPE_ = #{parameter.linkType} and ROOT_SCOPE_ID_ is not null
)
from ${prefix}ACT_HI_ENTITYLINK RES where (
<foreach item="listItem" index="listIndex" collection="parameter.rootScopeIds">
<if test="listIndex &gt; 0">
or
</if>
ROOT_SCOPE_ID_ in
<foreach item="item" index="index" collection="listItem" open="(" separator="," close=")">
#{item}
</foreach>
</foreach>
)
and LINK_TYPE_ = #{parameter.linkType}
</select>

<select id="selectRootScopeIdsByScopeIdsAndType" resultType="string">
select distinct ROOT_SCOPE_ID_
from ${prefix}ACT_HI_ENTITYLINK
where
(<foreach item="listItem" index="listIndex" collection="parameter.scopeIds">
<if test="listIndex &gt; 0">
or
</if>
SCOPE_ID_ in
<foreach item="item" index="index" collection="listItem" open="(" separator="," close=")">
#{item}
</foreach>
</foreach>)
and SCOPE_TYPE_ = #{parameter.scopeType} and LINK_TYPE_ = #{parameter.linkType} and ROOT_SCOPE_ID_ is not null
</select>

</mapper>

0 comments on commit 24fff39

Please sign in to comment.