欢迎访问我的个人网站==》 jiashubing.cn

mysql 连表查询太慢

  优化 join on性能,解决联表查询慢的问题

  这里只提供一种方式啊,就是如果连表有查询条件,那就先把条件查了,然后再连表,这个很有用

  比如:pub_user1 与 pub_user2 有相同的字段 user_id


  直接这么写会多查询很多数据

SELECT
    a.user_id,
    a.user_name,
    b.user_code,
    b.user_num
FROM
    pub_user1 a
RIGHT JOIN pub_user2 b ON a.user_id = b.user_id
AND b.user_num > 0;

 

  可以改成这样:

SELECT
    a.user_id,
    a.user_name,
    b.user_code,
    b.user_num
FROM
    pub_user1 a
RIGHT JOIN (
    SELECT
        user_id,
        user_code,
        user_num
    FROM
        pub_user2
    WHERE
        user_num > 0
) AS b ON a.user_id = b.user_id;

 

原创文章,欢迎转载,转载请注明出处!



posted @ 2024-10-31 17:18  贾树丙  阅读(3)  评论(0编辑  收藏  举报