views:

198

answers:

1

Hello,

I'm having a serious issue with MySQL and Entity Framework 4.0. I have dropped a Table onto the EF Designer surface, and everything seems OK. However, when I perform a query in the following fashion:

using(entityContext dc = new entityContext()) {
  int numRows = dc.myTable.Count();
}

The query that is generated looks something like this:

SELECT `GroupBy1`.`A1` AS `C1`
FROM   (SELECT Count(1) AS `A1`
        FROM   (SELECT `pricing table`.`a`,
                       `pricing table`.`b`,
                       `pricing table`.`c`,
                       `pricing table`.`d`,
                       `pricing table`.`e`,
                       `pricing table`.`f`,
                       `pricing table`.`g`,
                       `pricing table`.`h`,
                       `pricing table`.`i`
                FROM   `pricing table` AS `pricing table`) AS `Extent1`) AS `GroupBy1`

As should be evident, this is an excruciatingly unoptimized query. It is selecting every single row! This is not optimal, nor is it even possible for me to use MySQL + EF at this point.

I have tried both the MySQL 6.3.1 [that was fun to install] and DevArt's dotConnect for MySQL and both produce the same results. This table has 1.5 million records.. and takes 6-11s to execute!

What am I doing wrong ? Is there any way to optimize this [and other queries] to produce sane code like:

SELECT COUNT(*) FROM table

?

Generating the same query using SQLServer takes virtually no time and produces sane code.

Help!

Edit: I would also like to point out that I switched to the DevArt dotConnect MySQL LINQ to SQL driver and using L2S over EF is 1000000x faster. This includes queries as well.

Selecting anything in EF seems to generate completely bonkers queries.

dc.pricing_table.OrderBy(j => j.a).Skip(100).Take(100).ToList();

SELECT   `Extent1`.`a`,
         `Extent1`.`b`,
         `Extent1`.`c`,
         `Extent1`.`d`,
         `Extent1`.`e`,
         `Extent1`.`f`,
         `Extent1`.`g`,
         `Extent1`.`h`,
         `Extent1`.`i`
FROM     (SELECT `pricing table `.`a`,
                 `pricing table `.`b`,
                 `pricing table `.`c`,
                 `pricing table `.`d`,
                 `pricing table `.`e`,
                 `pricing table `.`f`,
                 `pricing table `.`g`,
                 `pricing table `.`h`,
                 `pricing table `.`i`
          FROM   `pricing table ` AS `pricing table`) AS `Extent1`
ORDER BY `a` ASC
LIMIT    100,100

Again, a completely bassackwards wrong query. The LIMIT 100,100 is in absolutely the wrong place. This of course, won't work for me at all.

Thanks!

  • Matthew
+4  A: 

The problem may be associated with the fact that you have Defining Query in your .edmx or .edml file.
Designer generates DefiningQuery often in case you have View, or your Table does not have Primary Key defined. Please check the XML code of your model and remove DefiningQuery in case it is present but unnecessary.

Devart
+1. And if you don't have a PK on a table, add it. If you don't have a key defined in EDMX for a view, add that, too.
Craig Stuntz
OH wow! Is there any way to set this/avoid this in the designer, or is editing always a requirement ? You were absolutely right, there was a DefiningQuery which was getting passed in. This is what I get for being an EF Newb. The L2S MySQL worked great though, which is what I did while waiting. Thanks Devart!
Matthew M.
Followup - Tested, and this was indeed the issue. I would totally give you more points if I could. Thank you for pointing this out. Hopefully there is some other way than editing the XML for the EDMX, but I'm happy knowing what the issue is and this solution for it.
Matthew M.