Bootstrap

开源测试_sqlsugar

分表测试

程序

using SqlSugar;
var db = new SqlSugarClient(new ConnectionConfig
{
    ConnectionString = "Server=127.0.0.1;Database=test;User ID=sa;Password=123456;Trusted_Connection=False;TrustServerCertificate=True;",
    DbType = DbType.SqlServer,
    IsAutoCloseConnection = true,
});

//创建表
db.CodeFirst.InitTables(typeof(SysUser));

//插入
for (int i = 0; i < 10; i++)
{
   var newUser = new SysUser
   {
       Id = SnowFlakeSingle.instance.NextId(),
       Name = "John Doe",
   };
    db.Insertable(newUser).SplitTable().ExecuteCommand();
}
//读取
var users = db.Queryable<SysUser>().SplitTable(tabs => tabs.Take(3)).ToList();
//
var i = db.MasterQueryable<SysUser>().Where(it => it.Name == "John Doe").SplitTable(tabs => tabs.Take(2)).Select(s => SqlFunc.AggregateDistinctCount(s.Id)).First();

测试表类

using SqlSugar;
[SplitTable(SplitType.Day)]//指定按照时间分表
[SugarTable("UserSplit{year}{month}{day}")]
public class SysUser
{
    /// <summary>
    /// 主键 
    ///</summary>
    [Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]
    [SugarColumn(ColumnName="id" ,IsPrimaryKey = true   )]
        public long Id { get; set; }
    /// <summary>
    /// 卡号 
    ///</summary>
        [SugarColumn(ColumnName="cardNo",IsNullable =true)]
        public string? CardNo { get; set; }
    /// <summary>
    /// 用户名 
    ///</summary>
        [SugarColumn(ColumnName="userNo" , IsNullable = true)]
        public string? UserNo { get; set; }
       
    /// <summary>
    /// 用户名 
    ///</summary>
        [SugarColumn(ColumnName="name")]
        public string? Name { get; set; }

    [SplitField] //分表以当前这个属性  对应的数据库表字段数据为维度来分表
    public DateTime CreateTime { get; set; } = DateTime.Now;
}
;