From 85824efa512f2b23a1b1d75407d0d5b3ec1faad9 Mon Sep 17 00:00:00 2001
From: Frank Tao <38163583+frankjtao@users.noreply.github.com>
Date: Wed, 10 Mar 2021 16:58:10 -0500
Subject: [PATCH] Added index to improve the performance (#2385)
* Added index to improve the performance
* Added index for FORCED_ID
* Added comments for the index of while 'missing' is supported.
* Updated comments
* Moved the migration script to V5_4_0
---
.../ca/uhn/fhir/jpa/api/config/DaoConfig.java | 5 +++++
.../tasks/HapiFhirJpaMigrationTasks.java | 17 ++++++++++++++++-
.../ca/uhn/fhir/jpa/model/entity/ForcedId.java | 2 ++
.../entity/ResourceIndexedSearchParamDate.java | 1 +
4 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/hapi-fhir-jpaserver-api/src/main/java/ca/uhn/fhir/jpa/api/config/DaoConfig.java b/hapi-fhir-jpaserver-api/src/main/java/ca/uhn/fhir/jpa/api/config/DaoConfig.java
index d45156245b0..6b494fa7ed2 100644
--- a/hapi-fhir-jpaserver-api/src/main/java/ca/uhn/fhir/jpa/api/config/DaoConfig.java
+++ b/hapi-fhir-jpaserver-api/src/main/java/ca/uhn/fhir/jpa/api/config/DaoConfig.java
@@ -608,6 +608,11 @@ public class DaoConfig {
* _sort
parameter on searches): If the server is configured
* to not index missing field.
*
+ *
+ * The following index may need to be added into the indexed tables such as HFJ_SPIDX_TOKEN
+ * to improve the search performance while :missing
is enabled.
+ * RES_TYPE, SP_NAME, SP_MISSING
+ *
*/
public void setIndexMissingFields(IndexEnabledEnum theIndexMissingFields) {
Validate.notNull(theIndexMissingFields, "theIndexMissingFields must not be null");
diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java
index ec2e34b732c..a53a5a1f6da 100644
--- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java
+++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java
@@ -71,9 +71,23 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks {
init501(); // 20200514 - 20200515
init510(); // 20200516 - 20201028
init520(); // 20201029 -
- init530();
+ init530();
+ init540(); // 20210218 -
}
+ private void init540() {
+
+ Builder version = forVersion(VersionEnum.V5_4_0);
+
+ //-- add index on HFJ_SPIDX_DATE
+ version.onTable("HFJ_SPIDX_DATE").addIndex("20210309.1", "IDX_SP_DATE_HASH_HIGH")
+ .unique(false).withColumns("HASH_IDENTITY", "SP_VALUE_HIGH");
+
+ //-- add index on HFJ_FORCED_ID
+ version.onTable("HFJ_FORCED_ID").addIndex("20210309.2", "IDX_FORCEID_FID")
+ .unique(false).withColumns("FORCED_ID");
+ }
+
private void init530() {
Builder version = forVersion(VersionEnum.V5_3_0);
@@ -126,6 +140,7 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks {
// HFJ_RES_LINK
version.onTable("HFJ_RES_LINK")
.addColumn("20210126.1", "TARGET_RESOURCE_VERSION").nullable().type(ColumnTypeEnum.LONG);
+
}
protected void init520() {
diff --git a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ForcedId.java b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ForcedId.java
index 638b070b1ae..64d6d4d4566 100644
--- a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ForcedId.java
+++ b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ForcedId.java
@@ -31,6 +31,7 @@ import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
+import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
@@ -48,6 +49,7 @@ import javax.persistence.UniqueConstraint;
* - IDX_FORCEDID_TYPE_RESID
* so don't reuse these names
*/
+ @Index(name = "IDX_FORCEID_FID", columnList = "FORCED_ID")
})
public class ForcedId extends BasePartitionable {
diff --git a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java
index 17adca2a4d9..868f759870d 100644
--- a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java
+++ b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java
@@ -55,6 +55,7 @@ import java.util.Date;
// We previously had an index called IDX_SP_DATE - Dont reuse
@Index(name = "IDX_SP_DATE_HASH", columnList = "HASH_IDENTITY,SP_VALUE_LOW,SP_VALUE_HIGH"),
@Index(name = "IDX_SP_DATE_HASH_LOW", columnList = "HASH_IDENTITY,SP_VALUE_LOW"),
+ @Index(name = "IDX_SP_DATE_HASH_HIGH", columnList = "HASH_IDENTITY,SP_VALUE_HIGH"),
@Index(name = "IDX_SP_DATE_ORD_HASH", columnList = "HASH_IDENTITY,SP_VALUE_LOW_DATE_ORDINAL,SP_VALUE_HIGH_DATE_ORDINAL"),
@Index(name = "IDX_SP_DATE_ORD_HASH_LOW", columnList = "HASH_IDENTITY,SP_VALUE_LOW_DATE_ORDINAL"),
@Index(name = "IDX_SP_DATE_RESID", columnList = "RES_ID"),