@@ -461,15 +461,15 @@ def test_size_benchmark(self):
461
461
bf .write (json .dumps (data , indent = 4 ))
462
462
463
463
def test_query_performance (self ):
464
- any_flag_count = {}
465
- all_flag_count = {}
466
- any_flag_load = {}
467
- all_flag_load = {}
464
+ has_any_flag_count = {}
465
+ has_all_flag_count = {}
466
+ has_any_flag_load = {}
467
+ has_all_flag_load = {}
468
468
469
- any_bool_count = {}
470
- all_bool_count = {}
471
- any_bool_load = {}
472
- all_bool_load = {}
469
+ has_any_bool_count = {}
470
+ has_all_bool_count = {}
471
+ has_any_bool_load = {}
472
+ has_all_bool_load = {}
473
473
474
474
with connection .cursor () as cursor :
475
475
for FlagModel , BoolModel in zip (self .FLAG_MODELS , self .BOOL_MODELS ):
@@ -480,8 +480,8 @@ def test_query_performance(self):
480
480
mask = 1
481
481
mask_en = FlagModel ._meta .get_field ("flags" ).enum (mask )
482
482
483
- flag_any_q = FlagModel .objects .filter (flags__any = mask_en )
484
- flag_all_q = FlagModel .objects .filter (flags__all = mask_en )
483
+ flag_any_q = FlagModel .objects .filter (flags__has_any = mask_en )
484
+ flag_all_q = FlagModel .objects .filter (flags__has_all = mask_en )
485
485
486
486
bool_q = [
487
487
Q (** {f"flg_{ flg } " : bool (mask & (1 << flg ) != 0 )})
@@ -502,11 +502,11 @@ def test_query_performance(self):
502
502
503
503
start = perf_counter ()
504
504
flag_any_count = flag_any_q .count ()
505
- any_flag_count [FlagModel .num_flags ] = perf_counter () - start
505
+ has_any_flag_count [FlagModel .num_flags ] = perf_counter () - start
506
506
507
507
start = perf_counter ()
508
508
bool_any_count = bool_any_q .count ()
509
- any_bool_count [BoolModel .num_flags ] = perf_counter () - start
509
+ has_any_bool_count [BoolModel .num_flags ] = perf_counter () - start
510
510
511
511
try :
512
512
# make sure our queries are equivalent
@@ -518,75 +518,83 @@ def test_query_performance(self):
518
518
519
519
start = perf_counter ()
520
520
flag_all_count = flag_all_q .count ()
521
- all_flag_count [FlagModel .num_flags ] = perf_counter () - start
521
+ has_all_flag_count [FlagModel .num_flags ] = perf_counter () - start
522
522
523
523
start = perf_counter ()
524
524
bool_all_count = bool_all_q .count ()
525
- all_bool_count [BoolModel .num_flags ] = perf_counter () - start
525
+ has_all_bool_count [BoolModel .num_flags ] = perf_counter () - start
526
526
527
527
# make sure our queries are equivalent
528
528
self .assertEqual (flag_all_count , bool_all_count )
529
529
530
530
start = perf_counter ()
531
531
flag_any_list = list (flag_any_q .all ())
532
- any_flag_load [FlagModel .num_flags ] = perf_counter () - start
532
+ has_any_flag_load [FlagModel .num_flags ] = perf_counter () - start
533
533
534
534
start = perf_counter ()
535
535
bool_any_list = list (bool_any_q .all ())
536
- any_bool_load [BoolModel .num_flags ] = perf_counter () - start
536
+ has_any_bool_load [BoolModel .num_flags ] = perf_counter () - start
537
537
538
538
# make sure our queries are equivalent
539
539
self .assertEqual (len (flag_any_list ), len (bool_any_list ))
540
540
541
541
start = perf_counter ()
542
542
flag_all_list = list (flag_all_q .all ())
543
- all_flag_load [FlagModel .num_flags ] = perf_counter () - start
543
+ has_all_flag_load [FlagModel .num_flags ] = perf_counter () - start
544
544
545
545
start = perf_counter ()
546
546
bool_all_list = list (bool_all_q .all ())
547
- all_bool_load [BoolModel .num_flags ] = perf_counter () - start
547
+ has_all_bool_load [BoolModel .num_flags ] = perf_counter () - start
548
548
549
549
# make sure our queries are equivalent
550
550
self .assertEqual (len (flag_all_list ), len (bool_all_list ))
551
551
552
- num_flags = sorted (any_flag_count .keys ())
552
+ num_flags = sorted (has_any_flag_count .keys ())
553
553
554
- any_count_diff = [
555
- any_bool_count [flg ] - any_flag_count [flg ] for flg in num_flags
554
+ has_any_count_diff = [
555
+ has_any_bool_count [flg ] - has_any_flag_count [flg ] for flg in num_flags
556
556
]
557
- all_count_diff = [
558
- all_bool_count [flg ] - all_flag_count [flg ] for flg in num_flags
557
+ has_all_count_diff = [
558
+ has_all_bool_count [flg ] - has_all_flag_count [flg ] for flg in num_flags
559
559
]
560
560
561
- any_load_diff = [any_bool_load [flg ] - any_flag_load [flg ] for flg in num_flags ]
562
- all_load_diff = [all_bool_load [flg ] - all_flag_load [flg ] for flg in num_flags ]
561
+ has_any_load_diff = [
562
+ has_any_bool_load [flg ] - has_any_flag_load [flg ] for flg in num_flags
563
+ ]
564
+ has_all_load_diff = [
565
+ has_all_bool_load [flg ] - has_all_flag_load [flg ] for flg in num_flags
566
+ ]
563
567
564
- # print(any_count_diff )
568
+ # print(has_any_count_diff )
565
569
# print('--------------------------------')
566
- # print(all_count_diff )
570
+ # print(has_all_count_diff )
567
571
# print('--------------------------------')
568
- # print(any_load_diff )
572
+ # print(has_any_load_diff )
569
573
# print('--------------------------------')
570
- # print(all_load_diff )
574
+ # print(has_all_load_diff )
571
575
572
- any_count_tpl = [
573
- (any_bool_count [flg ], any_flag_count [flg ]) for flg in num_flags
576
+ has_any_count_tpl = [
577
+ (has_any_bool_count [flg ], has_any_flag_count [flg ]) for flg in num_flags
574
578
]
575
- all_count_tpl = [
576
- (all_bool_count [flg ], all_flag_count [flg ]) for flg in num_flags
579
+ has_all_count_tpl = [
580
+ (has_all_bool_count [flg ], has_all_flag_count [flg ]) for flg in num_flags
577
581
]
578
582
579
- any_load_tpl = [(any_bool_load [flg ], any_flag_load [flg ]) for flg in num_flags ]
580
- all_load_tpl = [(all_bool_load [flg ], all_flag_load [flg ]) for flg in num_flags ]
583
+ has_any_load_tpl = [
584
+ (has_any_bool_load [flg ], has_any_flag_load [flg ]) for flg in num_flags
585
+ ]
586
+ has_all_load_tpl = [
587
+ (has_all_bool_load [flg ], has_all_flag_load [flg ]) for flg in num_flags
588
+ ]
581
589
582
- print ("------------ any_cnt ----------------" )
583
- print (any_count_tpl )
584
- print ("------------ all_cnt ----------------" )
585
- print (all_count_tpl )
586
- print ("------------ any_load ---------------" )
587
- print (any_load_tpl )
588
- print ("------------ all_load ---------------" )
589
- print (all_load_tpl )
590
+ print ("------------ has_any_cnt ----------------" )
591
+ print (has_any_count_tpl )
592
+ print ("------------ has_all_cnt ----------------" )
593
+ print (has_all_count_tpl )
594
+ print ("------------ has_any_load ---------------" )
595
+ print (has_any_load_tpl )
596
+ print ("------------ has_all_load ---------------" )
597
+ print (has_all_load_tpl )
590
598
591
599
592
600
class CreateRowMixin (BulkCreateMixin ):
@@ -709,11 +717,11 @@ def do_flag_query(self, masks):
709
717
# dont change query order
710
718
711
719
start = perf_counter ()
712
- flg_all .append (self .FlagModel .objects .filter (flags__all = mask ).count ())
720
+ flg_all .append (self .FlagModel .objects .filter (flags__has_all = mask ).count ())
713
721
flg_all_time += perf_counter () - start
714
722
715
723
all_explanation = json .loads (
716
- self .FlagModel .objects .filter (flags__all = mask ).explain (
724
+ self .FlagModel .objects .filter (flags__has_all = mask ).explain (
717
725
format = "json" ,
718
726
analyze = True ,
719
727
buffers = True ,
@@ -728,11 +736,11 @@ def do_flag_query(self, masks):
728
736
flg_all_ftr_time += all_explanation ["Execution Time" ]
729
737
730
738
start = perf_counter ()
731
- flg_any .append (self .FlagModel .objects .filter (flags__any = mask ).count ())
739
+ flg_any .append (self .FlagModel .objects .filter (flags__has_any = mask ).count ())
732
740
flg_any_time += perf_counter () - start
733
741
734
742
any_explanation = json .loads (
735
- self .FlagModel .objects .filter (flags__any = mask ).explain (
743
+ self .FlagModel .objects .filter (flags__has_any = mask ).explain (
736
744
format = "json" ,
737
745
analyze = True ,
738
746
buffers = True ,
@@ -826,7 +834,7 @@ def get_all_q(set_bits):
826
834
827
835
bq_all = reduce (and_ , get_all_q (set_bits ))
828
836
829
- # todo there is not a better way to formulate a any query
837
+ # todo there is not a better way to formulate a has_any query
830
838
# that will use the index ??
831
839
832
840
# bq_any = reduce(
@@ -1008,9 +1016,11 @@ def optimize():
1008
1016
any_ftr_time ,
1009
1017
exact_ftr_time ,
1010
1018
) = query (masks )
1011
- queries [f"{ name } all" ] = ctx .captured_queries [0 ]["sql" ]
1012
- queries [f"{ name } any" ] = ctx .captured_queries [1 ]["sql" ]
1013
- queries [f"{ name } exact" ] = ctx .captured_queries [2 ]["sql" ]
1019
+ queries [f"{ name } has_all" ] = ctx .captured_queries [0 ]["sql" ]
1020
+ queries [f"{ name } has_any" ] = ctx .captured_queries [1 ]["sql" ]
1021
+ queries [f"{ name } has_exact" ] = ctx .captured_queries [2 ][
1022
+ "sql"
1023
+ ]
1014
1024
1015
1025
pbar .refresh ()
1016
1026
0 commit comments