Hi,
Here's the output from the first query
select MID, max(ValuationDate) from CB where MID= @MID group by MID
Hope it helps.
The Lop tree:
( project
( group
( scan CB
)
)
)
OptBlock1
The Lop tree:
( scan CB
)
Generic Tables: ( Gtt1( CB ) Gti2( IDX1 ) )
Generic Columns: ( Gc0(CB ,Rid) Gc1(CB ,MID) Gc2(CB ,ValuationDate)) Virtual Generic Columns: ( )
Predicates: ( { CB.MID Gtg0.Gc4 } = 0 tc:{1} )
Transitive Closures: ( Tc0 = { Gc0(CB ,Rid)} Tc1 = { Gc1(CB ,MID) Gc4(Gtg0 ,_gcelement_4)} Tc2 = { Gc2(CB ,ValuationDate)} )
OptBlock0
The Lop tree:
( pseudoscan
)
Generic Tables: ( Gtg0 )
Generic Columns: ( Gc3(Gtg0 ,_gcelement_3) Gc4(Gtg0 ,_gcelement_4) Gc5(Gtg0 ,_virtualagg) Gc6(Gtg0 ,_virtualagg)) Virtual Generic Columns: ( )
Predicates: ( { CB.MID Gtg0.Gc4 } = 0 tc:{1} )
Transitive Closures: ( Tc3 = { Gc3(Gtg0 ,_gcelement_3) Gc5(Gtg0 ,_virtualagg) Gc6(Gtg0 ,_virtualagg)} Tc1 = { Gc1(CB ,MID) Gc4(Gtg0 ,_gcelement_4)} )
OptCriteria:
distinct_sorted
distinct_sorting
distinct_hashing
group_sorted
group_hashing
nl_join
append_union_all
merge_union_all
merge_union_distinct
hash_union_distinct
opportunistic_distinct_view
order_sorting
store_index
replicated_partition
index_union
streaming_sort
nary_nl_join
alternative_greedy_search
conserve_tempdb_space: keep estimated tempdb below resource granularity
search_engine_timeout_factor
cr559034: avoid preferring non-covering over covered index scans
allow_wide_top_sort: allow max row size to be exceeded for top sorts
cr562947: OPTLEVEL EXCEPTION SEE CR - allow cursor table scans
data_page_prefetch_costing: clustered row bias added
mru_buffer_costing: wash size buffer limit for MRU
cr546125: implicitly updatable cursor non-unique index scan
cr545771: improves multi-table outer-join and semi-join costing
cr545653: avoid inner table buffer estimate starvation
cr545585: covered iscan CPU costing too expensive
cr545379: disallow reformatting on user forced index scan
cr545180: avoid reformat with no sargs if useful index exists
cr545059: reduce usage of buffer manager optimization sorts
cr544485: mark subquery join predicates with distinct view as sargs
cr556728: facilitates merge joins between small tables
cr534175: compute GROUP BY worktables in nested subqueries only once when possible
cr531199: increases the number of useful nested loop join plans considered
cr500736: supports nocase sortorder columns in mergejoin and hashjoin keys
cr487450: improves DISTINCT costing of multi-table outer joins and/or semi-joins
cr467566: allow abstract plans and statement cache to work together
avoid_bmo_sorts: avoid sorts used only for buffer manager optimization
cr497066: infer the nullability of isnull() by looking at its parameters
cr421607: support NULL=NULL merge and hash join keys
distinct_exists_transform: transform distinct to semi-join
cr552795: eliminate duplicate rows during reformatting when they're not needed
outer_join_costing: outer join row counts and histogramming
join_duplicate_estimates: avoid overestimates of dups in joins
imdb_costing: 0 PIO costing for scans for in-memory database
auto_temptable_stats: auto generation of statistics for #temptables
use_mixed_dt_sarg_under_specialor: allow special OR in case of mixed datatype sargs in IN/OR list
timeout_cart_product: timeout queries involving cartesian product and more than 5 tables.
in_list_general_or: optimize in lists under general ors
cr611385: enable ScanValues costing
cr574923: extend search space hidden by greedy algorithm
cr614461: enable in-order rid scan estimates
cr611637: avoid duplicate NULL selectivity
cr619965: dense frequency for 'Jan 1 1900 12:00:00:000AM'
cr439123: 0.0 range density for low cardinality tables
cr619756: DISTINCT, EXISTS, and GROUP BY row estimation
cr624835: correlated equi-join constant costing
cr628311: subquery decorrelated store index costing
cr628845: ORDER BY consumes parallel bmo sorts
cr636592: do not skip good opportunistic distinct plans
cr631416: semi-join row estimate adjustment
cr639977: eager virtual column sarg evaluation
allow_large_index_row: allow large index row in basic optimization
basic_optimization_partitions: allow partitioned tables in basic optimization
cr606288: disable parallelism in 12.5 optimizer
cr647051: correct NULL selectivity estimates
cr644777: allow multiple IN lists in large-IN-List transformation
cr649006: reduce CPU cost for table scan partition elimination
cr646024: add in sort operator's child costs for fastfirstrow recosting
cr649470: subquery conserve_tempdb_space
cr649590: optimize always false queries
allow_minmax: allow local session to consider MINMAX optimization
cr646220: enable better store index key generation with correlated predicate
cr663304: forced index will restrict one row rule
cr575761: allow general OR scans under dirty reads
cr675179: composite special OR costing
cr673625: avoid_bmo_sorts performance INSERT/SELECT INTO with ORDER BY
cr674607: fixes parallel thread use count calculations
cr685239: enable XCHG op and basic parallel cost based costing updates
cr669847: transform non-functional update producer lop to semi-join
cr683626: forceplan shouldn't fail with subquery under outer join
cr685996: enhanced subquery and outerjoin rules for busy/right-nested plan
limit_row_constructor: limit index starting position optimization
cr731808: remove zeros from LIKE lower bound sarg
cr702725: optimize the INSERT...SELECT...ORDER BY plan selection
cr713638: LIKE optimization - upper bound mis-estimation
cr739820: accurate parallel create index cluster ratios
cr738692: copy multi node subquery cost
cr737628: intelligent index scan null support
cr741742: small table unique index density
cr744975: nested loop bmo sort ordering extension
cr752225: compatibility mode can handle greater than 32 GROUP BY columns
remove_orphaned_subplans: remove subplans not part of a best plan
equals_scan_superset: choose index with a superset of equality keys
avoid_zero_weight_histograms: avoid applying 0 weight to all histogram cells
cr759142: avoid 0 weight range cells followed by small weight frequency cells
Optimizer Level: ase_current
Optimization Goal: allrows_oltp
Optimization Learning mode enabled.
Optimization Timeout limit: 10% (Default)
Server Level Optimization Timeout has been set.
Optimization query tuning time limit enabled.
The compiler will use the current best plan when 10%(Optimization Timeout limit) of average execution time is exceeded.
Optimization query tuning mem limit enabled.
The compiler will use the current best plan when 10%(Max Resource Granularity) of procedure cache is exceeded.
******************************************************************************
BEGIN: Search Space Traversal for OptBlock1
******************************************************************************
Scan plans selected for this optblock:
Beginning selection of qualifying indexes for table 'CB',
Estimating selectivity of index 'CB.IDX1', indid 2
MID = 0
Estimated selectivity for MID,
selectivity = 0.001940126,
scan selectivity 0.001940126, filter selectivity 0.001940126
158985.8 rows, 383.3457 pages
Index covers query.
Data Row Cluster Ratio 0.9370485
Index Page Cluster Ratio 0.9126954
Data Page Cluster Ratio 0.5194609
using index prefetch (size 128K I/O)
Large IO selected: The number of leaf pages qualified is > MIN_PREFETCH pages
in index cache 'default data cache' (cacheid 0) with LRU replacement
Estimating selectivity for table 'CB'
Table scan cost is 8.19461e+07 rows, 516435 pages,
The table (Datarows) has 8.19461e+07 rows, 516435 pages,
Data Page Cluster Ratio 0.9999900
MID = 0
Estimated selectivity for MID,
selectivity = 0.001940126,
Search argument selectivity is 0.001940126.
using table prefetch (size 128K I/O)
Large IO selected: The number of leaf pages qualified is > MIN_PREFETCH pages
in data cache 'default data cache' (cacheid 0) with MRU replacement
MRU selected: More than 1/2 of the pool is swept out by the scan
OptBlock1 Eqc{1} -> Pops added:
( PopIndScan IDX1 CB ) cost:50406.55 T(L385.3457,P77.60534,C476957.2) O(L385.3457,P77.60534,C476957.2) tempdb:0 order: none
The best plan found in OptBlock1 :
( PopIndScan cost:50406.55 T(L385.3457,P77.60534,C476957.2) O(L385.3457,P77.60534,C476957.2) tempdb:0 props: [{}] Gti2( IDX1 ) Gtt1( CB ) ) cost:50406.55 T(L385.3457,P77.60534,C476957.2) O(L385.3457,P77.60534,C476957.2) tempdb:0 order: none
******************************************************************************
DONE: Search Space Traversal for OptBlock1
******************************************************************************
******************************************************************************
BEGIN: Search Space Traversal for OptBlock0
******************************************************************************
Scan plans selected for this optblock:
OptBlock0 Eqc{0} -> Pops added:
( PopGroupSorted ( PopIndScan IDX1 CB ) ) cost:66305.12 T(L385.3457,P77.60534,C635943) O(L0,P0,C158985.8) tempdb:0 order: none
The best plan found in OptBlock0 :
( PopGroupSorted cost:66305.12 T(L385.3457,P77.60534,C635943) O(L0,P0,C158985.8) tempdb:0 props: [{}] ( PopIndScan cost:50406.55 T(L385.3457,P77.60534,C476957.2) O(L385.3457,P77.60534,C476957.2) tempdb:0 props: [{}] Gti2( IDX1 ) Gtt1( CB ) ) cost:50406.55 T(L385.3457,P77.60534,C476957.2) O(L385.3457,P77.60534,C476957.2) tempdb:0 order: none
) cost:66305.12 T(L385.3457,P77.60534,C635943) O(L0,P0,C158985.8) tempdb:0 order: none
******************************************************************************
DONE: Search Space Traversal for OptBlock0
******************************************************************************
The Abstract Plan (AP) of the final query execution plan:
( group_sorted ( i_scan IDX1 CB ) ) ( prop CB ( parallel 1 ) ( prefetch 128 ) ( lru ) )
To experiment with the optimizer behavior, this AP can be modified and then passed to the optimizer using the PLAN clause: SELECT/INSERT/DELETE/UPDATE ... PLAN '( ... )'.
The best global plan (Pop tree) :
FINAL PLAN ( total cost = 66305.12 ):
lio=385.3457 pio=77.60534 cpu=635943 tempdb=0
( PopEmit
proj: {{ CB.MID Gtg0.Gc4 } ,{ Gtg0.Gc3 Gtg0._virtualagg Gtg0._virtualagg } }
pred: [Tc{} Pe{}]
subs: {}
cost: 66305.12
I/O estimate : [
rowcount=7.388771
averagewidth=19
pages=1
prefetchpages=1
lio=0 pio=0 cpu=0
total lio=385.3457 total pio=77.60534 total cpu=635943
tempdb=0
]
Cache Strategy: [
prefetch=YES
iosize=131072 Bytes
bufreplace=LRU
]
order: none
( PopGroupSorted
proj: {{ CB.MID Gtg0.Gc4 } ,{ Gtg0.Gc3 Gtg0._virtualagg Gtg0._virtualagg } }
pred: [Tc{} Pe{{ CB.MID Gtg0.Gc4 } = 0}]
subs: {CB.MID ,Gtg0._virtualagg }
cost: 66305.12
I/O estimate : [
rowcount=7.388771
averagewidth=19
pages=1
prefetchpages=1
lio=0 pio=0 cpu=158985.8
total lio=385.3457 total pio=77.60534 total cpu=635943
tempdb=0
]
Cache Strategy: [
prefetch=YES
iosize=131072 Bytes
bufreplace=LRU
]
order: none
( PopIndScan index: Gti2( IDX1 )
table: Gtt1( CB )
proj: {{ CB.MID Gtg0.Gc4 } ,{ CB.ValuationDate } }
pred: [Tc{} Pe{{ CB.MID Gtg0.Gc4 } = 0}]
subs: {CB.MID ,CB.ValuationDate }
cost: 50406.55
I/O estimate : [
rowcount=158985.8
averagewidth=14
pages=383.3457
prefetchpages=383.3457
lio=385.3457 pio=77.60534 cpu=476957.2
scanlio=385.3457 scanpio=77.60534 scancpu=476957.2
tempdb=0
]
Cache Strategy: [
prefetch=YES
iosize=131072 Bytes
bufreplace=LRU
]
order: none )
)
)
QUERY PLAN FOR STATEMENT 1 (at line 2).
STEP 1
The type of query is DECLARE.
Total estimated I/O cost for statement 1 (at line 2): 0.
QUERY PLAN FOR STATEMENT 2 (at line 3).
Optimized using Serial Mode
STEP 1
The type of query is SELECT.
1 operator(s) under root
|ROOT:EMIT Operator (VA = 1)
|
| |SCALAR Operator (VA = 0)
Total estimated I/O cost for statement 2 (at line 3): 0.
QUERY PLAN FOR STATEMENT 3 (at line 5).
Optimized using Serial Mode
STEP 1
The type of query is SELECT.
3 operator(s) under root
|ROOT:EMIT Operator (VA = 3)
|
| |RESTRICT Operator (VA = 2)(0)(0)(0)(4)(0)
| |
| | |GROUP SORTED Operator (VA = 1)
| | | Evaluate Grouped MAXIMUM AGGREGATE.
| | |
| | | |SCAN Operator (VA = 0)
| | | | FROM TABLE
| | | | CB
| | | | Index : IDX1
| | | | Forward Scan.
| | | | Positioning by key.
| | | | Index contains all needed columns. Base table will not be read.
| | | | Keys are:
| | | | MID ASC
| | | | Using I/O Size 128 Kbytes for index leaf pages.
| | | | With LRU Buffer Replacement Strategy for index leaf pages.
Total estimated I/O cost for statement 3 (at line 5): 66305.
Parse and Compile Time 0.
Adaptive Server cpu time: 0 ms.
Statement: 2 Compile time resource usage: (est worker processes=0 proccache=0), Execution time resource usage: (worker processes=0 auxsdesc=0 plansize=2 proccache=3 proccache hwm=3 tempdb hwm=0)
==================== Lava Operator Tree ====================
Emit
(VA = 1)
r:1 er:300
l:0 el:100
p:0 ep:100
cpu: 0
/
Scalar
(0)
(VA = 0)
r:1 er:-1
l:0 el:-1
p:0 ep:-1
============================================================
Total actual I/O cost for this command: 0.
Total writes for this command: 0
Execution Time 0.
Adaptive Server cpu time: 0 ms. Adaptive Server elapsed time: 0 ms.
Statement: 3 Compile time resource usage: (est worker processes=0 proccache=122), Execution time resource usage: (worker processes=0 auxsdesc=0 plansize=6 proccache=9 proccache hwm=9 tempdb hwm=0)
==================== Lava Operator Tree ====================
Emit
(VA = 3)
r:1 er:8
cpu: 300
/
Restrict
(0)(0)(0)(4)(0)
(VA = 2)
r:1 er:8
/
GroupSorted
Grouping
(VA = 1)
r:1 er:8
/
IndexScan
IDX1
(VA = 0)
r:353666 er:158986
l:863 el:386
p:176 ep:78
============================================================
Table: CB scan count 1, logical reads: (regular=863 apf=0 total=863), physical reads: (regular=8 apf=168 total=176), apf IOs used=168
Total actual I/O cost for this command: 6126.
Total writes for this command: 0
Execution Time 3.
Adaptive Server cpu time: 135 ms. Adaptive Server elapsed time: 3653 ms.
(1 row affected)
MID
----------- -------------------
(1 row affected)