0

Currently trying to figure out a performance issue we're having with our application running on EF5, connected with MySQL DB (DB first approach) inside an ASP.NET MVC project.

Nothing complicated with the DB - 10 or so tables with only a tiny bit of data (talking about under 50 rows). Each table has a PK and there's a few FKs around - nothing dramatic or unusual. My experience with EF or MySQL is not abundant, but I've had some previous mssql and plain sql experience and have not really had too many dramas there.

I've tried investigating the issue myself and this is what I've found when comparing EF using LINQ vs EF using plain SQL

  • The higher the query complexity, the bigger the difference between LINQ vs Plain SQL executed through EF
  • Originally I suspected that EF could be creating highly un-optimized queries, which does not seem to be the case (proven by intercepting the auto-generated query as plain SQL, which was picked up through MySql log)
  • It looks like the time taken by EF LINQ queries seems to be taken up constructing the actual query, for the lack of a better explanation, but I have no way of actually figuring that out

My understanding is that unless there is something going seriously wrong with EF or our DB schema, then there should be no way that EF should be taken 200 ms to construct a basic query:

    // EF LINQ ~250ms
    var joinCountEf =
    (from item in dbContext.item
     join user in dbContext.user on item.userID
     equals user.ID
     where item.ID > 0
     select item).Count();

    // EF SQL ~5ms
    var joinCountEfSql = 
    dbContext.Database
    .Sqlitem<int>("SELECT count(*) FROM item INNER JOIN user ON user.ID = item.userID WHERE item.ID > 0;")
    .First();

The EF generated SQL looks like this:

 SELECT
    `GroupBy1`.`A1` AS `C1`
    FROM (SELECT
    COUNT(1) AS `A1`
    FROM `item` AS `Extent1` INNER JOIN `user` AS `Extent2` 
    ON `Extent1`.`userID` = `Extent2`.`ID`
     WHERE `Extent1`.`ID` > 0) AS `GroupBy1`

Any ideas whatsoever?

Web Dev
  • 2,677
  • 2
  • 30
  • 41
  • What's the error, what's the question? The query you are executing isn't complicated at all, I'd expect that to execute faster that the blink of an eye. Unless there's zillions of items and users in there and the query produced isn't using the indexes effectively. – Luke Mar 30 '16 at 10:49
  • There is no error, however the LINQ performance is terrible when compared to straight up SQL, resulting in 50-100x slower results – Web Dev Mar 30 '16 at 10:52
  • Hmm ok. All I can really say is that's the trade off when you're using an ORM. – Luke Mar 30 '16 at 10:56
  • I understand that there will be a trade off, however, surely it should not be taking 200ms to construct a simple join between 2 tables, unless EF is the most inefficient piece of garbage ever, which I do not believe it is – Web Dev Mar 30 '16 at 11:00
  • How are you measuring the execution speed? – Luke Mar 30 '16 at 11:03
  • Maybe it's slow cause your [machine is running at 50-90% usage](http://stackoverflow.com/questions/34756886/vs2013-vs-vs2015-cpu-usage) xD – Luke Mar 30 '16 at 11:11
  • I wish that was the case! Measuring using the in-build profiler in VS2015 - seems pretty spot on – Web Dev Mar 30 '16 at 11:19
  • What is an acceptable execution time for this query using EF? Also have you tried profiling the speed of the execution in complied code in a production environment instead of just on your machine with VS running? – Luke Mar 30 '16 at 11:20
  • How does the generated SQL look? – JB06 Mar 30 '16 at 17:14
  • I'm not sure, but in my opinion it should not be more than 5-10ms for a query that takes 5ms to execute and a simple DB structure. I've only tried profiling in debug, but the solution sitting on the staging server is showing the same signs. It's also the same case for a few other devs that tried this out.. I've attached the generated SQL to the bottom of the original question at the top - looks pretty straight forward and executing that directly through EF SQL (rather than EF LINQ) produces results similar to executing it directly in SQL Workbench – Web Dev Mar 31 '16 at 00:38

0 Answers0