首页 > 代码库 > 使用 Parallel LINQ 进行数据分页
使用 Parallel LINQ 进行数据分页
a) 第一种【耗时11~18s】,这种查询方式并不是很优化,但是目前也没有想到更好的方式,除了创建一张中间表,是不是可以使用【全文索引】?
SELECT * FROM
(
SELECT
ROW_NUMBER() OVER(ORDER BY IsPutAway) AS Num, * FROM
(
SELECT * FROM Goods_1 WHERE Name Like ‘%测试%‘ UNION ALL
SELECT * FROM Goods_2 WHERE Name Like ‘%测试%‘ UNION ALL
SELECT * FROM Goods_3 WHERE Name Like ‘%测试%‘ UNION ALL
SELECT * FROM Goods_4 WHERE Name Like ‘%测试%‘ UNION ALL
SELECT * FROM Goods_5 WHERE Name Like ‘%测试%‘ UNION ALL
SELECT * FROM Goods_6 WHERE Name Like ‘%测试%‘ UNION ALL
SELECT * FROM Goods_7 WHERE Name Like ‘%测试%‘ UNION ALL
SELECT * FROM Goods_8 WHERE Name Like ‘%测试%‘
) AS T
) AS T
WHERE Num BETWEEN ((2 - 1) * 10 + 1) AND 2 * 10;
b) 第二种【耗时3~7s】,这种方式采用Linq 并行多多线程的方式,查询数据库,再使用程序进行排序并分组。这是我想到的一种解决方式,效果还算理想!
List<VGoodsModel> dataList = new List<VGoodsModel>(); using (var content = CreateContext()) { List<int> tableIndexList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 }; StringBuilder sbSqlAll = new StringBuilder(); sbSqlAll.AppendLine(); tableIndexList.AsParallel().ForAll(o => { StringBuilder sbSql = new StringBuilder(); sbSql.AppendFormat("SELECT * FROM Goods_{0} with(nolock) WHERE", o); if (Para.GoodsId <= 0) sbSql.AppendFormat(" Type={0}", GoodsClassify.TYPE); else sbSql.AppendFormat(" Id={0} AND Type={1}", Para.GoodsId, GoodsClassify.TYPE); if (!string.IsNullOrWhiteSpace(Para.GoodsName)) sbSql.AppendFormat(" AND Name LIKE ‘%{0}%‘", Para.GoodsName); var list = content.ExcuteList<VGoodsModel>(sbSql.ToString(), CommandType.Text, null); dataList.AddRange(list); sbSqlAll.AppendLine(string.Format("{0}, 执行SQL:{1}", o, sbSql.ToString())); }); Weimob.Log.FloderLogger.CreateInstance("GetGoodsPagingByGoodsName").SingleLineDebug(sbSqlAll.ToString()); }
使用 Parallel LINQ 进行数据分页