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;
|
2021-10-29 10:44:48 +08:00
|
|
|
|
using ShardingCore.Bootstrapers;
|
2021-10-03 14:09:01 +08:00
|
|
|
|
using ShardingCore.Extensions.ShardingPageExtensions;
|
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();
|
2021-10-11 20:58:55 +08:00
|
|
|
|
services.AddShardingDbContext<MyShardingDbContext>(
|
2021-11-04 17:42:26 +08:00
|
|
|
|
(conn, o) => o.UseSqlServer(conn).UseLoggerFactory(efLogger))
|
2021-09-23 10:17:25 +08:00
|
|
|
|
.Begin(o =>
|
|
|
|
|
{
|
|
|
|
|
o.CreateShardingTableOnStart = true;
|
|
|
|
|
o.EnsureCreatedWithOutShardingTable = true;
|
2021-10-08 13:15:17 +08:00
|
|
|
|
o.AutoTrackEntity = true;
|
2021-09-23 10:17:25 +08:00
|
|
|
|
})
|
2021-10-20 11:08:44 +08:00
|
|
|
|
.AddShardingTransaction((connection, builder) =>
|
|
|
|
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
2021-09-20 22:26:48 +08:00
|
|
|
|
.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=MyOrderSharding;Integrated Security=True;")
|
2021-09-23 10:17:25 +08:00
|
|
|
|
.AddShardingTableRoute(op=> {
|
2021-09-08 10:44:29 +08:00
|
|
|
|
op.AddShardingTableRoute<OrderVirtualRoute>();
|
2021-09-20 22:26:48 +08:00
|
|
|
|
}).End();
|
2021-09-08 10:44:29 +08:00
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
serviceProvider.GetService<IShardingBootstrapper>().Start();
|
|
|
|
|
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);
|
2021-09-08 10:44:29 +08:00
|
|
|
|
var queryable = myShardingDbContext.Set<Order>().Where(o => o.CreateTime >= b).OrderBy(o => o.CreateTime);
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
2021-09-08 10:44:29 +08:00
|
|
|
|
var startNew1 = Stopwatch.StartNew();
|
|
|
|
|
|
2021-10-08 13:15:17 +08:00
|
|
|
|
|
|
|
|
|
startNew1.Restart();
|
|
|
|
|
var list2 = queryable.Take(1000).ToList();
|
|
|
|
|
startNew1.Stop();
|
|
|
|
|
Console.WriteLine($"获取1000条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
|
|
|
|
|
|
|
|
|
startNew1.Restart();
|
|
|
|
|
var list = queryable.Take(10).ToList();
|
|
|
|
|
startNew1.Stop();
|
|
|
|
|
Console.WriteLine($"获取10条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startNew1.Restart();
|
|
|
|
|
var list1 = queryable.Take(100).ToList();
|
|
|
|
|
startNew1.Stop();
|
|
|
|
|
Console.WriteLine($"获取100条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startNew1.Restart();
|
|
|
|
|
var list3 = queryable.Take(10000).ToList();
|
|
|
|
|
startNew1.Stop();
|
|
|
|
|
Console.WriteLine($"获取100000条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startNew1.Restart();
|
|
|
|
|
var list4 = queryable.Take(20000).ToList();
|
|
|
|
|
startNew1.Stop();
|
|
|
|
|
Console.WriteLine($"获取20000条用时:{startNew1.ElapsedMilliseconds}毫秒");
|
|
|
|
|
|
|
|
|
|
|
2021-10-07 04:53:54 +08:00
|
|
|
|
int skip = 0, take = 1000;
|
2021-09-24 10:11:39 +08:00
|
|
|
|
for (int i = 20000; 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");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|