php - 如何解决执行查询时的数据库锁定问题

标签 php mysql database performance

嗨 friend 们谁能告诉我我的查询有什么问题。当我执行时,它会锁定数据库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;

最佳答案

我认为您的 where 条件中的子查询包含过多的数据。 也许你可以尝试像下面这样优化你的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;

关于php - 如何解决执行查询时的数据库锁定问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22142126/

相关文章:

mysql - 保存历史可编辑数据RDBMS

php - 您关注的人的 mysql Activity 源(点赞、评论等)

php - PECL AMQP 与 php-amqp

php - 我应该为 rest api 包装器编写测试吗?

PHP fatal error : Call to undefined function json_decode()

php - Zend 框架 : Meta attributes (keywords/description) from database

java - Spring JdbcTemplate 无法从 MySQL 获取插入 ID

mysql - 在 rails 事件记录迁移脚本中需要除 id 作为主键

mysql - 将 CSV 数据导入 SQL 数据库

mysql - SELECT INNER JOIN 只返回一个结果