So there's a few posts around complaining about the performance of the MySQL plugin for Entity Framework 6. Most of these, however, seem to come down to it generating bad SQL. I'm suffering with this issue but it seems to be due to a performance lag due to the plugin itself.
Here's my query in LINQ:
List<Address> matches = _rep.GetAddresses(s => s.AddressKey == cleanAddress).ToList();
And in the repository (_rep
) I have this:
public IQueryable<Address> GetAddresses(Expression<Func<Address, bool>> query)
{
//var foo = Addresses.AsNoTracking().Where(query);
//var bar = foo.ToString();
return Addresses.AsNoTracking().Where(query);
}
So I'm already using AsNoTracking to try and improve performance. The commented out lines are there so I can see the SQL that's being generated, which turns out to be:
SELECT
`Extent1`.`AddressId`,
`Extent1`.`AddressKey`,
`Extent1`.`NameKey`,
`Extent1`.`Title`,
`Extent1`.`Forename`,
`Extent1`.`Surname`,
FROM `Addresses` AS `Extent1`
WHERE (`Extent1`.`AddressKey` = @p__linq__0)
OR ((`Extent1`.`AddressKey` IS NULL) AND (@p__linq__0 IS NULL))
Simple enough. Worth noting that AddressKey is a varchar(255) column with an index.
Now, here's the thing. If I stick that query into MySQL workbench and run it (with a varied value of @p__linq__0
) it doesn't even register the run time. It lists the duration as 0.000 seconds.
However putting a Stopwatch around my query and logging the time taken to execute the Linq works out around 0.004 seconds. Not a big difference you might think, but this is part of a speed-critical application that runs this code millions of times over. It soon adds up.
I have the same issue with a later block of upsert code. Run natively in workbench and it's under a milisecond. Again, via EF, it takes 3-4 miliseconds.
Am I right in thinking this is down to weak design in the EF MySQL plugin? If so, can I presume that I'll run into the same problem if I try and change this to run via a stored procedure or submitting SQL directly with ExecuteSqlCommand()
?
Is there anything else I can try to clear up this performance lag?
DbReader
just to figure out if the problem is in EF and or MySQL plugin, but honestly I think your requirements/expectations are too high.