Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 976
Matthew M. Avatar asked May 21 '10 02:05

Matthew M.


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

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.

like image 200
Devart Avatar answered Oct 12 '22 10:10

Devart