使用C#检索数据时,SQL Server速度很慢

时间:2013-05-29 07:33:05

标签: c# sql sql-server

从我的SQL Server检索数据非常慢。有没有人看到问题?

创建SQL Server表:

using (SqlCommand cmd = connection.CreateCommand())
{
    cmd.Parameters.AddWithValue("jaar", jaartal);
    cmd.CommandText =
        @" 

       BEGIN
       CREATE TABLE " + jaartal + @"(
        [code] [Varchar](max) NULL,
        [type] [Varchar](max) NULL,
        [datum] [datetime] NULL,
        [uur] [Varchar](max) NULL,
        [controller] [Varchar](max) NULL,
        [fout] [Varchar](max) NULL,
        [teller] [Varchar](max) NULL,
        [omschrijving] [Varchar](max) NULL,
        [graad] [Varchar](max) NULL,
        [var1] [Varchar](max) NULL,
        [var2] [Varchar](max) NULL,
        [var3] [Varchar](max) NULL,
        [var4] [Varchar](max) NULL,
        [var5] [Varchar](max) NULL,
        [var6] [Varchar](max) NULL,
        [var7] [Varchar](max) NULL,
        [var8] [Varchar](max) NULL,
        [var9] [Varchar](max) NULL,
        [var10] [Varchar](max) NULL,
        [var11] [Varchar](max) NULL,
        [var12] [Varchar](max) NULL,
        [var13] [Varchar](max) NULL,
        [var14] [Varchar](max) NULL,
        [var15] [Varchar](max) NULL,
        [var16] [Varchar](max) NULL,
        [var17] [Varchar](max) NULL,
        [var18] [Varchar](max) NULL,
        [var19] [Varchar](max) NULL,
        [var20] [Varchar](max) NULL,
        [var21] [Varchar](max) NULL,
        [var22] [Varchar](max) NULL,
        [var23] [Varchar](max) NULL,
        [var24] [Varchar](max) NULL,
        [var25] [Varchar](max) NULL,
        [var26] [Varchar](max) NULL,
        [var27] [Varchar](max) NULL,
        [var28] [Varchar](max) NULL,
        [var29] [Varchar](max) NULL,
        [var30] [Varchar](max) NULL,
        [var31] [Varchar](max) NULL,
        [var32] [Varchar](max) NULL,
        [var33] [Varchar](max) NULL,
        [var34] [Varchar](max) NULL,
        [var35] [Varchar](max) NULL,
        [var36] [Varchar](max) NULL,
        [var37] [Varchar](max) NULL,
        [var38] [Varchar](max) NULL,
        [var39] [Varchar](max) NULL,
        [var40] [Varchar](max) NULL,
        [Cat_MMAP] [Varchar](max) NULL
        ); END";

    try
    {           
        cmd.ExecuteNonQuery();
    }
    catch
    { 
    }
}

connection.Close();

从SQL Server检索数据:

我每年都有一张桌子所以我必须查看所有这些表格。

但我在所有数据库中都有1000 000行。

Tbl_events.Columns.Clear();

aantalfouten = 0; //numbers of error 
jaartaltabel = DataContainer.jaartalstart;
statusbalk.Value=statusbalk.Minimum;

while (aantalfouten < 40)
{
    try
    {
        statusbalk.Visible = true;

        tabelnaam = "Evenementen" + jaartaltabel.ToString(); //create name of the table that i want to read so as Events2013                       
        connection.Open(); 
        datatabel.Load(new SqlCommand("SELECT type,datum,uur,controller,fout,teller,omschrijving,graad,var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11,var12,var13,var14,var15,var16,var17,var18,var19,var20,var21,var22,var23,var24,var25,var26,var27,var28,var29,var30,var31,var32,var33,var34,var35,Cat_MMAP  FROM " + tabelnaam + " where " + filter + "", connection).ExecuteReader());//Select statement
        connection.Close();

        Tbl_events.DataSource = datatabel.DefaultView; //insert the data in my datagridview this takes also a  lot of time
        jaartaltabel++;
    }
    catch
    {
        jaartaltabel++;
        aantalfouten++;
        connection.Close();
    }
}
ColumnWidth();

我还使用带有where语句的过滤器来接收所有数据。

有时我还有一个系统内存不足的例外。

有没有人能解决这些问题?

2 个答案:

答案 0 :(得分:3)

您正在运行查询,直到您有40个错误,这意味着您继续运行查询,直到您有40个异常。当你的内存不足时就会出现异常......这意味着你会在崩溃之前检索大量数据,这需要一段时间。

删除时间,你会没事的。

或者你想获得40 jaartabellen?将aantalfouten改为jaartaltabel:

 while (aantalfouten < 40) ====> while (jaartaltabel < 40)

我认为您的过滤器位于表的[datum] [datetime] NULL列上,因此如果您的表中有大量数据,则在基准列上添加索引可以改善您的查询(在创建表的末尾)查询)。

CREATE NONCLUSTERED INDEX IX_"+jaartal+" ON "+jaartal+" 
(
     datum
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW

答案 1 :(得分:1)

如果一切正常,在while循环中

使用break;走出循环

 while (aantalfouten < 40)
{
   try
   {
       statusbalk.Visible = true;

       tabelnaam = "Evenementen" + jaartaltabel.ToString(); //create name of the table that i want to read so as Events2013                       
       connection.Open(); 
       SqlCommand sqlcmd = new SqlCommand("SELECT type,datum,uur,controller,fout,teller,omschrijving,graad,var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11,var12,var13,var14,var15,var16,var17,var18,var19,var20,var21,var22,var23,var24,var25,var26,var27,var28,var29,var30,var31,var32,var33,var34,var35,Cat_MMAP  FROM " + tabelnaam + " where " + filter + "", connection);//Select statement
       SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
       DataSet ds = new DataSet();
       adp.Fill(ds);

       connection.Close();

       Tbl_events.DataSource = ds; //insert the data in my datagridview this takes also a  lot of time
       jaartaltabel++;
       break;
   }
   catch
   {
      jaartaltabel++;
      aantalfouten++;
      connection.Close();
   }
  }