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;
原创文章,欢迎转载,转载请注明出处!
把每一件简单的事情做好,就是不简单;把每一件平凡的事情做好,就是不平凡!相信自己,创造奇迹~~