SQL server在使用c#检索数据时非常慢
本文关键字:数据 非常 检索 server SQL | 更新日期: 2023-09-27 18:15:44
从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检索数据:
我有一个每年的表,所以我必须查看所有这些表。
但是我在所有数据库中有1000000行。
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语句不接收所有的数据。
有时我也有一个系统内存不足的异常。
谁有解决这些问题的办法?您正在运行查询,直到有40个错误,这意味着您将继续运行查询,直到有40个异常。当内存不足时将出现异常……这意味着您将在崩溃之前检索大量数据,这将需要一段时间。
去掉while,你就没事了。
还是你想要40块果酱?将aartaltaben更改为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
在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();
}
}