简体   繁体   中英

C# Entity FrameWork MySQL Slow Queries Count()

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM