Mysql Query Plan different in PHP compared to Query Plan generated via Navicat

84 Views Asked by At

I am quite confused at the time of posting this. I do not understand why the query plan for the query is different when I fire the EXPLAIN in php vs firing the EXPLAIN for the EXACT same query in navicat.

Query:

SELECT 
    MAX(`SaleID`) AS 'LatestSaleID',
    MAX(`SaleDate`) AS 'MyLastVisit',
    COUNT(SaleID) AS 'MyNumVisits'                          
FROM sales_table
WHERE sales_table.CustomerID = '5207'

Explain output in Navicat:

  • id - 1
  • select_type - SIMPLE
  • table - sales_table
  • partitions - NULL
  • type - ref
  • possible_keys - CustomerID
  • key - CustomerID
  • key_len - 22
  • ref - const
  • rows - 1
  • filtered - 100
  • Extra - NULL

Explain output when fired from PHP:

  • id - 1
  • select_type - SIMPLE
  • table - sales_table
  • partitions - NULL
  • type - ALL
  • possible_keys - CustomerID
  • key - NULL
  • key_len - NULL
  • ref - NULL
  • rows - 1772719
  • filtered - 10.00
  • Extra - Using where

As you can see, the explain output is vastly different. Due to this difference, the query takes 4 times longer to execute from my PHP page.

I tried using a forced index. However, that did not work either.

SELECT 
    MAX(`SaleID`) AS 'LatestSaleID',
    MAX(`SaleDate`) AS 'MyLastVisit',
    COUNT(SaleID) AS 'MyNumVisits'                          
FROM sales_table USE INDEX(CustomerID)
WHERE sales_table.CustomerID = '5207'

I found a related post - PHP takes 90x longer to run query than MySQL client. However, I have not been able to find any information regarding query plan differences.

This is a legacy app which is still using mysql_query.

  • PHP Version - 5.6.34-1+ubuntu16.04.1+deb.sury.org+1
  • Mysqlnd version - mysqlnd 5.0.11-dev - 20120503
0

There are 0 best solutions below