Skip to content

Commit

Permalink
Improve indexes, close #151
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed May 31, 2024
1 parent c00a973 commit 60ab3ad
Show file tree
Hide file tree
Showing 48 changed files with 711 additions and 979 deletions.
249 changes: 107 additions & 142 deletions Tzkt.Data/Migrations/20240424125408_Initial.Designer.cs

Large diffs are not rendered by default.

370 changes: 148 additions & 222 deletions Tzkt.Data/Migrations/20240424125408_Initial.cs

Large diffs are not rendered by default.

249 changes: 107 additions & 142 deletions Tzkt.Data/Migrations/20240501115741_Triggers.Designer.cs

Large diffs are not rendered by default.

249 changes: 107 additions & 142 deletions Tzkt.Data/Migrations/20240501115809_TokensValue.Designer.cs

Large diffs are not rendered by default.

249 changes: 107 additions & 142 deletions Tzkt.Data/Migrations/TzktContextModelSnapshot.cs

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions Tzkt.Data/Models/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ public static void BuildAccountModel(this ModelBuilder modelBuilder)
#endregion

#region indexes
modelBuilder.Entity<Account>()
.HasIndex(x => x.Id)
.IsUnique();

modelBuilder.Entity<Account>()
.HasIndex(x => x.Address)
.IsUnique();
Expand Down
5 changes: 2 additions & 3 deletions Tzkt.Data/Models/Accounts/Commitment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ public static void BuildCommitmentModel(this ModelBuilder modelBuilder)

#region indexes
modelBuilder.Entity<Commitment>()
.HasIndex(x => x.Id)
.HasIndex(x => x.Address)
.IsUnique();

