PDO - a specific query is drastically slow compared to manual execution

Viewed 281

I'm experiencing a drasticily slow query execution with a specific query (written below).

The weird thing is, when running the query manually (using phpMyAdmin), it only takes a second to complete, while running it from PHP with PDO, it takes almost 10 minutes!

Other queries execute normally (equal durations between PDO & phpMyAdmin).

Technical details:

  • I'm using Google's AppEngine & Cloud SQL.
  • Running PHP5.5
  • The app engine instance executing the php code isn't busy.
  • The query results with about 10,000 rows

The query I'm running:

SELECT s.saleID,s.year,s.kilometrage,s.hand,s.price,s.pictures,u.platform,s.date,
   (SELECT AVG(price) FROM sales s2 WHERE s2.carID = s.carID AND s2.year = s.year HAVING COUNT(s2.saleID)>5) AS avgPrice
   FROM sales s JOIN users u ON u.ID = s.sellerID LEFT JOIN sold so ON s.saleID = so.saleID WHERE so.saleID IS NULL

Any hints on this issue? It can't be related to indexes since the query runs perfectly well under phpMyAdmin.

Edit

In case someone is also having this - seems like PMA adds an implicit LIMIT on the query for paging, which makes everything runs faster in some cases.

1 Answers
Related