我試圖優化一些查詢,而且我有這個瘋狂的問題。基本的想法是我得到一堆有相應會議的房間。我目前正在運行一個查詢來獲取所有房間,然後在每個房間我需要召開會議,在那裏爲每個房間進行查詢。這爲大量的數據庫連接(即每個1000個房間都必須打開一個連接來召開會議)打開了一扇門,而且我希望以批處理的方式進行操作。我使用短小精悍的映射我的查詢模式,我嘗試使用列表參數描述here小巧的批量查詢而不是多次執行的單個查詢
SELECT
mm.id,
mm.organizer_name as Organizer,
mm.subject as Subject,
mm.start_time as StartTime,
mm.end_time as EndTime,
(mm.deleted_at IS NOT NULL) as WasCancelled,
(am.interactive = 0 AND am.cancelled_at IS NOT NULL) as WasNoShow,
c.name as name
FROM master_meeting mm
LEFT JOIN master_meeting__exchange mme ON mme.id=mm.id
LEFT JOIN master_meeting__forwarded_exchange mmfe ON mmfe.id=mm.id
LEFT JOIN meeting_instance__exchange mie ON mie.meeting_id=mm.id
LEFT JOIN meeting_instance__forwarded_exchange mife ON mife.meeting_id=mm.id
LEFT JOIN appointment_meta__exchange ame ON mie.item_id=ame.item_id
LEFT JOIN appointment_meta__exchange ame2 ON mife.item_id=ame2.item_id
LEFT JOIN appointment_meta am ON am.id=ame.id
LEFT JOIN appointment_meta am2 ON am2.id=ame2.id
LEFT JOIN calendar c on mie.calendar_id=c.id
WHERE mie.calendar_id = @Id OR [email protected]
AND mm.start_time BETWEEN @StartTime AND @EndTime
沒有進入瘋狂的長連接序列的詳細情況,我目前要做的這個查詢,一很多。它已被寫入了最初爲:
List<Result> resultSet = new List<Result>();
foreach(int id in idList){
resultSet.AddRange(
_queryHandler.Handle(
new MeetingQuery(id, "FixedStartTime", "FixedEndTime")
)
);
}
這反過來這一堆次調用並運行查詢:
_connection.Query<Meeting>(sql,
new {
Id = query.id,
StartTime = query.StartTime,
EndTime = query.EndTime
}
);
這顯然需要相當多的數據庫連接,我想通過具有短小精悍做多個查詢,以避免這種情況,但我得到了下面的錯誤,如果我嘗試添加參數爲它看起來像這樣的列表:
class Parameters {
int Id;
string StartTime;
string EndTime;
}
List<Parameters> parameters = new List<Parameters>();
foreach(int id in idList)
parameters.Add(new Parameters(id, "SameStartTime", "SameEndTime");
然後我會使用的參數列表作爲這樣的:
_connection.Query<Meeting>(sql,parameters);
我得到的錯誤是:
短小精悍的其他信息:參數可枚舉序列(數組,列表等)沒有在這方面允許