Skip to content

Conversation

@seawinde
Copy link
Contributor

@seawinde seawinde commented Jun 19, 2024

Proposed changes

Query is aggregate, the query group by expression is less than materialzied view group by expression.
when the more dimensions than queries in materialzied view can be eliminated with functional dependencies.
it can be rewritten with out roll up aggregate.
For example as following:
mv def is

   CREATE MATERIALIZED VIEW mv
   BUILD IMMEDIATE REFRESH AUTO ON MANUAL
   DISTRIBUTED BY RANDOM BUCKETS 2
   PROPERTIES ('replication_num' = '1')
   AS 
  select 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  ps_partkey, 
  cast(
    sum(
      IFNULL(ps_suppkey, 0) * IFNULL(ps_partkey, 0)
    ) as decimal(28, 8)
  ) as agg2 
from 
  lineitem_1 
  inner join orders_1 on lineitem_1.l_orderkey = orders_1.o_orderkey 
  inner join partsupp_1 on l_partkey = partsupp_1.ps_partkey 
  and l_suppkey = partsupp_1.ps_suppkey 
where 
  partsupp_1.ps_suppkey > 1 
group by 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  ps_partkey;

query is as following:

  select 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  cast(
    sum(
      IFNULL(ps_suppkey, 0) * IFNULL(ps_partkey, 0)
    ) as decimal(28, 8)
  ) as agg2 
from 
  lineitem_1 
  inner join orders_1 on lineitem_1.l_orderkey = orders_1.o_orderkey 
  inner join partsupp_1 on l_partkey = partsupp_1.ps_partkey 
  and l_suppkey = partsupp_1.ps_suppkey 
where 
  partsupp_1.ps_suppkey > 1 
group by 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey;

we can see that query doesn't use ps_partkey which is in mv group by expression.
Normally will add roll up aggragate on materialized view if the gorup by dimension in mv is mucher than query group by dimension.
And, in this scane we can get the function dependency on l_suppkey = ps_suppkey . and we doesn't need to add roll up aggregate on materialized view in rewritten plan. this improve performance and is beneficial for nest materialized view rewrite.

@doris-robot
Copy link

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR

Since 2024-03-18, the Document has been moved to doris-website.
See Doris Document.

@seawinde
Copy link
Contributor Author

run buildall

@seawinde seawinde changed the title [fix](mtmv) Fix high level materialized view can not be rewritten, because low level mv aggregate roll up [fix](mtmv) Fix high nest level materialized view can not be rewritten, because low level mv aggregate roll up Jun 19, 2024
@morrySnow
Copy link
Contributor

run performance

@doris-robot
Copy link

TPC-H: Total hot run time: 39837 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 566143d642b615a460465270d7a51cfd3eea5851, data reload: false

------ Round 1 ----------------------------------
q1	18024	4414	4400	4400
q2	3027	202	202	202
q3	11138	1151	1166	1151
q4	12391	736	753	736
q5	7502	2661	2623	2623
q6	224	138	134	134
q7	953	601	596	596
q8	9224	2057	2043	2043
q9	9036	6449	6435	6435
q10	8895	3654	3715	3654
q11	442	242	235	235
q12	510	232	225	225
q13	17765	2965	2961	2961
q14	256	237	217	217
q15	525	501	482	482
q16	517	378	390	378
q17	956	650	646	646
q18	8106	7513	7364	7364
q19	5210	1541	1475	1475
q20	650	310	344	310
q21	4894	3231	3927	3231
q22	400	339	348	339
Total cold run time: 120645 ms
Total hot run time: 39837 ms

----- Round 2, with runtime_filter_mode=off -----
q1	4432	4239	4226	4226
q2	366	293	263	263
q3	2947	2744	2759	2744
q4	1845	1589	1609	1589
q5	5201	5238	5262	5238
q6	217	125	125	125
q7	2112	1698	1691	1691
q8	3164	3303	3301	3301
q9	8322	8301	8289	8289
q10	3855	3654	3670	3654
q11	588	481	484	481
q12	767	580	582	580
q13	16337	2984	2988	2984
q14	302	273	253	253
q15	517	466	491	466
q16	480	417	434	417
q17	1782	1475	1460	1460
q18	7660	7532	7340	7340
q19	1685	1509	1651	1509
q20	1962	1798	1756	1756
q21	4938	4691	4601	4601
q22	585	553	540	540
Total cold run time: 70064 ms
Total hot run time: 53507 ms

