连接优化查询,按条件查询的时候,如何优化查询的时间

问题描述:

要维护两个表:

1.t_users表。里面有account stutus other字段

status有1,2,4,6,3,5,11各种状态

表有5W行以上

2.t_vps表。里面有vps,vacc,adsl,apwd字段

要求每一个vps的信息,以及对应个status的和

传统做法:
直接遍历t_vps,对每一个vps都查一下,让adsl和t_users的other字段相等,再count

优化做法做表的连接:

SELECT countid,a.other AS adsl_run,b.id as id,b.vps AS vps,b.vacc as vacc,b.vpwd as vpwd,b.adsl as adsl,b.apwd as apwd,b.description as description
                       FROM(SELECT count(id) AS countid,other FROM `t_users`
                       WHERE time > $tmpTimeStart AND time<$tmpTimeEnd AND $condition
                       GROUP BY other) AS a
                       RIGHT JOIN (SELECT id,vps,vacc,vpwd,adsl,apwd,description FROM `t_vps`) b ON a.other = b.adsl
                       GROUP BY adsl
                       ORDER BY id ";

 

转载于:https://www.cnblogs.com/caimuqing/p/5710267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值