怎么减轻单次大查询数据库压力

  • 分多次查询,减少数据库压力
var pageList = new Dictionary<int, int>();//分批次查询
var pageSize = 10000;//每次返回1w条
float total = 总条数;
var arr = (total / pageSize).ToString().Split('.');
var totalPage = int.Parse(arr[0]) + (arr.Length == 2 ? 1 : 0);//有小数则+1
for (int currNo = 1; currNo <= totalPage; currNo++)
{
	int startNum = 1 + (pageSize * (currNo - 1));//起
	int endNum = pageSize * currNo; //止
	pageList.Add(startNum, endNum);
}
var dt = new DataTable();
//分批次查询
foreach (var item in pageList)
{
	var tempSql =  $" select * from (select rownum rn ,tbl.* from tbl ) where rn between {item.Key} and {item.Value}"; //分页查询
	var tempDt = GetTable(tempSql);
	if (tempDt != null && tempDt.Rows.Count > 0)
	{
		dt.Merge(tempDt);
	}
}

posted on 2022-10-12 13:35  anjun_xf  阅读(28)  评论(0编辑  收藏  举报

导航

TOP