缓慢插入但快速选择查询

时间:2019-08-29 14:55:08

标签: sql sql-server

带有连接的选择查询返回结果的时间少于1秒(返回1000行:600 ms),但是插入到临时表或物理表中则需要15-16秒。

  • 经过测试的io性能:在没有任何连接的情况下进行选择或插入需要不到一秒的时间才能写入1000行。
  • 尝试跟踪标志1118
  • 尝试在临时表上添加聚簇索引,并使用tablock和maxdop提示插入。

这些性能都没有改善。

感谢您的所有评论。需要每5秒从Kafka中插入6000到20000行...

  • 我使用表类型变量将数据从kafka获取到sql服务器中
  • 将其作为参数传递给存储过程
  • 将此数据与其他表一起加载到临时表#table
  • 我使用#table将数据合并到应用程序表中

找到了一种变通办法,可以帮助我实现目标的周转时间,但不完全了解该行为的原因。正如我在问题语句中提到的那样,瓶颈在于编写select语句的结果集,该结果集将表变量与其他各种表连接到temp表。 我将其放入存储的产品中,并将存储的proc的执行返回到临时表。现在插入时间不到1秒


SELECT  
   i.Id AS IId, 
   df.Id AS dfid,
   MAX(CASE
                 WHEN lp.Value IS NULL and f.pnp = 1 THEN 0
          WHEN lp.Value = 0 and f.tzan = 1 and f.pnp = 0 THEN NULL                                          
          ELSE lp.Value                                          
   END) 'FV',   
   MAX(lp.TS),
   MAX(lp.Eid),
   MAX(0+lp.IsDelayedStream)
   FROM         
     f1 f WITH (NOLOCK)  
     INNER JOIN ft1 ft WITH (NOLOCK)  ON f.FeedTypeId = ft.Id         
     INNER JOIN FeedDataField fdf WITH (NOLOCK)        
      ON fdf.FeedId = f.Id        
     INNER JOIN df1 df WITH (NOLOCK)     
      ON fdf.dfId = df.Id        
     INNER JOIN ds1 ds  WITH (NOLOCK)    
      ON df.dsid = ds.Id        
     INNER JOIN dp1 dp  WITH (NOLOCK)  
      ON ds.dpId = dp.Id  
     INNER JOIN dc1 dc WITH (NOLOCK)  
      ON dc.dcId = ds.dcId      
     INNER JOIN i1 i  WITH (NOLOCK)  
      ON f.iId = I.Id 
     INNER JOIN id1 id WITH (NOLOCK)  
      ON id.iId = i.Id
     INNER JOIN IdentifierType it WITH (NOLOCK)  
      ON id.ItId = it.Id    
     INNER JOIN ivw_Tdf tdf WITH(NOEXPAND)  
      ON tdf.iId = i.Id 
     INNER JOIN z.dbo.[tlp] lp
      ON lp.Ticker = id.Name AND lp.Field = df.SourceName AND 
      lp.Contributor = dc.Name AND lp.YellowKey = tdf.TextValue
      WHERE         
        ft.Name in ('X', 'Y') AND f.SA = 1        
AND dp.Name = 'B' AND (i.Inactive IS NULL OR i.Inactive = 0)        
AND it.Name = 'T' AND id.ValidTo = @InfinityDate         
AND tdf.SourceName = 'MSD'        
AND tdf.ValidTo = @Infinity
GROUP BY i.Id, df.Id
OPTION(MAXDOP 4, OPTIMIZE FOR (@Infinity = '9999-12-31 23:59:59', 
@InfinityDate = '9999-12-31))

0 个答案:

没有答案