顺序查询
2022-11-24 小于 1 分钟
# 全表扫描了
如果你的分片是有序的并且可以通过某些字段排序来进行过滤,sharding-core提供了顺序查询功能,解决方案 (opens new window)
# 全局提示或阶段防止全表扫描
通过路由后置过滤器
public class SysUserLogByMonthRoute:AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<SysUserLogByMonth>
{
private readonly ILogger<SysUserLogByMonthRoute> _logger;
public SysUserLogByMonthRoute(ILogger<SysUserLogByMonthRoute> logger)
{
_logger = logger;
}
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 01);
}
public override bool AutoCreateTableByTime()
{
return true;
}
public override void Configure(EntityMetadataTableBuilder<SysUserLogByMonth> builder)
{
builder.ShardingProperty(o => o.Time);
}
protected override List<TableRouteUnit> AfterShardingRouteUnitFilter(DataSourceRouteResult dataSourceRouteResult, List<TableRouteUnit> shardingRouteUnits)
{
if (shardingRouteUnits.Count > 10)
{
_logger.LogInformation("截断前:"+string.Join(",",shardingRouteUnits.Select(o=>o.Tail)));
//这边你要自己做顺序处理阶段
var result= shardingRouteUnits.OrderByDescending(o=>o.Tail).Take(10).ToList();
_logger.LogInformation("截断后:"+string.Join(",",result.Select(o=>o.Tail)));
return result;
}
return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36