79192004

Date: 2024-11-15 10:39:35
Score: 0.5
Natty:
Report link

The issues you are encountering arise from the complexity of the query, the extensive use of EXISTS subqueries, and potential inefficiencies in indexing and optimisation within MySQL.

What Causes MySQL to Stall in the "Statistics" State?

The "statistics" state signifies that MySQL is calculating execution plans for the query. This phase can be particularly resource-intensive when:

Is This the Most Efficient Way to Query the Data?

Not entirely. While EXISTS is suitable for some situations, the sheer volume of subqueries in your example risks overburdening the optimiser. This can lead to significant inefficiencies, particularly when working with billions of records.

To improve this I would:

  1. Use composite indexes for the property_value_* tables covering (attribute_id, value, aid, deleted_at)
  2. Replace multiple EXISTS subqueries with JOINs and conditional aggregation like
    SELECT COUNT(DISTINCT properties.aid)
    FROM properties
    LEFT JOIN property_value_strings pvs1 
        ON pvs1.aid = properties.aid 
        AND pvs1.deleted_at IS NULL 
        AND pvs1.attribute_id = 48 
        AND pvs1.value = 'NC'
    LEFT JOIN property_value_strings pvs2 
        ON pvs2.aid = properties.aid 
        AND pvs2.deleted_at IS NULL 
        AND pvs2.attribute_id = 14 
        AND pvs2.value = 'Wake'
    LEFT JOIN property_value_numerics pvn 
        ON pvn.aid = properties.aid 
        AND pvn.deleted_at IS NULL 
        AND pvn.attribute_id = 175 
        AND pvn.value BETWEEN 200000.0 AND 1000000.0
    -- Add additional joins as necessary
    WHERE properties.deleted_at IS NULL
    GROUP BY properties.aid
    HAVING COUNT(pvs1.id) > 0
       AND COUNT(pvs2.id) > 0
       AND COUNT(pvn.id) > 0;
  1. Partition the tables - partitioning can significantly reduce the amount of data scanned in each query
Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Jan Suchanek