首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >慢SQLDataReader GetString

慢SQLDataReader GetString
EN

Stack Overflow用户
提问于 2014-12-31 21:50:28
回答 1查看 562关注 0票数 0

在一行中,rdr.GetString需要12-15秒。

几排要花4-5秒。

几个人需要1-4秒。

大多数行小于100毫秒。

整个查询只返回3 286行,在SSMS中运行时间为15秒。

值是varchar(600) --据我所知,它没有什么特别之处。

在fieldID上有一个索引,这个值是重建的(几次)

在SqlDataReader中,第208行需要12 -15秒。

读取一行所需时间与读取其余3 285行所需时间相同。

如果我将fieldID上的排序更改为desc,那么它将挂在2050行上。

而且这不是同一行(甚至不是相同的fieldID)

在rdr.GetString之前是一个rdr.GetByte,它从不挂在GetByte上。它已经排好了!即使碰到另一台具有类似但不是精确数据库的服务器,它也会挂起。

我知道这听起来很疯狂,但它正在发生。感觉就像SqlDataReader挂起了,但我在这个应用程序中一直使用SqlDataReader,返回的行比这个多得多。

如果我执行手动GC.Collect(),当fieldID更改时,它仍然挂在大多数相同的行上。个人挂起的时间稍微小一点,但总数大致相同。使用1秒的阈值可能会有一个刚刚进入或刚刚退出的阈值,但是问题行肯定会重复。

认为这可能与挂起之间的字符数有关,但可能低至600个,最高可达6万个。

但它似乎确实与返回的数据有关。如果我排除或包括rownumber,它将挂在不同的行上。但这两行都在同一附近。

代码语言:javascript
复制
 fieldID = rdr.GetByte(0);    // this line does not hang
 delta = sw.ElapsedMilliseconds;
 textValue = rdr.GetString(1);  // this is the line that hang on some rows 
 if ((sw.ElapsedMilliseconds - delta) > 1000L)
    Debug.WriteLine("GabeLib_Helper sw in  fields  thisFieldID = " + thisFieldID + " counter = " + counter + " ccount = " + ccount + " after getstring read delta = " + (sw.ElapsedMilliseconds - delta).ToString("N0") + " textValue = " + textValue);

select [fieldID], [value], [rowNum]
from 
(
SELECT [fieldID], ltrim(rtrim([value])) as [value]
     , ROW_NUMBER() over ( partition by [fieldID] order by ltrim(rtrim([value])) ) as [rowNum]
  FROM [docSVtext] with (nolock)
  JOIN [docFieldDef] with (nolock)
    ON [docFieldDef].[ID] = [fieldID] 
   AND [docFieldDef].[typeID] in (101) 
   AND [docFieldDef].[active] =  'true' 
   AND len(ltrim(rtrim([value]))) > 0 and len(ltrim(rtrim([value]))) <= 200 
  JOIN [docSVsys] with (nolock) 
    on [docSVsys].[sID] = [docSVtext].[sID] 
   and [docSVsys].[visibility] = 0 
 group by [fieldID], ltrim(rtrim([value]))
 ) as withRow
 where [rowNum] < 1001
 order by [fieldID], [rowNum]

如果我加上rdr.GetInt64(2);在rdr.GetString(1)上面;

然后它挂在rdr.GetInt64(2)上,而不挂在rdr.GetString(1)上;

对于rdr.GetInt64(2),我得到了一个缓慢的rdr.Read()。

代码语言:javascript
复制
delta = sw.ElapsedMilliseconds; 
rowNum = rdr.GetInt64(2);
if ((sw.ElapsedMilliseconds - delta) > 1000L)
    Debug.WriteLine("GabeLib_Helper sw in  fields  in PastEntries new rowNum  rdr.GetInt64(2) " + rowNum + " counter = " + counter + " ccount = " + ccount + "  delta = " + (sw.ElapsedMilliseconds - delta).ToString("N0"));

下面的查询在2秒内在SSMS中运行。

这不是问题查询

包含以显示索引似乎正常工作。

代码语言:javascript
复制
  select fieldID, value, count(*)  
  from docSVtext 
  group by fieldID, value

我跟踪了GC,这与减速没有关系

即使GC运行,也只需要20 ms。

代码语言:javascript
复制
delta = sw.ElapsedMilliseconds;
List<int> gcCounts = new List<int>();
for (int g = 0; g <= GC.MaxGeneration; g++) gcCounts.Add(GC.CollectionCount(g));
textValue = rdr.GetString(1);  //  " Chen, Andy </O=ENRON/OU=NA/CN=RECIPIENTS/CN=ACHEN>" + Guid.NewGuid(); //
if ((sw.ElapsedMilliseconds - delta) > 100L)
{
    Debug.WriteLine("GabeLib_Helper sw in  fields  thisFieldID = " + thisFieldID + " counter = " + counter + " ccount = " + ccount + " after getstring read delta = " + (sw.ElapsedMilliseconds - delta).ToString("N0") + " textValue = " + textValue);

}
for (int g = 0; g <= GC.MaxGeneration; g++)
{
    if (GC.CollectionCount(g) != gcCounts[g])
        Debug.WriteLine("GabeLib_Helper GC new count = " + GC.CollectionCount(g) + " old count =" + gcCounts[g] + " generation " + g + " ccount = " + ccount + " after getstring read delta = " + (sw.ElapsedMilliseconds - delta).ToString("N0"));
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-05 21:12:30

我想我想出来了

如果我移除

代码语言:javascript
复制
where [rowNum] < 1001

然后是没有任何长行读取。

查询所需时间更长,因为它现在读取每一行。

挂起是当它跳过那些行的时候

因为实际的读者领先于阅读,所以很难看出

但是在SSMS中没有任何问题仍然困扰着我。

试过

代码语言:javascript
复制
select ID, count(*) 
  from table 
  ...
group by ID 
having count(*) > x

只查询x计数下的ID。

问题是查询花了几秒钟时间运行。

我最后做的是

代码语言:javascript
复制
select top (x + 1), value 
  from table 
 where ID = i1
 group by value;
select top (x + 1), value 
  from table 
 where ID = i2
 group by value;
...

如果计数上升到x+1,我就不用它了

正在发生的是一些非常重要的事情,在那里消耗了大量的时间,而我甚至不想要这些。即使算上一个大数目也是很昂贵的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27726621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档