LINQPad / LINQ To SQL - 簡單查詢僅在循環內執行時才會引發內存不足 (LINQPad / LINQ To SQL - Simple Query Throws Out of Memory Only When Executed Inside a Loop)


問題描述

LINQPad / LINQ To SQL ‑ 簡單查詢僅在循環內執行時才會引發內存不足 (LINQPad / LINQ To SQL ‑ Simple Query Throws Out of Memory Only When Executed Inside a Loop)

我有一個具有以下關係的數據庫:

計算 ‑

我需要循環所有的計算,然後查詢相關的 BinaryData 行。BinaryData 的查詢是 BinaryData.bdCalculationKey 上的一個簡單 where 子句。運行此查詢在獨立運行時會立即返回,但在計算循環內運行時會拋出內存不足異常。代碼如下所示:

var groupName = "NYPH";
var serverName = "192.168.100.132";

var tahitiDB = 
    new BTR.Evolution.Data.DataContexts.Legacy.Profile.BtrProfile( 
        BTR.Evolution.Core.Registry.ProfileDatabase( groupName, serverName ) 
    ) { ObjectTrackingEnabled = false, DeferredLoadingEnabled = false, CommandTimeout = 30 };

var data = tahitiDB.Calculations.Where(c => c.Profile.Client.cName == groupName && !c.calcIsFailed);

data.Count().Dump();

tahitiDB.BinaryDatas
        .Where(b => b.bdCalculationKey == new Guid( "3d53aa12‑0353‑e911‑83b4‑005056b9729f" ) )
        .Dump();

foreach( var p in data )
{
    p.calcKey.Dump();
    tahitiDB.BinaryDatas
            .Where(b => b.bdCalculationKey == p.calcKey )
            .Select(b => new { b.bdKey, b.bdCalculationKey, b.bdFilename, b.bdDateCreated })
            .Dump();

    break;
}

這是 LINQPad 結果的屏幕截圖(也顯示了成功轉儲查詢outside循環):






<hr>




<h2>參考解法</h2>

<h4>方法 1:</h4> <p>Given that you're not making updates, you should disable object tracking to avoid keeping all the database objects in memory:</p><pre><code>this.ObjectTrackingEnabled = false;
</code></pre><p>(by <a href=TerryJoe Albahari)

參考文件

  1. LINQPad / LINQ To SQL ‑ Simple Query Throws Out of Memory Only When Executed Inside a Loop (CC BY‑SA 2.5/3.0/4.0)

#locking #SQL #linqpad #linq-to-sql






相關問題

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

在程序文件夾中創建鎖定文件會導致異常 (Creating Lock file in Programs Folder causes exception)

我什麼時候會使用 AutoResetEvent 和 ManualResetEvent 而不是 Monitor.Wait()/Monitor.Pulse()? (When would I use AutoResetEvent and ManualResetEvent instead of Monitor.Wait()/Monitor.Pulse()?)

鎖定一個 JavaScript 函數 (Lock a JavaScript Function)

當只有一個線程寫入共享變量時,我需要鎖嗎? (Do I need a lock when only a single thread writes to a shared variable?)

為什麼 lock(objLock) 比 lock(this) 好 (Why is it better to lock(objLock) than lock(this))

雙重檢查鎖定的修復有什麼問題? (What's wrong with this fix for double checked locking?)

在表格行上調用 Dibs (Calling Dibs on a table row)

我怎樣才能優雅地寫 lock {}? (how can I write lock {} elegantly?)

Double Check Lock 不能在這個 java 代碼上工作? (Doubly Check Lock Dosen't work on this java code?)

LINQPad / LINQ To SQL - 簡單查詢僅在循環內執行時才會引發內存不足 (LINQPad / LINQ To SQL - Simple Query Throws Out of Memory Only When Executed Inside a Loop)

如何在 dynamoDB 中實現 50 次寫入的事務? (How can I implement a transaction of 50 writes in dynamoDB?)







留言討論