Skip to content

Commit a90c71c

Browse files
author
Timothy Dodd
committed
Update dependencies and enhance database indexing
Updated multiple package dependencies across projects, including `Microsoft.Extensions`, `System.Text.Json`, `MySql.Data`, and testing libraries. Enhanced `DatabaseInitializer` with logging support and added the `EnsureLogIndexes` method to create a composite index (`Deployment_Pod_TimeStamp_idx`) on the `Log` table, improving query performance. Modified `LogRepo` queries to use `USE INDEX` for better database optimizer flexibility. Marked `RoboDodd.OrmLite` subproject as dirty, indicating uncommitted changes.
1 parent d42a099 commit a90c71c

4 files changed

Lines changed: 55 additions & 16 deletions

File tree

src/LogMkAgent/LogMkAgent.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.10" />
17-
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.10" />
18-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.10" />
19-
<PackageReference Include="System.Text.Json" Version="9.0.10" />
16+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.11" />
17+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.11" />
18+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.11" />
19+
<PackageReference Include="System.Text.Json" Version="9.0.11" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

src/LogMkApi/Data/DatabaseInitializer.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
using LogMkApi.Data.Models;
22
using LogMkApi.Services;
33
using RoboDodd.OrmLite;
4+
using Dapper;
45

56
namespace LogMkApi.Data;
67

78
public class DatabaseInitializer
89
{
910
private readonly DbConnectionFactory _dbFactory;
1011
private readonly PasswordService _passwordService;
12+
private readonly ILogger<DatabaseInitializer> _logger;
1113

12-
public DatabaseInitializer(DbConnectionFactory dbFactory, PasswordService passwordService)
14+
public DatabaseInitializer(DbConnectionFactory dbFactory, PasswordService passwordService, ILogger<DatabaseInitializer> logger)
1315
{
1416
_dbFactory = dbFactory;
1517
_passwordService = passwordService;
18+
_logger = logger;
1619
}
1720

1821
public void CreateTable()
@@ -36,6 +39,42 @@ public void CreateTable()
3639
user.PasswordHash = _passwordService.HashPassword(user, "admin");
3740
db.Insert(user);
3841
}
42+
43+
// Ensure critical indexes exist (migration for existing databases)
44+
EnsureLogIndexes(db);
45+
}
46+
}
47+
48+
private void EnsureLogIndexes(System.Data.IDbConnection db)
49+
{
50+
try
51+
{
52+
// Check if the composite index exists
53+
var checkIndexSql = @"
54+
SELECT COUNT(*)
55+
FROM information_schema.statistics
56+
WHERE table_schema = DATABASE()
57+
AND table_name = 'Log'
58+
AND index_name = 'Deployment_Pod_TimeStamp_idx'";
59+
60+
var indexExists = db.ExecuteScalar<int>(checkIndexSql) > 0;
61+
62+
if (!indexExists)
63+
{
64+
_logger.LogInformation("Creating missing composite index Deployment_Pod_TimeStamp_idx on Log table");
65+
66+
// Create the index
67+
var createIndexSql = @"
68+
CREATE INDEX Deployment_Pod_TimeStamp_idx
69+
ON `Log` (Deployment, Pod, TimeStamp)";
70+
71+
db.Execute(createIndexSql);
72+
_logger.LogInformation("Successfully created composite index Deployment_Pod_TimeStamp_idx");
73+
}
74+
}
75+
catch (Exception ex)
76+
{
77+
_logger.LogWarning(ex, "Failed to create missing indexes, they may already exist or require manual creation");
3978
}
4079
}
4180
}

src/LogMkApi/Data/LogRepo.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ public async Task<IEnumerable<LatestDeploymentEntry>> GetLatestEntryTimes()
161161
{
162162
using (var db = _dbFactory.CreateConnection())
163163
{
164-
// Use FORCE INDEX to utilize the composite index (Deployment, Pod, TimeStamp)
165-
// This significantly improves GROUP BY and MAX() performance on large tables
164+
// Use the composite index (Deployment, Pod, TimeStamp) for optimal performance
165+
// The index is created by DatabaseInitializer.EnsureLogIndexes()
166166
var query = @"
167167
SELECT Deployment, Pod, MAX(TimeStamp) AS TimeStamp
168-
FROM Log FORCE INDEX (Deployment_Pod_TimeStamp_idx)
168+
FROM Log USE INDEX (Deployment_Pod_TimeStamp_idx)
169169
GROUP BY Deployment, Pod
170170
";
171171
return await db.QueryAsync<LatestDeploymentEntry>(query);
@@ -176,11 +176,11 @@ public async Task<IEnumerable<DeploymentCount>> GetDeploymentCounts()
176176
{
177177
using (var db = _dbFactory.CreateConnection())
178178
{
179-
// Use FORCE INDEX to utilize the composite index (Deployment, Pod, TimeStamp)
180-
// This significantly improves GROUP BY performance on large tables
179+
// Use the composite index (Deployment, Pod, TimeStamp) for optimal performance
180+
// The index is created by DatabaseInitializer.EnsureLogIndexes()
181181
var query = @"
182182
SELECT Deployment, Pod, COUNT(*) AS Count
183-
FROM Log FORCE INDEX (Deployment_Pod_TimeStamp_idx)
183+
FROM Log USE INDEX (Deployment_Pod_TimeStamp_idx)
184184
GROUP BY Deployment, Pod
185185
";
186186
return await db.QueryAsync<DeploymentCount>(query);

src/LogMkApi/LogMkApi.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
<ItemGroup>
1616

17-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.10" />
18-
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.10" />
19-
<PackageReference Include="MySql.Data" Version="9.4.0" />
20-
<PackageReference Include="System.Text.Json" Version="9.0.10" />
17+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.11" />
18+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.11" />
19+
<PackageReference Include="MySql.Data" Version="9.5.0" />
20+
<PackageReference Include="System.Text.Json" Version="9.0.11" />
2121
<PackageReference Include="Microsoft.AspNetCore.SpaProxy">
22-
<Version>9.0.10</Version>
22+
<Version>9.0.11</Version>
2323
</PackageReference>
2424
</ItemGroup>
2525
<ItemGroup>

0 commit comments

Comments
 (0)