gpt4 book ai didi

c# - 大表查询连接超时

转载 作者:IT老高 更新时间:2023-10-29 00:08:59 26 4
gpt4 key购买 nike

我在从大表上的查询中获取数据时遇到脚本超时问题。

该表有 9,521,457 行。

我要执行的查询是:

SELECT * 
FROM `dialhistory`
WHERE `customerId` IN (22606536, 22707251, 41598836);

此查询在 HeidiSQL 上运行没有问题,大约需要 171 秒并返回 434 行。

但是当我运行我的 C# 脚本时,它会在 161 行后超时。

16:54:55: Row 1
...
16:54:55: Row 161
16:55:32: Error -> Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

这里是代码

public MySqlDatabase(string server, string database, string username, string password)
{
ConnectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + username + ";PASSWORD=" + password + ";";

}

public IQueryable<DailHistory> GetHistory(IList<int> customerIds)
{
IList<DailHistory> list = new List<DailHistory>();
var connection = new MySqlConnection(ConnectionString);
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `dialhistory` WHERE `customerId` in ("+string.Join(",", customerIds.ToArray())+")";
var reader = command.ExecuteReader();
int i = 1;
while (reader.Read())
{
Console.WriteLine(DateTime.Now.ToLongTimeString() + ": Row " + i);
i++;
try
{
var d = new DailHistory();
d.CustomerId = int.Parse((string) reader["customerId"]);
d.Agent = ParseNullAbleString(reader["agent"].ToString());
d.CallBackReason = ParseNullAbleString(reader["callBackReason"].ToString());
d.CallState = ParseCallSate(reader["callState"].ToString());
d.ContactResponse = ParseNullAbleString(reader["contactResponse"].ToString());
d.DailTime = new DailTime(reader["dialStart"].ToString(), reader["dialEnd"].ToString());
d.HistoryIndex = int.Parse(reader["historyIndex"].ToString());
d.Note = ParseNullAbleString(reader["note"].ToString());
d.OldDialNo = ParseNullAbleInt(reader["oldDialNo"].ToString());
d.ProjectJob = ParseNullAbleString(reader["projectJob"].ToString());
list.Add(d);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
reader.Close();
return list.AsQueryable();
}

最佳答案

command.CommandTimeout = int.MaxValue;

如果您更确切地知道要插入哪个数字,那就去做吧。如果您将其设置为 int.MaxValue,您将移除一个安全屏障。

关于c# - 大表查询连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4315701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com