20

I've been troubleshooting this complex query today, thinking it was a performance issue. It appears that the query is getting stuck into the 'Statistics' state. It is currently in that state for 1300 seconds.

I've checked the indexes for the tables involved -- I've optimized the table -- what could be causing this hang?

SELECT
    Import_Values.id,
    Import_Values.part_id,
    Import_Values.qty,
    Import_Values.note,
    Parts.partterminologyname,
    GROUP_CONCAT(BaseVehicle.YearID, ' ', Make.MakeName, ' ', Model.modelname, ' ', SubModel.SubModelName SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineDesignation.EngineDesignationName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineVIN.EngineVINName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.Liter) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.CC) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.CID) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.Cylinders) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.BlockType) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.EngBoreIn) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.EngBoreMetric) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.EngStrokeIn) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineBase.EngStrokeMetric) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(FuelDeliveryType.FuelDeliveryTypeName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(FuelDeliverySubType.FuelDeliverySubTypeName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(FuelSystemControlType.FuelSystemControlTypeName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(FuelSystemDesign.FuelSystemDesignName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(Aspiration.AspirationName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(CylinderHeadType.CylinderHeadTypeName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(FuelType.FuelTypeName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(IgnitionSystemType.IgnitionSystemTypeName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(Mfr.MfrName) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(EngineVersion.EngineVersion) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(Valves.ValvesPerEngine) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(BedLength.BedLength) SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT(BedLength.BedLengthMetric) SEPARATOR ', ')
    FROM 
    Import_Values
    INNER JOIN BaseVehicle 
        ON Import_Values.base_vehicle_id=BaseVehicle.BaseVehicleID
    INNER JOIN Parts 
        ON Import_Values.part_type_id=Parts.PartTerminologyID
    INNER JOIN Make
        ON BaseVehicle.MakeID=Make.MakeID
    INNER JOIN Model
        ON BaseVehicle.ModelID=Model.ModelID
    INNER JOIN Vehicle
        ON Import_Values.base_vehicle_id=Vehicle.BaseVehicleID
    INNER JOIN SubModel
        ON Vehicle.SubModelID=SubModel.SubModelID
    INNER JOIN VehicleToEngineConfig
        ON Vehicle.VehicleID=VehicleToEngineConfig.VehicleID
    INNER JOIN EngineConfig
        ON VehicleToEngineConfig.EngineConfigID=EngineConfig.EngineConfigID
    INNER JOIN EngineDesignation
        ON EngineConfig.EngineDesignationID=EngineDesignation.EngineDesignationID
    INNER JOIN EngineVIN
        ON EngineConfig.EngineVINID=EngineVIN.EngineVINID
    INNER JOIN EngineBase
        ON EngineConfig.EngineBaseID=EngineBase.EngineBaseID
    INNER JOIN FuelDeliveryConfig
        ON EngineConfig.FuelDeliveryConfigID=FuelDeliveryConfig.FuelDeliveryConfigID
    INNER JOIN FuelDeliveryType
        ON FuelDeliveryConfig.FuelDeliveryTypeID=FuelDeliveryType.FuelDeliveryTypeID
    INNER JOIN FuelDeliverySubType
        ON FuelDeliveryConfig.FuelDeliverySubTypeID=FuelDeliverySubType.FuelDeliverySubTypeID
    INNER JOIN FuelSystemControlType
        ON FuelDeliveryConfig.FuelSystemControlTypeID=FuelSystemControlType.FuelSystemControlTypeID
    INNER JOIN FuelSystemDesign
        ON FuelDeliveryConfig.FuelSystemDesignID=FuelSystemDesign.FuelSystemDesignID
    INNER JOIN Aspiration
        ON EngineConfig.AspirationID=Aspiration.AspirationID
    INNER JOIN CylinderHeadType
        ON EngineConfig.CylinderHeadTypeID=CylinderHeadType.CylinderHeadTypeID
    INNER JOIN FuelType
        ON EngineConfig.FuelTypeID=FuelType.FuelTypeID
    INNER JOIN IgnitionSystemType
        ON EngineConfig.IgnitionSystemTypeID=IgnitionSystemType.IgnitionSystemTypeID
    INNER JOIN Mfr
        ON EngineConfig.EngineMfrID=Mfr.MfrID
    INNER JOIN EngineVersion
        ON EngineConfig.EngineVersionID=EngineVersion.EngineVersionID
    INNER JOIN Valves
        ON EngineConfig.ValvesID=Valves.Valvesid
    INNER JOIN VehicleToBedConfig
        ON Vehicle.VehicleID=VehicleToBedConfig.VehicleID
    INNER JOIN BedConfig
        ON VehicleToBedConfig.BedConfigID=BedConfig.BedConfigID
    INNER JOIN BedLength
        ON BedConfig.BedLengthID=BedLength.BedLengthID
    GROUP BY part_id
7
  • Q: Have you run an EXPLAIN to get details on the execution plan? If any of these are MyISAM tables, I'd recommend a CHECK TABLE x; If any problems are reported, then REPAIR TABLE x; (BUT BEWARE: this could result in data loss. It's advisable to take a cold backup of the table, as well as a mysqldump of the table, before the repair) and gather statistics with ANALYZE TABLE x;. Jul 22, 2013 at 20:53
  • I have been doing so, yes. I thought it was a query issue so I added JOIN by JOIN to see where the issue was. Right now I'm trying to run EXPLAIN SELECT on this query and it's at 100 seconds (thus far) in Statistics. Jul 22, 2013 at 21:01
  • sounds like MySQL may be cranking to gather statistics on the tables and indexes. Jul 22, 2013 at 21:08
  • Hey Spencer. Is there a way to speed it up? Jul 22, 2013 at 21:14
  • I'd make sure statistics are up to date. If not, ANALYZE TABLE x;. This query has a lot of tables, so there are a lot of join paths to be considered. Try getting a query with fewer tables to run. Jul 22, 2013 at 21:22

5 Answers 5

48

I ran into the same problem recently: MySQL started to choke (stuck in state 'statistics') on queries with a lot of tables joined together. I found a good blog post explaining why this happens and how to solve it.

Basically at least in MySQL 5.5, the default value for the configuration parameter optimizer_search_depth is 62 which will cause the query optimizer to take exponentially more time as the number of tables in the query increases. After a certain point it will start to take days or even longer to finish the query.

However, if you set the optimizer_search_depth to 0 in your my.cnf, MySQL automatically chooses suitable depth, but limits it to 7 which is not yet too complex.

Apparently this issue has been fixed in MySQL 5.6 but I haven't tested it myself yet.

5
  • 9
    Thank you! I can say that I am working with v5.7 and I've had this same issue, so I guess it's not solved yet.
    – Unapedra
    Nov 2, 2016 at 10:31
  • 2
    Same issue with MariaDB 10.0.31 (Ubunti 16.04), optimizer_search_depth = 0 fixed it. Thank you! Nov 3, 2017 at 11:07
  • 1
    The issue persists in the most recent MariaDB (10.3.12) too. My multi-day query now runs in 40 ms. Awesome! Jan 18, 2019 at 23:53
  • 1
    We had the same problem running v5.6.49 but fortunately, after setting the optimizer_search_depth = 7 the problem has gone.
    – frommelmak
    Feb 2, 2021 at 10:17
  • We had dozens of totally hanging queries with MariaDB 10.6. It seemed that queries that joined same table multiple tables in sub views were problem. Optimizer wasn't even able to tell query plan with EXPLAIN, but hanged for seemingly infinite time. Setting optimizer_search_depth to 0 fixed them all instantly, thanks!! Jan 3 at 11:23
1

I had the same issue on a Centos server 4 cores and 180GB ram. A simple query was taking forever to run and stuck on statistics state just like what you have explained. I ran OPTIMIZE TABLE on the tables it was querying and like 5 minutes later problem was solved. However, I never found out if it really solved the issue or not.

Just a suggestion.

0

I spent the day yesterday dealing with a similarly troublesome query...

Another option to quickly eliminate query optimizer hangs is to add STRAIGHT_JOIN just after SELECT.

For example:

SELECT STRAIGHT_JOIN
table1.column_a,
table1.column_b,
table2.column_a,
table2.column_b
FROM table1
JOIN table2 USING(column_a)

This is not an ideal solution, since it is entirely bypassing query optimization for joins, but if you're stuck solving a mission-critical problem in a legacy application like I was yesterday, it might help. I do not recommend slapping this on every query, but it's an effective solution in some cases.

0

In my case it was a huge database - many databases, many tables. After startup Mariadb was working slowly, all the queries not executing fast enough but stuck on "statistics". I did 3 things:

  • increased file limit to 4000000 in etc/systemd/system/multi-user.target.wants/mariadb.service
  • increased open_files_limit and table_definition_cache to about 2500000
  • changed table_open_cache and table_definition_cache to about 3000
  • installed "libjemalloc" for Mariadb as memory usage was still growing.

That solved the problem. Of course you don't have to set these numbers so high, that depends on your number of databases, tables & files.

0

Ran into this, and showed that /tmp was just full because someone downsized it...

[Warning] Disk is full writing '/tmp/#sql_e31_0.MYI' (Errcode: 28 - No space left on device). Waiting for someone to free space...

Resized /tmp und restartet, fixed.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.