Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question