Rob*_*Rob 2 c# mysql sql optimization
我们目前正在使用LINQ来生成SQL查询,内部有一点魔力来处理特定于案例的查询.
到目前为止,它运作良好; 非常快,几乎没有任何问题.我们最近在从数据库查询大量数据时遇到了效率问题.
我们构造查询如下:
var someIntList = new List<int> { 1,2,3,4,5 };
var query = dtx.Query.Containers.Where(c => c.ContainerID.IsIn(someIntList));
Run Code Online (Sandbox Code Playgroud)
要么
var someStringList = new List<int> {"a", "b", "c" };
query = dtx.Query.Containers.Where(c => c.BuildingName.IsIn(someStringList));
Run Code Online (Sandbox Code Playgroud)
哪个会产生(以及一堆与此无关的其他东西):
SELECT * FROM Container WHERE ContainerID IN (1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)
和
SELECT * FROM Container WHERE BuildingName IN ('a','b','c')
Run Code Online (Sandbox Code Playgroud)
现在在这种特殊情况下,我们需要返回50,000行..这是通过5个单独的查询生成的,分割负载.数据库返回相当快(几秒钟内),但生成查询需要很长时间.
这是调用生成此特定查询的最后一个函数:
private static string GetSafeValueForItem(object item)
{
if (item == null)
return "NULL";
if (item is bool)
return ((bool)item ? "1" : "0");
if (item is string)
return string.Format("'{0}'", item.ToString().Replace("'", "''"));
if (item is IEnumerable)
return ListToDBList((IEnumerable)item);
if (item is DateTime)
return string.Format("'{0}'", ((DateTime)item).ToString("yyyy-MM-dd HH:mm:ss"));
return item.ToString();
}
private static string ListToDBList(IEnumerable list)
{
var str = list.Cast<object>().Aggregate("(", (current, item) => current + string.Format("{0},", GetSafeValueForItem(item)));
str = str.Trim(',');
str += ")";
return str;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,是否有任何明显的改进可以加速字符串连接?重构代码并使用不同的实现(例如避免生成查询并直接命中数据库)不是首选,但如果它提供了很大的性能提升,那么很高兴听到.
您的聚合代码基本上是循环中的字符串连接.不要那样做.
选项:
StringBuilder