D
D
Dima Petrov2018-07-20 12:22:53
MySQL
Dima Petrov, 2018-07-20 12:22:53

Why is the request hanging and unanswered?

For all conditions, there is an index.

SELECT u.timestamp ,u.login, app.id appId, a.id aId, b.id bId, ah.id ahId, ah.date_time, ah.as_revealed, ah.user_id, ah.barrier_id
        FROM `user` u
        LEFT JOIN attribut_app app ON u.id = app.user_id
        LEFT JOIN barrier b ON app.barrier_id = b.id AND b.manager_id IS NULL /*не будет работать если у объекта появится */
        LEFT JOIN access a ON SUBSTRING(u.login,2) = SUBSTRING(a.access_number,2) AND deleted = '0000-00-00 00:00:00' /*получаем все возможные доступы это ползователя по нашим */
        LEFT JOIN access_history ah ON DATE_SUB(NOW(), INTERVAL 30 DAY ) <= ah.date_time AND (ah.access_id = a.id OR ah.user_id = app.user_id) /*история  за полседние 30 дней*/
        WHERE u.key IS NOT NULL
        ORDER BY u.timestamp DESC

If we remove this piece, the request passes OR ah.user_id = app.user_id
All that in the join pass individually.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-07-20
@Rsa97

If functions are used on the left side of the JOIN, WHERE or HAVING conditions, then indexes on these expressions do not work, which greatly slows down the query.
Well also it is necessary to look at the size of initial tables and JOIN'ov selectivity. Maybe you are building a Cartesian product for 1000000 5 lines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question