在从大型表上查询数据时,我遇到了脚本超时的问题.
该表有9,521,457行.
我正在尝试预先形成的查询是:
SELECT *
FROM `dialhistory`
WHERE `customerId` IN (22606536, 22707251, 41598836);
Run Code Online (Sandbox Code Playgroud)
此查询在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.
Run Code Online (Sandbox Code Playgroud)
这是代码
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();
}
Run Code Online (Sandbox Code Playgroud)
Jan*_*rre 30
command.CommandTimeout = int.MaxValue;
Run Code Online (Sandbox Code Playgroud)
如果您更准确地知道要插入的数字,请执行此操作.如果将其设置为int.MaxValue,则表示您正在删除安全屏障.
Joh*_*ock 12
在命令对象上设置CommandTimeout
var command = connection.CreateCommand();
command.CommandTimeout = 0;
//zero specifies never timeout.
//Any number greater than zero is the number of seconds before
//the command will time out.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36160 次 |
| 最近记录: |