[英]sql nhibernate performance for loop
I have the following logic: 我有以下逻辑:
loop through a list of ids, get the associated entity, and for that entity, loop through another list of ids and get another entity. 遍历一个ID列表,获取关联的实体,对于该实体,遍历另一个ID列表,获取另一个实体。 Code is below: 代码如下:
foreach (var docId in docIds)
{
var doc = new EntityManager<Document>().GetById(docId);
foreach (var tradeId in tradeIds)
{
var trade = new EntityManager<Trade>().GetById(tradeId);
if (doc.Trade.TradeId != trade.TradeId)
{
Document newDoc = new Document(doc, trade, 0);
new EntityManager<Document>().Add(newDoc);
}
}
}
my question is mainly about sql performance. 我的问题主要是关于sql性能。 Obviously there will be a bunch of selects
happening, as well as some adds
. 显然,将会有很多selects
,以及一些adds
。 Is this a bad way to go about doing something like this? 这是做这样事情的不好方法吗?
Should I, instead, use a session
and get a list of all entities that match the list of ids (with 1 select statement) and then loop after? 相反,我是否应该使用session
并获取与ID列表匹配的所有实体的列表(使用1条select语句),然后在之后循环?
It depends only on my expirience. 这仅取决于我的经验。 But you can test it yourselve. 但是您可以自己进行测试。 If Trade entity isn't very big and count of entities wouldnt be over 1000 - reading all entities and loop after will be much preferable. 如果贸易实体不是很大,并且实体数不会超过1000,则最好读取所有实体并在其后循环。 If count is more 1k - its better to call stored procedure with joining temp table, containing your ids. 如果count大于1k-最好使用包含您的ID的联接临时表来调用存储过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.