New tag indexing (#3570)

* New tag indexing

* Migration for tag indexing
This commit is contained in:
michaelabuckley 2022-04-30 16:51:59 -04:00 committed by GitHub
parent b59f2d05a7
commit 5aef3cec9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 5 deletions

View File

@ -0,0 +1,4 @@
---
type: change
issue: 3570
title: "The tag indexes have been changed to provide faster tag searches."

View File

@ -41,7 +41,9 @@ import ca.uhn.fhir.jpa.model.entity.SearchParamPresentEntity;
import ca.uhn.fhir.util.VersionEnum;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@ -328,6 +330,53 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
// we will drop the updated column. Start with the index.
tokenTable.dropIndexOnline("20220428.5", "IDX_SP_STRING_UPDATED");
}
// Update tag indexing
{
Builder.BuilderWithTableName resTagTable = version.onTable("HFJ_RES_TAG");
// add res_id, and partition_id so queries are covered without row-reads.
resTagTable
.addIndex("20220429.1", "IDX_RES_TAG_RES_TAG")
.unique(false)
.online(true)
.withColumns("RES_ID", "TAG_ID", "PARTITION_ID");
resTagTable
.addIndex("20220429.2", "IDX_RES_TAG_TAG_RES")
.unique(false)
.online(true)
.withColumns("RES_ID", "TAG_ID", "PARTITION_ID");
resTagTable.dropIndexOnline("20220429.4", "IDX_RESTAG_TAGID");
// Weird that we don't have addConstraint. No time to do it today.
Map<DriverTypeEnum, String> addResTagConstraint = new HashMap<>();
addResTagConstraint.put(DriverTypeEnum.H2_EMBEDDED, "ALTER TABLE HFJ_RES_TAG ADD CONSTRAINT IDX_RESTAG_TAGID UNIQUE (RES_ID, TAG_ID)");
addResTagConstraint.put(DriverTypeEnum.MARIADB_10_1, "ALTER TABLE HFJ_RES_TAG ADD CONSTRAINT IDX_RESTAG_TAGID UNIQUE (RES_ID, TAG_ID)");
addResTagConstraint.put(DriverTypeEnum.MSSQL_2012, "ALTER TABLE HFJ_RES_TAG ADD CONSTRAINT IDX_RESTAG_TAGID UNIQUE (RES_ID, TAG_ID)");
addResTagConstraint.put(DriverTypeEnum.MYSQL_5_7, "ALTER TABLE HFJ_RES_TAG ADD CONSTRAINT IDX_RESTAG_TAGID UNIQUE (RES_ID, TAG_ID)");
addResTagConstraint.put(DriverTypeEnum.ORACLE_12C, "ALTER TABLE HFJ_RES_TAG ADD CONSTRAINT IDX_RESTAG_TAGID UNIQUE (RES_ID, TAG_ID)");
addResTagConstraint.put(DriverTypeEnum.POSTGRES_9_4, "ALTER TABLE HFJ_RES_TAG ADD CONSTRAINT IDX_RESTAG_TAGID UNIQUE (RES_ID, TAG_ID)");
version.executeRawSql("20220429.5", addResTagConstraint);
Builder.BuilderWithTableName tagTable = version.onTable("HFJ_TAG_DEF");
tagTable
.addIndex("20220429.6", "IDX_TAG_DEF_TP_CD_SYS")
.unique(false)
.online(false)
.withColumns("TAG_TYPE", "TAG_CODE", "TAG_SYSTEM", "TAG_ID");
// move constraint to new index
// note the constraint has fewer columns than the index. But these engines can enforce a constraint narrower than the index.
tagTable.dropIndexOnline("20220429.8", "IDX_TAGDEF_TYPESYSCODE");
Map<DriverTypeEnum, String> addTagDefConstraint = new HashMap<>();
addTagDefConstraint.put(DriverTypeEnum.H2_EMBEDDED, "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)");
addTagDefConstraint.put(DriverTypeEnum.MARIADB_10_1, "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)");
addTagDefConstraint.put(DriverTypeEnum.MSSQL_2012, "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)");
addTagDefConstraint.put(DriverTypeEnum.MYSQL_5_7, "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)");
addTagDefConstraint.put(DriverTypeEnum.ORACLE_12C, "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)");
addTagDefConstraint.put(DriverTypeEnum.POSTGRES_9_4, "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)");
version.executeRawSql("20220429.9", addTagDefConstraint);
}
}
/**

View File

@ -32,6 +32,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.ManyToOne;
import javax.persistence.SequenceGenerator;
@ -39,8 +40,13 @@ import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "HFJ_RES_TAG", uniqueConstraints = {
@UniqueConstraint(name = "IDX_RESTAG_TAGID", columnNames = {"RES_ID", "TAG_ID"})
@Table(
name = "HFJ_RES_TAG",
indexes = {
@Index(name = "IDX_RES_TAG_RES_TAG", columnList = "RES_ID, TAG_ID, PARTITION_ID"),
@Index(name = "IDX_RES_TAG_TAG_RES", columnList = "TAG_ID, RES_ID, PARTITION_ID")
},
uniqueConstraints = { @UniqueConstraint(name = "IDX_RESTAG_TAGID", columnNames = {"RES_ID", "TAG_ID"})
})
public class ResourceTag extends BaseTag {

View File

@ -33,6 +33,7 @@ import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@ -42,9 +43,15 @@ import java.io.Serializable;
import java.util.Collection;
@Entity
@Table(name = "HFJ_TAG_DEF", uniqueConstraints = {
@Table(
name = "HFJ_TAG_DEF",
indexes = {
@Index(name = "IDX_TAG_DEF_TP_CD_SYS", columnList = "TAG_TYPE, TAG_CODE, TAG_SYSTEM, TAG_ID"),
},
uniqueConstraints = {
@UniqueConstraint(name = "IDX_TAGDEF_TYPESYSCODE", columnNames = {"TAG_TYPE", "TAG_SYSTEM", "TAG_CODE"})
})
}
)
public class TagDefinition implements Serializable {
private static final long serialVersionUID = 1L;