Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions BenchmarkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ namespace OptimizeMePlease
{
[MemoryDiagnoser]
[HideColumns(BenchmarkDotNet.Columns.Column.Job, BenchmarkDotNet.Columns.Column.RatioSD, BenchmarkDotNet.Columns.Column.StdDev, BenchmarkDotNet.Columns.Column.AllocRatio)]
//[Config(typeof(Config))]
[Config(typeof(Config))]
public class BenchmarkService
{
public BenchmarkService()
{
}

//private class Config : ManualConfig
//{
// public Config()
// {
// SummaryStyle = BenchmarkDotNet.Reports.SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend);
// }
//}
private class Config : ManualConfig
{
public Config()
{
SummaryStyle = BenchmarkDotNet.Reports.SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend);
}
}

/// <summary>
/// Get top 2 Authors (FirstName, LastName, UserName, Email, Age, Country)
/// from country Serbia aged 27, with the highest BooksCount
/// and all his/her books (Book Name/Title and Publishment Year) published before 1900
/// </summary>
/// <returns></returns>
[Benchmark]
[Benchmark(Baseline = true)]
public List<AuthorDTO> GetAuthors()
{
using var dbContext = new AppDbContext();
Expand Down Expand Up @@ -98,6 +98,45 @@ public List<AuthorDTO> GetAuthors()
return finalAuthors;
}

// 1260x faster than GetAuthors() with this query + these indexes
// CREATE NONCLUSTERED INDEX idx_Books ON Books (AuthorId, Published) INCLUDE (Name)
// CREATE NONCLUSTERED INDEX idx_Author ON Authors (Age, BooksCount DESC) INCLUDE (Id, Country, UserId)
private static readonly Func<AppDbContext, IEnumerable<AuthorDTO>> CompiledOptimized =
EF.CompileQuery((AppDbContext context) => context.Authors
.Where(x => x.Country == "Serbia" && x.Age == 27)
.OrderByDescending(x => x.BooksCount)
.Select(x => new AuthorDTO
{
UserFirstName = x.User.FirstName,
UserLastName = x.User.LastName,
UserEmail = x.User.Email,
UserName = x.User.UserName,
AuthorAge = x.Age,
AuthorCountry = x.Country,
AllBooks = x.Books.Where(b => b.Published.Year < 1900).Select(y => new BookDto
{
Name = y.Name,
Published = y.Published,
}).ToList()
})
.Take(2));

/// <summary>
/// Get top 2 Authors (FirstName, LastName, UserName, Email, Age, Country)
/// from country Serbia aged 27, with the highest BooksCount
/// and all his/her books (Book Name/Title and Publishment Year) published before 1900
/// </summary>
/// <returns></returns>
[Benchmark]
public List<AuthorDTO> GetAuthors_Optimized()
{
using var dbContext = new AppDbContext();

var authors = CompiledOptimized(dbContext).ToList();

return authors;
}

//[Benchmark]
//public List<AuthorDTO_Optimized> GetAuthors_Optimized()
//{
Expand Down
4 changes: 3 additions & 1 deletion Context/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true");
options.UseSqlServer(
"Server=localhost,1433;Database=OptimizeMePlease;TrustServerCertificate=True;User Id=sa;Password=test@123;MultipleActiveResultSets=true")
/*.LogTo(s => { Console.WriteLine(s); }, LogLevel.Information)*/;
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
14 changes: 7 additions & 7 deletions OptimizeMePlease.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.29">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0-rc.2.23480.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="161.47027.0" />
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="170.18.0" />
<!--<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.15.1" />-->
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class Program
static void Main(string[] args)
{
//Debugging
//BenchmarkService benchmarkService = new BenchmarkService();
//var p = benchmarkService.GetAuthors_Optimized_Struct();
//var d = benchmarkService.GetAuthors_Optimized_Struct1();
BenchmarkService benchmarkService = new BenchmarkService();
// var d = benchmarkService.GetAuthors();
// var p = benchmarkService.GetAuthors_Optimized();

//Comment me after first execution, please.
//IWillPopulateData();
Expand All @@ -36,7 +36,7 @@ static void Main(string[] args)

public static void IWillPopulateData()
{
string sqlConnectionString = @"Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true";
string sqlConnectionString = @"Server=localhost,1433;Database=OptimizeMePlease;TrustServerCertificate=True;User Id=sa;Password=test@123;MultipleActiveResultSets=true";

string workingDirectory = Environment.CurrentDirectory;
string path = Path.Combine(Directory.GetParent(workingDirectory).Parent.Parent.FullName, @"script.sql");
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# OptimizeMePlease

# Docker Compose

- Run `docker-compose up -d` to start the database

## You are probably here because you saw my post on Linkedin.
## Welcome!

Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.9"

services:
db:
image: "mcr.microsoft.com/mssql/server:2022-latest"
user: root
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "test@123"
volumes:
- db_vol:/var/opt/mssql/data
ports:
- "1433:1433"

volumes:
db_vol: {}