@doris-robot
Copy link

TPC-DS: Total hot run time: 171735 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit 566143d642b615a460465270d7a51cfd3eea5851, data reload: false

query1	928	379	372	372
query2	6462	2430	2276	2276
query3	6664	206	219	206
query4	18851	17265	17288	17265
query5	4135	481	486	481
query6	249	160	173	160
query7	4594	294	289	289
query8	328	299	295	295
query9	8451	2457	2436	2436
query10	603	305	285	285
query11	10646	9979	10029	9979
query12	144	95	84	84
query13	1648	368	391	368
query14	8785	6092	7037	6092
query15	244	201	193	193
query16	7535	261	263	261
query17	1501	529	523	523
query18	1835	280	275	275
query19	207	156	146	146
query20	92	82	88	82
query21	218	130	125	125
query22	4180	4065	4123	4065
query23	33819	32930	33075	32930
query24	11309	2840	2848	2840
query25	669	357	361	357
query26	1674	156	150	150
query27	3128	321	315	315
query28	7881	2080	2058	2058
query29	1044	623	611	611
query30	290	151	150	150
query31	960	725	761	725
query32	94	57	61	57
query33	771	296	282	282
query34	965	484	460	460
query35	748	618	662	618
query36	1076	931	913	913
query37	171	75	74	74
query38	2883	2754	2715	2715
query39	865	788	796	788
query40	282	126	130	126
query41	55	52	53	52
query42	121	99	100	99
query43	604	542	567	542
query44	1173	725	736	725
query45	203	165	165	165
query46	1093	716	680	680
query47	1839	1736	1776	1736
query48	367	285	290	285
query49	1192	399	400	399
query50	771	376	387	376
query51	6858	6791	6840	6791
query52	114	95	96	95
query53	354	297	294	294
query54	869	442	429	429
query55	76	71	73	71
query56	280	258	290	258
query57	1117	1056	1070	1056
query58	256	240	243	240
query59	3392	3312	3196	3196
query60	298	267	276	267
query61	92	94	89	89
query62	646	447	448	447
query63	318	286	297	286
query64	9741	2295	1723	1723
query65	3153	3136	3135	3135
query66	1407	338	353	338
query67	15389	14787	14766	14766
query68	4590	518	543	518
query69	465	298	297	297
query70	1199	1064	1165	1064
query71	422	279	272	272
query72	7611	5733	5660	5660
query73	742	320	315	315
query74	5952	5570	5544	5544
query75	3468	2659	2661	2659
query76	2685	900	877	877
query77	448	309	305	305
query78	10263	9842	9665	9665
query79	2347	512	573	512
query80	908	486	467	467
query81	581	227	225	225
query82	806	107	102	102
query83	266	169	177	169
query84	241	87	82	82
query85	1565	277	272	272
query86	486	308	334	308
query87	3262	3090	3146	3090
query88	3973	2363	2331	2331
query89	475	388	384	384
query90	1821	185	190	185
query91	129	98	100	98
query92	63	51	49	49
query93	2213	514	493	493
query94	1263	186	184	184
query95	409	318	317	317
query96	596	264	269	264
query97	3201	3028	3079	3028
query98	220	209	193	193
query99	1343	877	839	839
Total cold run time: 272720 ms
Total hot run time: 171735 ms

@seawinde seawinde force-pushed the nested_mv_not_hit_fix branch from 566143d to fe79381 Compare June 21, 2024 04:01
@doris-robot
Copy link

ClickBench: Total hot run time: 30.42 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 566143d642b615a460465270d7a51cfd3eea5851, data reload: false

query1	0.04	0.03	0.04
query2	0.08	0.03	0.04
query3	0.23	0.04	0.05
query4	1.67	0.07	0.07
query5	0.50	0.49	0.50
query6	1.12	0.73	0.72
query7	0.02	0.01	0.01
query8	0.06	0.05	0.04
query9	0.55	0.50	0.50
query10	0.54	0.54	0.54
query11	0.16	0.12	0.11
query12	0.16	0.13	0.13
query13	0.59	0.59	0.61
query14	0.77	0.77	0.78
query15	0.83	0.81	0.81
query16	0.37	0.37	0.37
query17	0.97	1.03	0.95
query18	0.23	0.25	0.26
query19	1.79	1.73	1.72
query20	0.01	0.01	0.01
query21	15.43	0.68	0.67
query22	3.96	7.84	1.80
query23	18.28	1.32	1.24
query24	2.10	0.24	0.23
query25	0.17	0.08	0.08
query26	0.27	0.18	0.17
query27	0.07	0.08	0.08
query28	13.26	1.02	1.00
query29	12.66	3.29	3.24
query30	0.26	0.07	0.06
query31	2.84	0.41	0.38
query32	3.23	0.47	0.47
query33	2.86	2.89	2.95
query34	17.23	4.41	4.40
query35	4.50	4.44	4.47
query36	0.65	0.47	0.48
query37	0.19	0.17	0.15
query38	0.15	0.15	0.15
query39	0.04	0.03	0.04
query40	0.17	0.15	0.15
query41	0.10	0.05	0.05
query42	0.06	0.05	0.05
query43	0.05	0.04	0.04
Total cold run time: 109.22 s
Total hot run time: 30.42 s

