Fixing: DbLinq Slow MySql Queries, Linq to SQL

modified

The following is a fix for an issue with slow queries in DbLinq when using MySql. The problem causes very slow MySQL queries when using LINQ to SQL with DbLinq, and displays a list of ArgumentExceptions, thrown before the query is executed. The query can take up to 8-10 seconds to execute.

Here is a work-around fix for the issue:

Download SVN patch 1
Download SVN patch 2

  1. Download the latest SVN code for DbLinq at
    http://code.google.com/p/dblinq2007/source/checkout

  2. Open the following file for editing in Visual Studio 2008 DBLinq\src\DbLinq\Data\Linq\Sugar\Implementation\ExpressionOptimizer.cs

  3. On line 78, the problem manifests itself when attempting to evaluate the lambda expression. Change the function AnalyzeConstant() as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
protected virtual Expression AnalyzeConstant(Expression expression, BuilderContext builderContext)
{
    // we try to find a non-constant operand, and if we do, we won't change this expression.
    foreach (var operand in expression.GetOperands())
    {
        if (!(operand is ConstantExpression))
            return expression;
    }
    // now, we just simply return a constant with new value
// ***** COMMENTED OUT CODE *****
    /*try
    {
        var optimizedExpression = Expression.Constant(expression.Evaluate());
        // sometimes, optimizing an expression changes its type, and we just can't allow this.
        if (optimizedExpression.Type == expression.Type)
            return optimizedExpression;
    }
    // if we fail to evaluate the expression, then just return it
    catch (ArgumentException) { }*/
// ***** END COMMENTED OUT CODE *****//

    return expression;
}
  1. Re-compile the DbLinq project to get the new DbLinq.dll and reference it in your projects. The ArgumentExceptions should no longer appear and queries are executed instantly.
Share