单个字段 count distinct 快了。和ck差不多
select/*+ PARALLEL(16) */ COUNT(DISTINCT user_id) from t1;
+-------------------------+
| COUNT(DISTINCT user_id) |
+-------------------------+
| 2272948 |
+-------------------------+
1 row in set (1.84 sec)
mysql> explain select/*+ PARALLEL(16) */ COUNT(DISTINCT user_id) from t1;
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query Plan |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| =============================================================================================================================== |
| |ID|OPERATOR |NAME |EST.ROWS |EST.TIME(us)| |
| ------------------------------------------------------------------------------------------------------------------------------- |
| |0 |SCALAR GROUP BY | |1 |2132405 | |
| |1 |└─PX COORDINATOR | |16 |2132405 | |
| |2 | └─EXCHANGE OUT DISTR |:EX10001 |16 |2132403 | |
| |3 | └─MERGE GROUP BY | |16 |2132402 | |
| |4 | └─SUBPLAN SCAN |VIEW1 |2226824 |2129880 | |
| |5 | └─MERGE DISTINCT | |2226824 |2129511 | |
| |6 | └─EXCHANGE IN MERGE SORT DISTR| |34574727 |2105471 | |
| |7 | └─EXCHANGE OUT DISTR (HASH) |:EX10000 |34574727 |1305365 | |
| |8 | └─MERGE DISTINCT | |34574727 |241684 | |
| |9 | └─PX BLOCK ITERATOR | |121458154|157231 | |
| |10| └─TABLE FULL SCAN |t1(idx_userid)|121458154|157231 | |
| =============================================================================================================================== |
| Outputs & filters: |
| ------------------------------------- |
| 0 - output([T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))]), filter(nil), rowset=256 |
| group(nil), agg_func([T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))]) |
| 1 - output([T_FUN_COUNT(VIEW1.t1.user_id)]), filter(nil), rowset=256 |
| 2 - output([T_FUN_COUNT(VIEW1.t1.user_id)]), filter(nil), rowset=256 |
| dop=16 |
| 3 - output([T_FUN_COUNT(VIEW1.t1.user_id)]), filter(nil), rowset=256 |
| group(nil), agg_func([T_FUN_COUNT(VIEW1.t1.user_id)]) |
| 4 - output([VIEW1.t1.user_id]), filter(nil), rowset=256 |
| access([VIEW1.t1.user_id]) |
| 5 - output([t1.user_id]), filter(nil), rowset=256 |
| distinct([t1.user_id]) |
| 6 - output([t1.user_id]), filter(nil), rowset=256 |
| sort_keys([t1.user_id, ASC]) |
| 7 - output([t1.user_id]), filter(nil), rowset=256 |
| (#keys=1, [t1.user_id]), dop=16 |
| 8 - output([t1.user_id]), filter(nil), rowset=256 |
| distinct([t1.user_id]) |
| 9 - output([t1.user_id]), filter(nil), rowset=256 |
| asc |
| 10 - output([t1.user_id]), filter(nil), rowset=256 |
| access([t1.user_id]), partitions(p0) |
| is_index_back=false, is_global_index=false, |
| range_key([t1.user_id], [t1.id]), range(MIN,MIN ; MAX,MAX)always |
| true |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
41 rows in set (0.01 sec)
但 select 多个字段时这种还没ck快,麻烦老师再帮忙看下。
ob:12.7s
ck:5.5s
mysql> explain SELECT/*+ PARALLEL(16) */ disbursed_on_local, gender, COUNT(DISTINCT user_id), COUNT(*), SUM(`total_amount_usd`)
-> FROM `t1`
-> GROUP BY 1,2
-> ORDER BY 1 DESC
-> LIMIT 1;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query Plan |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ============================================================================================================================= |
| |ID|OPERATOR |NAME |EST.ROWS |EST.TIME(us)| |
| ----------------------------------------------------------------------------------------------------------------------------- |
| |0 |LIMIT | |1 |36437539 | |
| |1 |└─PX COORDINATOR MERGE SORT | |16 |36437539 | |
| |2 | └─EXCHANGE OUT DISTR |:EX10002 |16 |36437515 | |
| |3 | └─TOP-N SORT | |16 |36437512 | |
| |4 | └─HASH GROUP BY | |2034 |36437511 | |
| |5 | └─EXCHANGE IN DISTR | |32544 |36437176 | |
| |6 | └─EXCHANGE OUT DISTR (HASH) |:EX10001 |32544 |36434171 | |
| |7 | └─HASH GROUP BY | |32544 |36427411 | |
| |8 | └─SUBPLAN SCAN |VIEW1 |119873195|35395629 | |
| |9 | └─HASH GROUP BY | |119873195|35375798 | |
| |10| └─EXCHANGE IN DISTR | |121406905|31665697 | |
| |11| └─EXCHANGE OUT DISTR (HASH) |:EX10000 |121406905|23037313 | |
| |12| └─HASH GROUP BY | |121406905|3634818 | |
| |13| └─PX BLOCK ITERATOR | |121458154|267697 | |
| |14| └─COLUMN TABLE FULL SCAN|t1|121458154|267697 | |
| ============================================================================================================================= |
| Outputs & filters: |
| ------------------------------------- |
| 0 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))], |
| [T_FUN_COUNT_SUM(T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*)))], [T_FUN_SUM(T_FUN_SUM(VIEW1.SEL_2))]), filter(nil), rowset=256 |
| limit(1), offset(nil) |
| 1 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))], |
| [T_FUN_COUNT_SUM(T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*)))], [T_FUN_SUM(T_FUN_SUM(VIEW1.SEL_2))]), filter(nil), rowset=256 |
| sort_keys([VIEW1.SEL_3, DESC]) |
| 2 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))], |
| [T_FUN_COUNT_SUM(T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*)))], [T_FUN_SUM(T_FUN_SUM(VIEW1.SEL_2))]), filter(nil), rowset=256 |
| dop=16 |
| 3 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))], |
| [T_FUN_COUNT_SUM(T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*)))], [T_FUN_SUM(T_FUN_SUM(VIEW1.SEL_2))]), filter(nil), rowset=256 |
| sort_keys([VIEW1.SEL_3, DESC]), topn(1) |
| 4 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))], |
| [T_FUN_COUNT_SUM(T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*)))], [T_FUN_SUM(T_FUN_SUM(VIEW1.SEL_2))]), filter(nil), rowset=256 |
| group([VIEW1.SEL_3], [VIEW1.t1.gender]), agg_func([T_FUN_COUNT_SUM(T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*)))], |
| [T_FUN_SUM(T_FUN_SUM(VIEW1.SEL_2))], [T_FUN_COUNT_SUM(T_FUN_COUNT(VIEW1.t1.user_id))]) |
| 5 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*))], [T_FUN_SUM(VIEW1.SEL_2)], |
| [T_FUN_COUNT(VIEW1.t1.user_id)]), filter(nil), rowset=256 |
| 6 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*))], [T_FUN_SUM(VIEW1.SEL_2)], |
| [T_FUN_COUNT(VIEW1.t1.user_id)]), filter(nil), rowset=256 |
| (#keys=2, [VIEW1.SEL_3], [VIEW1.t1.gender]), dop=16 |
| 7 - output([VIEW1.SEL_3], [VIEW1.t1.gender], [T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*))], [T_FUN_SUM(VIEW1.SEL_2)], |
| [T_FUN_COUNT(VIEW1.t1.user_id)]), filter(nil), rowset=256 |
| group([VIEW1.SEL_3], [VIEW1.t1.gender]), agg_func([T_FUN_COUNT_SUM(VIEW1.T_FUN_COUNT(*))], [T_FUN_SUM(VIEW1.SEL_2)], |
| [T_FUN_COUNT(VIEW1.t1.user_id)]) |
| 8 - output([VIEW1.T_FUN_COUNT(*)], [VIEW1.SEL_2], [VIEW1.SEL_3], [VIEW1.t1.gender], [VIEW1.t1.user_id]), filter(nil), rowset=256 |
| access([VIEW1.T_FUN_COUNT(*)], [VIEW1.SEL_2], [VIEW1.SEL_3], [VIEW1.t1.gender], [VIEW1.t1.user_id]) |
| 9 - output([T_FUN_COUNT_SUM(T_FUN_COUNT(*))], [T_FUN_SUM(T_FUN_SUM(t1.total_amount_usd))], [t1.disbursed_on_loc |
| al], [t1.gender], [t1.user_id]), filter(nil), rowset=256 |
| group([t1.disbursed_on_local], [t1.gender], [t1.user_id]), |
| agg_func([T_FUN_COUNT_SUM(T_FUN_COUNT(*))], [T_FUN_SUM(T_FUN_SUM(t1.total_amount_usd))]) |
| 10 - output([t1.disbursed_on_local], [t1.gender], [t1.user_id], |
| [T_FUN_COUNT(*)], [T_FUN_SUM(t1.total_amount_usd)]), filter(nil), rowset=256 |
| 11 - output([t1.disbursed_on_local], [t1.gender], [t1.user_id], |
| [T_FUN_COUNT(*)], [T_FUN_SUM(t1.total_amount_usd)]), filter(nil), rowset=256 |
| (#keys=3, [t1.disbursed_on_local], [t1.gender], [t1.user_id]), |
| dop=16 |
| 12 - output([t1.disbursed_on_local], [t1.gender], [t1.user_id], |
| [T_FUN_COUNT(*)], [T_FUN_SUM(t1.total_amount_usd)]), filter(nil), rowset=256 |
| group([t1.disbursed_on_local], [t1.gender], [t1.user_id]), |
| agg_func([T_FUN_COUNT(*)], [T_FUN_SUM(t1.total_amount_usd)]) |
| 13 - output([t1.disbursed_on_local], [t1.gender], [t1.user_id], |
| [t1.total_amount_usd]), filter(nil), rowset=256 |
| 14 - output([t1.disbursed_on_local], [t1.gender], [t1.user_id], |
| [t1.total_amount_usd]), filter(nil), rowset=256 |
| access([t1.disbursed_on_local], [t1.gender], [t1.user_id], |
| [t1.total_amount_usd]), partitions(p0) |
| is_index_back=false, is_global_index=false, |
| range_key([t1.id]), range(MIN ; MAX)always true |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
70 rows in set (0.01 sec)