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.