@seawinde
Copy link
Contributor Author

run buildall

@doris-robot
Copy link

TPC-H: Total hot run time: 40247 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 5d503224a1ebf4b57863a9b1e85be32f84bea0e7, data reload: false

------ Round 1 ----------------------------------
q1	17829	4551	4396	4396
q2	2542	193	210	193
q3	10968	1135	1157	1135
q4	11343	830	848	830
q5	7661	2726	2669	2669
q6	228	135	140	135
q7	959	620	632	620
q8	9652	2110	2087	2087
q9	8898	6490	6480	6480
q10	9011	3754	3722	3722
q11	485	242	238	238
q12	453	241	235	235
q13	17760	2987	2986	2986
q14	263	225	219	219
q15	510	484	502	484
q16	539	380	380	380
q17	974	731	701	701
q18	7969	7414	7387	7387
q19	2486	1474	1401	1401
q20	644	318	326	318
q21	4986	3284	4081	3284
q22	415	348	347	347
Total cold run time: 116575 ms
Total hot run time: 40247 ms

----- Round 2, with runtime_filter_mode=off -----
q1	4350	4251	4227	4227
q2	375	264	269	264
q3	3022	2735	2751	2735
q4	1897	1606	1594	1594
q5	5305	5271	5311	5271
q6	216	129	130	129
q7	2149	1703	1728	1703
q8	3176	3344	3345	3344
q9	8341	8348	8331	8331
q10	3875	3690	3708	3690
q11	585	504	490	490
q12	781	593	606	593
q13	17538	3040	3005	3005
q14	304	259	270	259
q15	516	479	488	479
q16	481	404	420	404
q17	1780	1464	1438	1438
q18	7512	7556	7282	7282
q19	1770	1654	1473	1473
q20	1981	1773	1790	1773
q21	4872	4849	4732	4732
q22	652	553	540	540
Total cold run time: 71478 ms
Total hot run time: 53756 ms

@doris-robot
Copy link

TPC-DS: Total hot run time: 172986 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit 5d503224a1ebf4b57863a9b1e85be32f84bea0e7, data reload: false

