2021-09-24 10:54:30 +08:00
|
|
|
|
using EFCore.BulkExtensions;
|
2021-09-08 10:44:29 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Sample.BulkConsole.DbContexts;
|
|
|
|
|
using Sample.BulkConsole.Entities;
|
|
|
|
|
using ShardingCore;
|
|
|
|
|
using ShardingCore.Extensions;
|
2021-09-24 10:54:30 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2022-06-17 10:57:25 +08:00
|
|
|
|
using ShardingCore.Bootstrappers;
|
2021-10-03 14:09:01 +08:00
|
|
|
|
using ShardingCore.Extensions.ShardingPageExtensions;
|
2022-01-06 21:30:05 +08:00
|
|
|
|
using ShardingCore.TableExists;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
using ShardingCore.TableExists.Abstractions;
|
2021-09-08 10:44:29 +08:00
|
|
|
|
|
|
|
|
|
namespace Sample.BulkConsole
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
|
|
|
|
});
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
|
services.AddLogging();
|
2022-01-06 21:30:05 +08:00
|
|
|
|
|
|
|
|
|
services.AddShardingDbContext<MyShardingDbContext>()
|
|
|
|
|
.AddEntityConfig(o =>
|
2021-09-23 10:17:25 +08:00
|
|
|
|
{
|
2022-01-06 21:30:05 +08:00
|
|
|
|
o.AddShardingTableRoute<OrderVirtualRoute>();
|
2021-09-23 10:17:25 +08:00
|
|
|
|
})
|
2022-01-06 21:30:05 +08:00
|
|
|
|
.AddConfig(op =>
|
|
|
|
|
{
|
|
|
|
|
op.UseShardingQuery((conStr, builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
|
|
|
|
|
});
|
|
|
|
|
op.UseShardingTransaction((connection, builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger);
|
|
|
|
|
});
|
|
|
|
|
op.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=MyOrderSharding;Integrated Security=True;");
|
|
|
|
|
|
2022-07-03 16:52:03 +08:00
|
|
|
|
}).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>().EnsureConfig();
|
2021-09-08 10:44:29 +08:00
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
2022-07-03 16:52:03 +08:00
|
|
|
|
serviceProvider.UseAutoTryCompensateTable();
|
2021-09-08 10:44:29 +08:00
|
|
|
|
using (var serviceScope = serviceProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var myShardingDbContext = serviceScope.ServiceProvider.GetService<MyShardingDbContext>();
|
2021-09-24 10:11:39 +08:00
|
|
|
|
|
2021-09-08 10:44:29 +08:00
|
|
|
|
if (!myShardingDbContext.Set<Order>().Any())
|
|
|
|
|
{
|
|
|
|
|
var begin = DateTime.Now.Date.AddDays(-3);
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
var current = begin;
|
|
|
|
|
ICollection<Order> orders = new LinkedList<Order>();
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (current < now)
|
|
|
|
|
{
|
|
|
|
|
orders.Add(new Order()
|
|
|
|
|
{
|
|
|
|
|
Id = i.ToString(),
|
|
|
|
|
OrderNo = $"orderno-" + i.ToString(),
|
|
|
|
|
Seq = i,
|
|
|
|
|
CreateTime = current
|
|
|
|
|
});
|
|
|
|
|
i++;
|
|
|
|
|
current = current.AddMilliseconds(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var startNew = Stopwatch.StartNew();
|
2021-09-24 10:11:39 +08:00
|
|
|
|
var bulkShardingEnumerable = myShardingDbContext.BulkShardingTableEnumerable(orders);
|
2021-09-08 10:44:29 +08:00
|
|
|
|
startNew.Stop();
|
|
|
|
|
Console.WriteLine($"订单总数:{i}条,myShardingDbContext.BulkShardingEnumerable(orders)用时:{startNew.ElapsedMilliseconds}毫秒");
|
|
|
|
|
startNew.Restart();
|
2021-09-23 10:17:25 +08:00
|
|
|
|
foreach (var dataSourceMap in bulkShardingEnumerable)
|
2021-09-08 10:44:29 +08:00
|
|
|
|
{
|
2021-09-24 10:11:39 +08:00
|
|
|
|
dataSourceMap.Key.BulkInsert(dataSourceMap.Value.ToList());
|
2021-09-08 10:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
startNew.Stop();
|
|
|
|
|
Console.WriteLine($"订单总数:{i}条,myShardingDbContext.BulkInsert(orders)用时:{startNew.ElapsedMilliseconds}毫秒");
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("ok");
|
|
|
|
|
}
|
2021-09-17 13:16:52 +08:00
|
|
|
|
|
2021-09-15 16:50:00 +08:00
|
|
|
|
var b = DateTime.Now.Date.AddDays(-3);
|
2022-05-17 17:20:03 +08:00
|
|
|
|
var queryable = myShardingDbContext.Set<Order>().Select(o=>new {Id=o.Id,OrderNo=o.OrderNo, CreateTime =o.CreateTime });//.Where(o => o.CreateTime >= b);
|
2022-10-26 10:09:14 +08:00
|
|
|
|
var dateTime = DateTime.Now;
|
|
|
|
|
var dbContexts = myShardingDbContext.BulkShardingTableExpression<MyShardingDbContext,Order>(o=>o.CreateTime<=dateTime);
|
|
|
|
|
using (var dbContextTransaction = myShardingDbContext.Database.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
foreach (var dbContext in dbContexts)
|
|
|
|
|
{
|
|
|
|
|
dbContext.Set<Order>().Where(o=>o.CreateTime<=dateTime).BatchUpdate(a => new Order { OrderNo = "12345" });
|
|
|
|
|
}
|
|
|
|
|
dbContextTransaction.Commit();
|
|
|
|
|
}
|
2021-09-08 10:44:29 +08:00
|
|
|
|
var startNew1 = Stopwatch.StartNew();
|
|
|
|
|
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
2022-05-17 17:20:03 +08:00
|
|
|
|
//startNew1.Restart();
|
|
|
|
|
//var list2 = queryable.Take(1000).ToList();
|
|
|
|
|
//startNew1.Stop();
|
|
|
|
|
//Console.WriteLine($"获取1000条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
2022-05-17 17:20:03 +08:00
|
|
|
|
//startNew1.Restart();
|
|
|
|
|
//var list = queryable.Take(10).ToList();
|
|
|
|
|
//startNew1.Stop();
|
|
|
|
|
//Console.WriteLine($"获取10条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
|
|
|
|
|
2022-05-17 17:20:03 +08:00
|
|
|
|
//startNew1.Restart();
|
|
|
|
|
//var list1 = queryable.Take(100).ToList();
|
|
|
|
|
//startNew1.Stop();
|
|
|
|
|
//Console.WriteLine($"获取100条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
|
|
|
|
|
2022-05-17 17:20:03 +08:00
|
|
|
|
//startNew1.Restart();
|
|
|
|
|
//var list3 = queryable.Take(10000).ToList();
|
|
|
|
|
//startNew1.Stop();
|
|
|
|
|
//Console.WriteLine($"获取100000条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-17 17:20:03 +08:00
|
|
|
|
//startNew1.Restart();
|
|
|
|
|
//var list4 = queryable.Take(20000).ToList();
|
|
|
|
|
//startNew1.Stop();
|
|
|
|
|
//Console.WriteLine($"获取20000条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
|
|
|
|
|
2021-10-07 04:53:54 +08:00
|
|
|
|
int skip = 0, take = 1000;
|
2022-05-17 17:20:03 +08:00
|
|
|
|
for (int i = 20; i < 30000; i++)
|
2021-09-08 10:44:29 +08:00
|
|
|
|
{
|
|
|
|
|
skip = take * i;
|
|
|
|
|
startNew1.Restart();
|
|
|
|
|
var shardingPagedResult = queryable.ToShardingPage(i+1, take);
|
|
|
|
|
startNew1.Stop();
|
|
|
|
|
Console.WriteLine($"流式分页skip:[{skip}],take:[{take}]耗时用时:{startNew1.ElapsedMilliseconds}毫秒");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("ok");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|