[英]How do I solve the database lock issue on execution the query
嗨朋友可以告訴我我的查詢有什么問題。 當我執行時,它鎖定數據庫5到10分鍾,請幫我優化這個查詢。
SELECT
u.username,
u.email,
u.created_at,
p.firstname,
p.lastname,
p.address,
p.address_2,
p.city,
p.country,
p.state,
p.zip,
p.phone,
p.phone_2,
u.last_ip,
u.last_login_at,
u.auto_login,
u.registration_page,
s.product_name
FROM
users AS u
Left Join subscriptions AS s ON u.id = s.user_id
Left Join profiles AS p ON u.id = p.user_id
where u.registration_page='Chris_Cleanse' and
u.id not in (select user_id from goal_sheets) and
u.id not in(select user_id from sheet_user_popup_not_adam) and
s.expired=TRUE ORDER BY u.id DESC;
我想你的子查詢在條件包含大量數據的地方。 也許您可以嘗試優化您的SQL,如下所示:
SELECT
u.username,u.email
FROM
users AS u
Left Join subscriptions AS s ON u.id = s.user_id
Left Join profiles AS p ON u.id = p.user_id
Left Join goal_sheets gs ON u.id = gs.user_id
Left Join sheet_user_popup_not_adam sa ON u.id = sa.user_id
where u.registration_page='Chris_Cleanse'
and gs. user_id is null
and sa.user_id is null
s.expired=TRUE ORDER BY u.id DESC;
嘗試使用INNER JOIN
。 訪問此頁面以獲得JOIN
的直觀說明。
PS : 在查詢中也正確地轉義`table`
和`columns`
名稱。 這是一個很好的做法。
更新 :運行EXPLAIN SELECT ...
(此處的查詢而不是SELECT ...
)並分析結果。 也發布在這里。
您可以嘗試為上述記錄創建mysql視圖,因為大約有500萬條記錄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.