query1	934	384	383	383
query2	6438	2373	2395	2373
query3	6643	208	194	194
query4	19331	17235	17187	17187
query5	4196	474	461	461
query6	240	155	171	155
query7	4591	312	291	291
query8	329	278	283	278
query9	8576	2376	2367	2367
query10	596	325	282	282
query11	10670	10000	9938	9938
query12	139	84	84	84
query13	1627	380	376	376
query14	10245	6944	7117	6944
query15	241	188	188	188
query16	7826	274	275	274
query17	1883	557	540	540
query18	1953	286	286	286
query19	198	166	156	156
query20	92	86	85	85
query21	212	138	122	122
query22	4223	4130	4061	4061
query23	33609	33040	32964	32964
query24	12025	2771	2958	2771
query25	699	378	382	378
query26	1815	163	157	157
query27	3008	321	320	320
query28	7475	2066	2055	2055
query29	1191	652	631	631
query30	281	154	156	154
query31	966	735	759	735
query32	94	56	58	56
query33	783	296	291	291
query34	978	477	487	477
query35	753	637	626	626
query36	1086	934	898	898
query37	290	75	72	72
query38	2870	2724	2714	2714
query39	878	796	802	796
query40	281	127	127	127
query41	55	53	56	53
query42	120	100	100	100
query43	599	546	537	537
query44	1257	719	735	719
query45	199	166	170	166
query46	1084	713	716	713
query47	1843	1746	1776	1746
query48	370	302	300	300
query49	1211	418	415	415
query50	769	385	401	385
query51	6952	6849	6756	6756
query52	109	93	99	93
query53	352	300	299	299
query54	905	465	455	455
query55	78	75	80	75
query56	308	281	283	281
query57	1188	1038	1023	1023
query58	277	268	256	256
query59	3592	3275	3310	3275
query60	327	299	291	291
query61	109	111	111	111
query62	672	566	441	441
query63	324	302	287	287
query64	9813	2250	1756	1756
query65	3195	3106	3107	3106
query66	1366	331	331	331
query67	15605	14812	15060	14812
query68	8824	552	555	552
query69	727	458	429	429
query70	1313	1075	1091	1075
query71	542	278	274	274
query72	9160	5494	5726	5494
query73	1943	334	325	325
query74	6018	5479	5530	5479
query75	5605	2644	2661	2644
query76	5129	923	952	923
query77	784	301	301	301
query78	10492	9890	9864	9864
query79	8128	518	505	505
query80	977	454	462	454
query81	562	225	221	221
query82	741	109	101	101
query83	352	167	173	167
query84	278	84	87	84
query85	1201	293	269	269
query86	397	292	308	292
query87	3296	3074	3126	3074
query88	4329	2350	2373	2350
query89	528	402	380	380
query90	1973	189	195	189
query91	133	98	104	98
query92	67	50	50	50
query93	6550	497	496	496
query94	1206	190	183	183
query95	408	313	318	313
query96	617	271	260	260
query97	3245	3072	3068	3068
query98	225	205	198	198
query99	1178	845	864	845
Total cold run time: 299872 ms
Total hot run time: 172986 ms

@doris-robot
Copy link

ClickBench: Total hot run time: 30.9 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 5d503224a1ebf4b57863a9b1e85be32f84bea0e7, data reload: false

query1	0.04	0.04	0.03
query2	0.08	0.04	0.04
query3	0.22	0.04	0.04
query4	1.69	0.06	0.07
query5	0.51	0.48	0.50
query6	1.14	0.72	0.72
query7	0.02	0.02	0.01
query8	0.05	0.04	0.04
query9	0.54	0.49	0.49
query10	0.54	0.54	0.54
query11	0.15	0.11	0.11
query12	0.14	0.12	0.11
query13	0.59	0.58	0.59
query14	0.76	0.78	0.78
query15	0.82	0.81	0.81
query16	0.37	0.37	0.38
query17	0.97	1.00	0.97
query18	0.21	0.25	0.24
query19	1.90	1.73	1.75
query20	0.01	0.00	0.00
query21	15.42	0.67	0.66
query22	4.51	7.18	2.27
query23	18.34	1.39	1.26
query24	2.16	0.24	0.22
query25	0.15	0.09	0.09
query26	0.28	0.18	0.19
query27	0.08	0.07	0.08
query28	13.17	1.02	1.01
query29	12.60	3.29	3.32
query30	0.25	0.06	0.06
query31	2.89	0.39	0.39
query32	3.27	0.48	0.48
query33	2.87	2.88	2.91
query34	17.07	4.38	4.44
query35	4.46	4.45	4.44
query36	0.64	0.46	0.47
query37	0.19	0.16	0.15
query38	0.14	0.15	0.15
query39	0.05	0.04	0.04
query40	0.18	0.15	0.14
query41	0.09	0.05	0.05
query42	0.05	0.05	0.04
query43	0.05	0.04	0.04
Total cold run time: 109.66 s
Total hot run time: 30.9 s