modelBuilder.Entity<Commitment>()
.HasIndex(x => x.Address)
.IsUnique();
.HasIndex(x => x.AccountId);
#endregion
}
}
Expand Down
8 changes: 6 additions & 2 deletions Tzkt.Data/Models/Accounts/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ public static void BuildContractModel(this ModelBuilder modelBuilder)
{
#region indexes
modelBuilder.Entity<Contract>()
.HasIndex(x => new { x.Type, x.Kind })
.HasFilter(@"""Type"" = 2");
.HasIndex(x => x.Kind, $"IX_{nameof(TzktContext.Accounts)}_{nameof(Contract.Kind)}_Partial")
.HasFilter($@"""{nameof(Account.Type)}"" = {(int)AccountType.Contract}");

modelBuilder.Entity<Contract>()
.HasIndex(x => x.CreatorId);

modelBuilder.Entity<Contract>()
.HasIndex(x => x.ManagerId);

modelBuilder.Entity<Contract>()
.HasIndex(x => x.WeirdDelegateId)
.HasFilter($@"""{nameof(Contract.WeirdDelegateId)}"" IS NOT NULL");

modelBuilder.Entity<Contract>()
.HasIndex(x => x.TypeHash);

Expand Down
8 changes: 6 additions & 2 deletions Tzkt.Data/Models/Accounts/Delegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ public static void BuildDelegateModel(this ModelBuilder modelBuilder)
{
#region indexes
modelBuilder.Entity<Delegate>()
.HasIndex(x => new { x.Type, x.Staked })
.HasFilter(@"""Type"" = 1");
.HasIndex(x => x.Staked, $"IX_{nameof(TzktContext.Accounts)}_{nameof(Delegate.Staked)}_Partial")
.HasFilter($@"""{nameof(Account.Type)}"" = {(int)AccountType.Delegate}");

modelBuilder.Entity<Delegate>()
.HasIndex(x => x.DeactivationLevel, $"IX_{nameof(TzktContext.Accounts)}_{nameof(Delegate.DeactivationLevel)}_Partial")
.HasFilter($@"""{nameof(Account.Type)}"" = {(int)AccountType.Delegate}");
#endregion

#region relations
Expand Down
3 changes: 0 additions & 3 deletions Tzkt.Data/Models/Baking/BakingRight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public static void BuildBakingRightModel(this ModelBuilder modelBuilder)
modelBuilder.Entity<BakingRight>()
.HasIndex(x => x.Level);

modelBuilder.Entity<BakingRight>()
.HasIndex(x => x.Cycle);

modelBuilder.Entity<BakingRight>()
.HasIndex(x => new { x.Cycle, x.BakerId });
#endregion
Expand Down
3 changes: 0 additions & 3 deletions Tzkt.Data/Models/Baking/Cycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public static void BuildCycleModel(this ModelBuilder modelBuilder)
#region keys
modelBuilder.Entity<Cycle>()
.HasKey(x => x.Id);

modelBuilder.Entity<Cycle>()
.HasAlternateKey(x => x.Index);
#endregion

#region props
Expand Down
5 changes: 1 addition & 4 deletions Tzkt.Data/Models/Baking/SnapshotBalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public static void BuildSnapshotBalanceModel(this ModelBuilder modelBuilder)

#region indexes
modelBuilder.Entity<SnapshotBalance>()
.HasIndex(x => x.Level);

modelBuilder.Entity<SnapshotBalance>()
.HasIndex(x => x.Level, "IX_SnapshotBalance_Level_Partial")
.HasIndex(x => x.Level, $"IX_{nameof(TzktContext.SnapshotBalances)}_{nameof(SnapshotBalance.Level)}_Partial")
.HasFilter(@"""AccountId"" = ""BakerId""");

modelBuilder.Entity<SnapshotBalance>()
Expand Down
14 changes: 7 additions & 7 deletions Tzkt.Data/Models/Baking/StakingUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void BuildStakingUpdateModel(this ModelBuilder modelBuilder)

#region indexes
modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => x.Level);
.HasIndex(x => new { x.Level, x.Id });

modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => new { x.BakerId, x.Cycle, x.Id });
Expand All @@ -55,27 +55,27 @@ public static void BuildStakingUpdateModel(this ModelBuilder modelBuilder)

modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => x.AutostakingOpId)
.HasFilter($@"""{nameof(StakingUpdate.AutostakingOpId)}"" is not null");
.HasFilter($@"""{nameof(StakingUpdate.AutostakingOpId)}"" IS NOT NULL");

modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => x.StakingOpId)
.HasFilter($@"""{nameof(StakingUpdate.StakingOpId)}"" is not null");
.HasFilter($@"""{nameof(StakingUpdate.StakingOpId)}"" IS NOT NULL");

modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => x.DelegationOpId)
.HasFilter($@"""{nameof(StakingUpdate.DelegationOpId)}"" is not null");
.HasFilter($@"""{nameof(StakingUpdate.DelegationOpId)}"" IS NOT NULL");

modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => x.DoubleBakingOpId)
.HasFilter($@"""{nameof(StakingUpdate.DoubleBakingOpId)}"" is not null");
.HasFilter($@"""{nameof(StakingUpdate.DoubleBakingOpId)}"" IS NOT NULL");

modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => x.DoubleEndorsingOpId)
.HasFilter($@"""{nameof(StakingUpdate.DoubleEndorsingOpId)}"" is not null");
.HasFilter($@"""{nameof(StakingUpdate.DoubleEndorsingOpId)}"" IS NOT NULL");

modelBuilder.Entity<StakingUpdate>()
.HasIndex(x => x.DoublePreendorsingOpId)
.HasFilter($@"""{nameof(StakingUpdate.DoublePreendorsingOpId)}"" is not null");
.HasFilter($@"""{nameof(StakingUpdate.DoublePreendorsingOpId)}"" IS NOT NULL");
#endregion
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tzkt.Data/Models/Baking/UnstakeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class UnstakeRequestModel
public static void BuildUnstakeRequestModel(this ModelBuilder modelBuilder)
{
#region keys
modelBuilder.Entity<StakingUpdate>()
modelBuilder.Entity<UnstakeRequest>()
.HasKey(x => x.Id);
#endregion

Expand Down
3 changes: 0 additions & 3 deletions Tzkt.Data/Models/Blocks/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ public static void BuildBlockModel(this ModelBuilder modelBuilder)
#region keys
modelBuilder.Entity<Block>()
.HasKey(x => x.Id);

modelBuilder.Entity<Block>()
.HasAlternateKey(x => x.Level);
#endregion

#region props
Expand Down
13 changes: 10 additions & 3 deletions Tzkt.Data/Models/Blocks/Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ public static void BuildProtocolModel(this ModelBuilder modelBuilder)
#region keys
modelBuilder.Entity<Protocol>()
.HasKey(x => x.Id);

modelBuilder.Entity<Protocol>()
.HasAlternateKey(x => x.Code);
#endregion

#region props
Expand All @@ -150,6 +147,16 @@ public static void BuildProtocolModel(this ModelBuilder modelBuilder)
.Property<string>("Extras")
.HasColumnType("jsonb");
#endregion

#region indexes
modelBuilder.Entity<Protocol>()
.HasIndex(x => x.Code)
.IsUnique();

modelBuilder.Entity<Protocol>()
.HasIndex(x => x.Hash)
.IsUnique();
#endregion
}
}
}
2 changes: 1 addition & 1 deletion Tzkt.Data/Models/Operations/DelegationOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void BuildDelegationOperationModel(this ModelBuilder modelBuilder)
.HasIndex(x => x.OpHash);

modelBuilder.Entity<DelegationOperation>()
.HasIndex(x => x.SenderId);
.HasIndex(x => new { x.SenderId, x.Id });

modelBuilder.Entity<DelegationOperation>()
.HasIndex(x => x.SenderCodeHash)
Expand Down
3 changes: 0 additions & 3 deletions Tzkt.Data/Models/Operations/NonceRevelationOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public static void BuildNonceRevelationOperationModel(this ModelBuilder modelBui
#region keys
modelBuilder.Entity<NonceRevelationOperation>()
.HasKey(x => x.Id);

modelBuilder.Entity<NonceRevelationOperation>()
.HasAlternateKey(x => x.RevealedLevel);
#endregion

#region props
Expand Down
9 changes: 7 additions & 2 deletions Tzkt.Data/Models/Operations/RegisterConstantOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public static void BuildRegisterConstantOperationModel(this ModelBuilder modelBu
#endregion

#region props
modelBuilder.Entity<RegisterConstantOperation>()
.Property(x => x.OpHash)
.IsFixedLength(true)
.HasMaxLength(51)
.IsRequired();

modelBuilder.Entity<RegisterConstantOperation>()
.Property(x => x.Address)
.HasMaxLength(54); // expr
Expand All @@ -42,8 +48,7 @@ public static void BuildRegisterConstantOperationModel(this ModelBuilder modelBu

modelBuilder.Entity<RegisterConstantOperation>()
.HasIndex(x => x.Address)
.HasFilter($@"""{nameof(RegisterConstantOperation.Address)}"" is not null")
.IsUnique();
.HasFilter($@"""{nameof(RegisterConstantOperation.Address)}"" IS NOT NULL");
#endregion

#region relations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public static void BuildSetDelegateParametersOperationModel(this ModelBuilder mo
.HasIndex(x => x.OpHash);

modelBuilder.Entity<SetDelegateParametersOperation>()
.HasIndex(x => x.SenderId);
.HasIndex(x => new { x.SenderId, x.Id });

modelBuilder.Entity<SetDelegateParametersOperation>()
.HasIndex(x => x.ActivationCycle);
#endregion

#region relations
Expand Down
10 changes: 9 additions & 1 deletion Tzkt.Data/Models/Operations/SetDepositsLimitOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public static void BuildSetDepositsLimitOperationModel(this ModelBuilder modelBu
.HasKey(x => x.Id);
#endregion

#region props
modelBuilder.Entity<SetDepositsLimitOperation>()
.Property(x => x.OpHash)
.IsFixedLength(true)
.HasMaxLength(51)
.IsRequired();
#endregion

#region indexes
modelBuilder.Entity<SetDepositsLimitOperation>()
.HasIndex(x => x.Level);
Expand All @@ -26,7 +34,7 @@ public static void BuildSetDepositsLimitOperationModel(this ModelBuilder modelBu
.HasIndex(x => x.OpHash);

modelBuilder.Entity<SetDepositsLimitOperation>()
.HasIndex(x => x.SenderId);
.HasIndex(x => new { x.SenderId, x.Id });
#endregion

#region relations
Expand Down
8 changes: 4 additions & 4 deletions Tzkt.Data/Models/Operations/SmartRollupCementOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public static void BuildSmartRollupCementOperationModel(this ModelBuilder modelB
.HasIndex(x => x.OpHash);

modelBuilder.Entity<SmartRollupCementOperation>()
.HasIndex(x => x.SenderId);
.HasIndex(x => new { x.SmartRollupId, x.Id });

modelBuilder.Entity<SmartRollupPublishOperation>()
.HasIndex(x => x.SmartRollupId);
modelBuilder.Entity<SmartRollupCementOperation>()
.HasIndex(x => x.SenderId);

modelBuilder.Entity<SmartRollupPublishOperation>()
modelBuilder.Entity<SmartRollupCementOperation>()
.HasIndex(x => x.CommitmentId);
#endregion

Expand Down
2 changes: 1 addition & 1 deletion Tzkt.Data/Models/Operations/SmartRollupExecuteOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void BuildSmartRollupExecuteOperationModel(this ModelBuilder model
.HasIndex(x => x.SmartRollupId);

modelBuilder.Entity<SmartRollupExecuteOperation>()
.HasIndex(x => x.CommitmentId);
.HasIndex(x => new { x.CommitmentId, x.Id });
#endregion

#region relations
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Data/Models/Operations/SmartRollupPublishOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public static void BuildSmartRollupPublishOperationModel(this ModelBuilder model
.HasIndex(x => x.SenderId);

modelBuilder.Entity<SmartRollupPublishOperation>()
.HasIndex(x => x.SmartRollupId);
.HasIndex(x => x.CommitmentId);

modelBuilder.Entity<SmartRollupPublishOperation>()
.HasIndex(x => x.CommitmentId);
.HasIndex(x => new { x.SmartRollupId, x.SenderId, x.Id });

modelBuilder.Entity<SmartRollupPublishOperation>()
.HasIndex(x => new { x.SmartRollupId, x.BondStatus, x.SenderId })
Expand Down
2 changes: 1 addition & 1 deletion Tzkt.Data/Models/Operations/SmartRollupRefuteOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void BuildSmartRollupRefuteOperationModel(this ModelBuilder modelB
.HasIndex(x => x.SmartRollupId);

modelBuilder.Entity<SmartRollupRefuteOperation>()
.HasIndex(x => x.GameId);
.HasIndex(x => new { x.GameId, x.Id });
#endregion

#region relations
Expand Down
8 changes: 8 additions & 0 deletions Tzkt.Data/Models/Operations/TransactionOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public static void BuildTransactionOperationModel(this ModelBuilder modelBuilder
modelBuilder.Entity<TransactionOperation>()
.HasIndex(x => x.Level);

modelBuilder.Entity<TransactionOperation>()
.HasIndex(x => x.TargetId, $"IX_{nameof(TzktContext.TransactionOps)}_{nameof(TransactionOperation.TargetId)}_Partial")
.HasFilter($"""
"{nameof(TransactionOperation.Entrypoint)}" = 'transfer'
AND "{nameof(TransactionOperation.TokenTransfers)}" IS NULL
AND "{nameof(TransactionOperation.Status)}" = {(int)OperationStatus.Applied}
""");

modelBuilder.Entity<TransactionOperation>()
.HasIndex(x => x.OpHash);

Expand Down
2 changes: 1 addition & 1 deletion Tzkt.Data/Models/Operations/VdfRevelationOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void BuildVdfRevelationOperationModel(this ModelBuilder modelBuild
.HasIndex(x => x.BakerId);

modelBuilder.Entity<VdfRevelationOperation>()
.HasIndex(x => x.Cycle);
.HasIndex(x => new { x.Cycle, x.Id });
#endregion

#region relations
Expand Down
7 changes: 0 additions & 7 deletions Tzkt.Data/Models/Scripts/BigMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,9 @@ public static void BuildBigMapModel(this ModelBuilder modelBuilder)
#region keys
modelBuilder.Entity<BigMap>()
.HasKey(x => x.Id);

modelBuilder.Entity<BigMap>()
.HasAlternateKey(x => x.Ptr);
#endregion

#region indexes
modelBuilder.Entity<BigMap>()
.HasIndex(x => x.Id)
.IsUnique();

modelBuilder.Entity<BigMap>()
.HasIndex(x => x.Ptr)
.IsUnique();
Expand Down
10 changes: 2 additions & 8 deletions Tzkt.Data/Models/Scripts/BigMapKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void BuildBigMapKeyModel(this ModelBuilder modelBuilder)
#region props
modelBuilder.Entity<BigMapKey>()
.Property(x => x.KeyHash)
.IsFixedLength(true)
.HasMaxLength(54);

modelBuilder.Entity<BigMapKey>()
Expand All @@ -43,18 +44,11 @@ public static void BuildBigMapKeyModel(this ModelBuilder modelBuilder)
#endregion

#region indexes
modelBuilder.Entity<BigMapKey>()
.HasIndex(x => x.Id)
.IsUnique();

modelBuilder.Entity<BigMapKey>()
.HasIndex(x => x.BigMapPtr);

modelBuilder.Entity<BigMapKey>()
.HasIndex(x => x.LastLevel);

modelBuilder.Entity<BigMapKey>()
.HasIndex(x => new { x.BigMapPtr, x.Active })
.HasIndex(x => x.BigMapPtr, $"IX_{nameof(TzktContext.BigMapKeys)}_{nameof(BigMapKey.BigMapPtr)}_Partial")
.HasFilter($@"""{nameof(BigMapKey.Active)}"" = true");

modelBuilder.Entity<BigMapKey>()
Expand Down
Loading

0 comments on commit 60ab3ad

Please sign in to comment.