2013-07-03 66 views
0

下面的程序是一個以非常快的速度接收數據的套接字程序。它在CodeFirst部分被禁用的情況下運行良好。如果我啓用它,程序開始正常,但是會慢慢降級到SQL EXPRESS 2012 db沒有任何內容寫入的地步。我通過運行下面的SQL語句來檢查這個SQL語句,它只選擇最後五行SQL Management Studio 2012.Codefirst隨着時間的推移而降低

有沒有什麼我做錯了?

select * from [MarketDataEntities] 
where MarketDataEntities.MarketDataEntityID not in (
    select top (
     (select count(*) from [MarketDataEntities]) - 5 
    ) MarketDataEntities.MarketDataEntityID 
    from [MarketDataEntities] 
) 



       using (var dbTDC = new TickDataTestContext()) 
       { 
        var tde = new SymbolTickDataEntity { Symbol = symbol }; 
        if (!dbTDC.SymbolTickDataDbSet.Any(a => a.Symbol.Equals(symbol))) 
        { 
         dbTDC.SymbolTickDataDbSet.Add(tde); 
         dbTDC.SaveChanges(); 
        } 

        var mdde = new MarketDataDepthEntity(); 
        dbTDC.MarketDataDepthDbSet.Add(mdde); 

        dbTDC.SaveChanges(); 

        while (true) 
        { 
         // Wait for next request from client 
         int len = socket.Receive(zmq_buffer); 
         if (len < 1) 
         { 
          Console.WriteLine("Len < 1"); 

          continue; 
         } 

         //Console.WriteLine("Got quote"); 

         count++; 

         // Copy the bytes 
         byte[] bytes = new byte[len]; 
         Buffer.BlockCopy(zmq_buffer, 0, bytes, 0, len); 

         MarketDataDepth mdd = MarketDataDepth.CreateBuilder().MergeFrom(bytes).Build(); 

         PrintMarketDataDepth(mdd); 
#if false 
         for (int i = 0; i < mdd.MdCount; i++) 
         { 
          MarketData md = mdd.MdList[i]; 

          string timestamp = md.Time; 
          int index = timestamp.IndexOf(","); 
          if(-1 != index) 
           timestamp = timestamp.Remove(index); 

          DateTime parseResult; 
          if (false == System.DateTime.TryParseExact(timestamp, format, provider, DateTimeStyles.None, out parseResult)) 
           continue; 

          var mde = new MarketDataEntity 
          { 
           NMDDTag = (long)mdd.NMDDTag, 
           QType = (0 == md.QuoteType ? QuoteType.Bid : QuoteType.Ask), 
           QPrice = md.Price, 
           QSize = md.Size, 
           QSource = md.Source, 
           QLiquidityTag = md.ID, 
           QSilo = md.Silo, 
           QTimeStamp = parseResult 
          }; 

          dbTDC.MarketDataDbSet.Add(mde); 

          mdde.Depth.Add(mde); 
         } 

         if (0 == count % 500) 
          dbTDC.SaveChanges(); 
#endif 
        } 
       } 
      } 
      catch (DbEntityValidationException e) 
      { 
       foreach (var eve in e.EntityValidationErrors) 
       { 
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", 
         eve.Entry.Entity.GetType().Name, eve.Entry.State); 
        foreach (var ve in eve.ValidationErrors) 
        { 
         Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", 
         ve.PropertyName, ve.ErrorMessage); 
        } 
       } 

       throw; 
      } 
     } 
    } 

    public enum QuoteType { Bid = 0, Ask } 

    public class MarketDataEntity 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int MarketDataEntityID { get; set; } 

     public long NMDDTag { get; set; } 
     public QuoteType QType { get; set; } 
     public double QPrice { get; set; } 
     public double QSize { get; set; } 
     public string QSource { get; set; } 
     public string QLiquidityTag { get; set; } 
     public string QSilo { get; set;} 
     [Column("timestamp", TypeName = "datetime2")] 
     public DateTime QTimeStamp { get; set; } 
    } 

    public class MarketDataDepthEntity 
    { 
     public int MarketDataDepthEntityID { get; set; } 
     public virtual IList<MarketDataEntity> Depth { get; set; } 

     [Column("timestamp", TypeName = "datetime2")] 
     public DateTime TimeStamp { get; set; } 
     public MarketDataDepthEntity() { Depth = new List<MarketDataEntity>(); } 
    } 

    public class SymbolTickDataEntity 
    { 
     public int SymbolTickDataEntityID { get; set; } 

     [Key] 
     [Required] 
     public string Symbol { get; set; } 

     public virtual IList<MarketDataDepthEntity> Mdds { get; set; } 
     public SymbolTickDataEntity() { Mdds = new List<MarketDataDepthEntity>(); } 
    } 

    public class TickDataTestContext : DbContext 
    { 
     public DbSet<MarketDataEntity> MarketDataDbSet { get; set; } 
     public DbSet<MarketDataDepthEntity> MarketDataDepthDbSet { get; set; } 
     public DbSet<SymbolTickDataEntity> SymbolTickDataDbSet { get; set; } 
    } 
+0

添加到數據庫的數據量是多少?大約在幾個時間段內有多少行? – Kevin

+0

那麼現在它是書的頂部。它每天每個符號有2,000,000行。那麼每天有5000萬行嗎?但它在寫作約十分鐘內就會降解。 – user1676605

回答

2

從您的代碼看起來像您在應用程序的整個生命週期內圍繞TickDataTestContext。因此,隨着您不斷添加數據,本地緩存不斷增加,從而導致性能下降。

您應該重寫代碼以爲每個需要保存的請求創建一個TickDataTestContext的新實例,然後執行該操作,保存更改並處理上下文。

+0

謝謝。這樣可行! – user1676605

相關問題