-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linq中的where Context.[table].Any(...)未能使用分表 #245
Comments
是生成的sql里面b表没有分片,还是分片了但是没有按规则过滤 @NuxYoung |
是生成的sql里b表没有分片 |
@NuxYoung 什么版本的sharding-core |
@NuxYoung 最新版本我测试是可以的我不知道你是什么版本你发一下我看 |
我用的也是最新版本(7.7.1.9),但生成的查询sql、查询结果都是没有分片的。 public class TableBVirtualTableRoute : AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<TableB>
{
public override bool AutoCreateTableByTime()
{
return true;
}
public override void Configure(EntityMetadataTableBuilder<BoxCode> builder)
{
builder.ShardingProperty(b => b.BTime);
builder.AutoCreateTable(true);
}
/// <summary>
/// 使用固定值,否则重启服务的时候会重置
/// </summary>
/// <returns></returns>
public override DateTime GetBeginTime()
{
return new DateTime(2023, 3, 1);
}
// 若要查看ShadingCore的异常,取消注释
//public override bool DoLogError => true;
} 然后目前我是用了 |
@NuxYoung o.UseShardingMigrationConfigure(b =>
{
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
}); |
如果日志级别改成debug应该会有如下日志打印
|
日志输出应该是有异常。其中 |
@NuxYoung 还有一种办法你可以吧变量先提取出来 var tableb=Context.B;
var test = from a in Context.A
where tableb.Any(
b => b.foreignId == a.Id
&& b.column1 == "1" || a.column2 > 0) |
相关代码如下: public class BoxStickerContext : AbstractShardingDbContext, IShardingTableDbContext
{
public BoxStickerContext(DbContextOptions<BoxStickerContext> options) : base(options)
{
}
public DbSet<BoxCode> BoxCode { get; set; }
public DbSet<FactoryOrder> FactoryOrder { get; set; }
public IRouteTail RouteTail { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BoxCode>();
modelBuilder.Entity<FactoryOrder>();
base.OnModelCreating(modelBuilder);
}
} Startup.cs: public void ConfigureServices(IServiceCollection services)
{
services.AddShardingDbContext<BoxStickerContext>()
.UseRouteConfig(op =>
{
// 此处配置需要分表的表名
op.AddShardingTableRoute<BoxCodeVirtualTableRoute>();
}).UseConfig((sp, op) =>
{
op.UseShardingQuery((conn, builder) =>
{
builder.UseSqlServer(conn).UseLoggerFactory(efLogger);
});
op.UseShardingTransaction((conn, builder) =>
{
builder.UseSqlServer(conn).UseLoggerFactory(efLogger);
});
op.AddDefaultDataSource("BoxStickerContext", configuration.GetConnectionString("BoxStickerConnection"));
op.UseShardingMigrationConfigure(op =>
{
op.ReplaceService<IMigrationsSqlGenerator, ShardingMigrationsSqlGenerator<BoxStickerContext>>();
});
}).AddShardingCore();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.ApplicationServices.UseAutoTryCompensateTable();
} 分片路由: public class BoxCodeVirtualTableRoute : AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<BoxCode>
{
public override bool AutoCreateTableByTime()
{
return true;
}
public override void Configure(EntityMetadataTableBuilder<BoxCode> builder)
{
builder.ShardingProperty(b => b.BTime);
builder.AutoCreateTable(true);
}
/// <summary>
/// 使用固定值,否则重启服务的时候会重置
/// </summary>
/// <returns></returns>
public override DateTime GetBeginTime()
{
return new DateTime(2023, 3, 1);
}
// 若要查看ShadingCore的异常,取消注释
//public override bool DoLogError => true;
} |
@NuxYoung 实在不好意思我想问下您具体执行的queryable表达式是什么 |
有表A和表B,其中表B采用了按创建时间的月份分表,执行以下查询的时候未能从分表中查询数据:
有好的办法解决这个问题吗?
The text was updated successfully, but these errors were encountered: