Run
dotnet add package PgHintPlanEnable the extension
// create the data source
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
await using var dataSource = dataSourceBuilder.Build();
// enable extension using the data source
await dataSource.EnablePgHintPlanAsync();
// alternatively, you can use the connection
var conn = dataSource.OpenConnection();
await conn.EnablePgHintPlanAsync();Run
dotnet add package PgHintPlan.EntityFrameworkCoreImport the library
using PgHintPlan.EntityFrameworkCore;Enable the extension
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UsePgHintPlan();
}Configure the connection
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UsePgHintPlan();
optionsBuilder.UseNpgsql();
}Disable Hash Join
var items = await ctx.Items
.EnableHashJoin(false)
.ToListAsync();Force nested loop for the joins on the tables
var data = await ctx.Products
.Join(ctx.Items,
(product) => product.Id,
(item) => item.ProductId,
(product, item) => new
{
ProductId = product.Id,
Id = item.Id
})
.NestLoop(ctx.Items.EntityType, ctx.Products.EntityType)
.ToListAsync();Force index scan on the table
var index = ctx.Items.EntityType
.GetIndexes()
.Where(i => i.Name == "test_index")
.Single();
var items = await ctx.Items
.IndexScan(ctx.Items.EntityType, index)
.ToListAsync();You can stack as many commands as you want
var items = await ctx.Items
.EnableHashAgg()
.EnableHashJoin(false)
.EnableIndexOnlyScan()
.ToListAsync();Join methods
HashJoinNoHashJoinNestLoopNoNestLoop
Scan methods
SeqScanTidScanIndexScanIndexOnlyScanBitmapScanIndexScanRegexpIndexOnlyScanRegexpBitmapScanRegexpNoSeqScanNoTidScanNoIndexScanNoIndexOnlyScanNoBitmapScan
Join order
Leading
Behavior control on Join
MemoizeNoMemoize
Row number correction
SetRows
Parallel query configuration
Parallel
GUC
EnableHashAggEnableHashJoinEnableIndexOnlyScanEnableIndexScanEnableMaterialEnableNestLoopEnablePartitionPruningEnablePartitionWiseAggregateEnablePartitionWiseJoinEnableSeqScanEnableSort