The query time of mysql query statement is too long (it still takes about 10 seconds with the index added), and the amount of data is about 150W. How to deeply optimize sql to shorten the query time?

problem description
mysql query time is too long

the environmental background of the problems and what methods you have tried

after adding the index of the data table, the time is shortened a lot, but it is still not very ideal

related codes

SELECT
    count(
        DISTINCT (auo.user_wxopenid)
    ) AS countPeople,
    count(DISTINCT(auo.session_id)) AS count
FROM
    analysis_user_operation auo
LEFT JOIN wechat we ON we.wxopenid = auo.user_wxopenid
LEFT JOIN analysis_user_operation_new auon ON auon.user_wxopenid = auo.user_wxopenid
WHERE
    1 = 1
AND (
    (
        auo.resource_type = 0
        AND auo.resource_id = 20
    )
    OR (
        auo.resource_type = 2
        AND auo.resource_id IN (
            SELECT
                id
            FROM
                museum_exhibit_audio
            WHERE
                exhibit_id IN (
                    SELECT
                        id
                    FROM
                        museum_exhibit
                    WHERE
                        area_id IN (
                            SELECT
                                id
                            FROM
                                museum_area
                            WHERE
                                room_id = 20
                        )
                )
        )
    )
    OR (
        auo.resource_type = 1
        AND auo.resource_id IN (
            SELECT
                id
            FROM
                museum_exhibit
            WHERE
                area_id IN (
                    SELECT
                        id
                    FROM
                        museum_area
                    WHERE
                        room_id = 20
                )
        )
    )
    OR (
        auo.resource_type = 4
        AND auo.resource_id = 20
    )
    OR (
        auo.resource_type = 5
        AND auo.resource_id IN (
            SELECT
                id
            FROM
                museum_area
            WHERE
                room_id = 20
        )
    )
)
AND auo.operate_starttime >= "2018-10-01 00:00:00"
AND auo.operate_starttime < "2018-10-30 23:59:59"
ORDER BY
    auo.gmt_create

result of sql explain: -sharp-sharp-sharp
:

what result do you expect?

Business requirements are about the number of visitors and the number of visits to the room (audio, exhibits and exhibition area under the room are counted).
I hope God can help see where the sql query is slow and how to optimize the sql so that the query time is about 1: 3 seconds, which is now 7: 10 seconds.

Dec.22,2021

EXPLAIN to see if the index is used, and then optimize it.


the result of sql explain is already above

updated


add relevant indexes to the second row of explain. There are too many scanning lines


you can try to split the or in the where statement into multiple subquery unified fields and add an identification field to associate with union, and then count on the outer layer to try

.
Menu