如果查询的结果少,可以把查询结果当子集,再查另一张表,比join的速度快得多得多

select up.UP_NAME as printName,count(1) as qty from riv_print_history_l phl  JOIN tab_user_profile up
 on up.UP_USER_ID = phl.PHL_PRINT_USER_ID
 where 
 phl.CREATE_TIME >= '2017-00-15 00:00:00' and phl.CREATE_TIME < '2017-09-21 00:00:00' 
GROUP BY  phl.PHL_PRINT_USER_ID;



select 
(select UP_NAME from tab_user_profile where UP_USER_ID = tab1.PHL_PRINT_USER_ID)
     as printName,tab1.qty from 
(select PHL_PRINT_USER_ID,count(1) as qty from riv_print_history_l
 where CREATE_TIME >= '2017-00-15 00:00:00' and CREATE_TIME < '2017-09-21 01:00:00' GROUP BY  PHL_PRINT_USER_ID)tab1;

tab_user_profile     116行

riv_print_history_l   640746行

select PHL_PRINT_USER_ID,count(1) as qty from riv_print_history_l
 where CREATE_TIME >= '2017-00-15 00:00:00' and CREATE_TIME < '2017-09-21 01:00:00' GROUP BY  PHL_PRINT_USER_ID
查出的结果只有3行

第一个用时10s,第二个用时0.6s

posted @ 2017-09-26 14:45  tonggc1668  阅读(132)  评论(0编辑  收藏  举报