ORM框架对分表分库的实现 (3)

db搜索的条件含有id字段,或者 db更新的条件含有id字段 或者db删除的条件含有id字段

都应该走取模算法来走对应的table。

下面来测试它

Table Sharding 使用场景举例说明 /// <summary> /// 测试mod分表插入到testorm3数据库的orders_1表里面 /// </summary> [TestMethod] public void TestMethod1_01() { var id = 1; var odIsExist = DB.Tables.Orders.Any(r => r.ID.Equals(id)); if (odIsExist) { return; } var order = new Orders { ID = id, Name = "订单1" }; var result = DB.Insert(order); Assert.AreEqual(result, 1); } /// <summary> /// 测试mod分表插入到testorm3数据库的orders_0表里面 /// </summary> [TestMethod] public void TestMethod1_02() { var id = 2; var odIsExist = DB.Tables.Orders.Any(r => r.ID.Equals(id)); if (odIsExist) { return; } var order = new Orders { ID = id, Name = "订单2" }; var result = DB.Insert(order); Assert.AreEqual(result, 1); } /// <summary> /// 测试mod分表 查询testorm3数据库orders_1 /// </summary> [TestMethod] public void TestMethod1_03() { var id = 1; var tb1 = DB.Tables.Orders.FirstOrDefault(r => r.ID.Equals(id)); Assert.IsNotNull(tb1); } /// <summary> /// 测试mod分表 查询testorm3数据库orders_0 /// </summary> [TestMethod] public void TestMethod1_04() { var id = 2; var tb1 = DB.Tables.Orders.FirstOrDefault(r => r.ID.Equals(id)); Assert.IsNotNull(tb1); } /// <summary> /// 测试mod分表 不指定sharing column 查询叠加 /// </summary> [TestMethod] public void TestMethod1_05() { var tb1 = DB.Tables.Orders.ToList(); Assert.IsNotNull(tb1); Assert.AreEqual(tb1.Count, 2); var odIsExist = DB.Tables.Orders.Where(r => r.ID.Equals(1) || r.ID.Equals(2)).ToList(); Assert.AreEqual(odIsExist.Count, 2); } /// <summary> /// 测试mod分表修改到testorm3数据库orders_1 /// </summary> [TestMethod] public void TestMethod1_06() { var id = 1; var result = DB.Tables.Orders.Where(r => r.ID.Equals(id)).Set(r => r.Name, y => y.Name + "1").Update(); Assert.AreEqual(result, 1); } /// <summary> /// 测试mod分表修改到testorm3数据库orders_0 /// </summary> [TestMethod] public void TestMethod1_07() { var id = 2; var result = DB.Tables.Orders.Where(r => r.ID.Equals(id)).Set(r => r.Name, y => y.Name + "1").Update(); Assert.AreEqual(result, 1); } /// <summary> /// 测试mod分表删除到testorm3数据库orders_1 /// </summary> [TestMethod] public void TestMethod6_08() { var id = 1; var result = DB.Tables.Orders.Where(r => r.ID.Equals(id)).Delete(); Assert.AreEqual(result, 1); } /// <summary> /// 测试mod分表删除到testorm3数据库orders_0 /// </summary> [TestMethod] public void TestMethod6_09() { var id = 2; var result = DB.Tables.Orders.Where(r => r.ID.Equals(id)).Delete(); Assert.AreEqual(result, 1); } /// <summary> /// 测试mod分库批量分别插入到testorm3数据库orders_0 orders_1 /// </summary> [TestMethod] public void TestMethod7_01() { var orderList = new List<Orders>(); orderList.Add(new Orders { ID = 3, Name = "上海大学" }); orderList.Add(new Orders { ID = 4, Name = "上海大学" }); //没有指定 shading column的话是默认分到第一个分片 orderList.Add(new Orders { ID = null, Name = "上海大学" }); var rows = DB.BulkCopy(orderList); Assert.AreEqual(rows.RowsCopied, 3); } /// <summary> /// 不指定sharding 字段会删除所有子表 /// </summary> [TestMethod] public void TestMethod7_03() { var odIsExist = DB.Tables.Orders.Delete(); Assert.AreEqual(odIsExist, 3); } 总结

目前在AntData orm中使用分库分表其实是根据查询的字段来匹配取模或者区间来进行分片,然后只需要配置上进行修改即可。对于实际业务上orm的写法是不需要变化的

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxppf.html