Plan rewrittenPlan = MaterializedViewUtils.rewriteByRules(cascadesContext,
childContext -> {
Rewriter.getCteChildrenRewriter(childContext,
ImmutableList.of(Rewriter.topDown(new EliminateGroupByKey()))).execute();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need apply this rule seperately? rewrite the whole mv plan tree could not eliminate group by key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because there is five group by dimension in mv, but query use four of them.
So need add projects that query used on mv plan, and run eliminate group by key again.

@github-actions github-actions bot added the approved Indicates a PR has been approved by one committer. label Jun 25, 2024
@github-actions
Copy link
Contributor

PR approved by at least one committer and no changes requested.

@github-actions
Copy link
Contributor

PR approved by anyone and no changes requested.

@morrySnow morrySnow merged commit 3da8351 into apache:master Jun 25, 2024
dataroaring pushed a commit that referenced this pull request Jun 26, 2024
…n, because low level mv aggregate roll up (#36567)

Query is aggregate, the query group by expression is less than
materialzied view group by expression.
when the more dimensions than queries in materialzied view can be
eliminated with functional dependencies.
it can be rewritten with out roll up aggregate.
For example as following:
mv def is 

CREATE MATERIALIZED VIEW mv
BUILD IMMEDIATE REFRESH AUTO ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES ('replication_num' = '1')
AS 
select 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  ps_partkey, 
  cast(
    sum(
      IFNULL(ps_suppkey, 0) * IFNULL(ps_partkey, 0)
    ) as decimal(28, 8)
  ) as agg2 
from 
  lineitem_1 
  inner join orders_1 on lineitem_1.l_orderkey = orders_1.o_orderkey
  inner join partsupp_1 on l_partkey = partsupp_1.ps_partkey 
  and l_suppkey = partsupp_1.ps_suppkey 
where 
  partsupp_1.ps_suppkey > 1 
group by 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  ps_partkey;

query is as following:

select 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  cast(
    sum(
      IFNULL(ps_suppkey, 0) * IFNULL(ps_partkey, 0)
    ) as decimal(28, 8)
  ) as agg2 
from 
  lineitem_1 
  inner join orders_1 on lineitem_1.l_orderkey = orders_1.o_orderkey
  inner join partsupp_1 on l_partkey = partsupp_1.ps_partkey 
  and l_suppkey = partsupp_1.ps_suppkey 
where 
  partsupp_1.ps_suppkey > 1 
group by 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey;

we can see that query doesn't use `ps_partkey` which is in mv group by
expression.
Normally will add roll up aggragate on materialized view if the gorup by
dimension in mv is mucher than query group by dimension.
And, in this scane we can get the function dependency on `l_suppkey =
ps_suppkey `. and we doesn't need to add roll up aggregate on
materialized view in rewritten plan. this improve performance and is
beneficial for nest materialized view rewrite.
seawinde added a commit to seawinde/doris that referenced this pull request Jul 11, 2024
…n, because low level mv aggregate roll up (apache#36567)

Query is aggregate, the query group by expression is less than
materialzied view group by expression.
when the more dimensions than queries in materialzied view can be
eliminated with functional dependencies.
it can be rewritten with out roll up aggregate.
For example as following:
mv def is 

CREATE MATERIALIZED VIEW mv
BUILD IMMEDIATE REFRESH AUTO ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES ('replication_num' = '1')
AS 
select 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  ps_partkey, 
  cast(
    sum(
      IFNULL(ps_suppkey, 0) * IFNULL(ps_partkey, 0)
    ) as decimal(28, 8)
  ) as agg2 
from 
  lineitem_1 
  inner join orders_1 on lineitem_1.l_orderkey = orders_1.o_orderkey
  inner join partsupp_1 on l_partkey = partsupp_1.ps_partkey 
  and l_suppkey = partsupp_1.ps_suppkey 
where 
  partsupp_1.ps_suppkey > 1 
group by 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  ps_partkey;

query is as following:

select 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey, 
  cast(
    sum(
      IFNULL(ps_suppkey, 0) * IFNULL(ps_partkey, 0)
    ) as decimal(28, 8)
  ) as agg2 
from 
  lineitem_1 
  inner join orders_1 on lineitem_1.l_orderkey = orders_1.o_orderkey
  inner join partsupp_1 on l_partkey = partsupp_1.ps_partkey 
  and l_suppkey = partsupp_1.ps_suppkey 
where 
  partsupp_1.ps_suppkey > 1 
group by 
  l_orderkey, 
  l_partkey, 
  l_suppkey, 
  o_orderkey, 
  o_custkey;

we can see that query doesn't use `ps_partkey` which is in mv group by
expression.
Normally will add roll up aggragate on materialized view if the gorup by
dimension in mv is mucher than query group by dimension.
And, in this scane we can get the function dependency on `l_suppkey =
ps_suppkey `. and we doesn't need to add roll up aggregate on
materialized view in rewritten plan. this improve performance and is
beneficial for nest materialized view rewrite.
morrySnow pushed a commit that referenced this pull request Jul 12, 2024
cherry-pick from master
pr: #36318
commitId: c199947

pr: #36111
commitId: 35ebef6

pr: #36175
commitId: 4c8e66b

pr: #36414
commitId: 5e009b5

pr: #36770
commitId: 19e2126

pr: #36567
commitId: 3da8351
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. dev/2.1.5-merged dev/3.0.0-merged reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants