@@ -278,15 +278,16 @@ def sum_query_property_filter(client):
278278 # Execute sum aggregation query with filters
279279 completed_tasks = client .query (kind = "Task" ).add_filter ("done" , "=" , True )
280280 completed_tasks_query = client .aggregation_query (query = completed_tasks ).sum (
281- property_ref = "hours" ,
282- alias = "total_completed_sum_hours"
281+ property_ref = "hours" , alias = "total_completed_sum_hours"
283282 )
284283
285284 completed_query_result = completed_tasks_query .fetch ()
286285 for aggregation_results in completed_query_result :
287286 for aggregation_result in aggregation_results :
288287 if aggregation_result .alias == "total_completed_sum_hours" :
289- print (f"Total sum of hours in completed tasks is { aggregation_result .value } " )
288+ print (
289+ f"Total sum of hours in completed tasks is { aggregation_result .value } "
290+ )
290291 # [END datastore_sum_aggregation_query_with_filters]
291292 return tasks
292293
@@ -339,15 +340,16 @@ def avg_query_property_filter(client):
339340 # Execute average aggregation query with filters
340341 completed_tasks = client .query (kind = "Task" ).add_filter ("done" , "=" , True )
341342 completed_tasks_query = client .aggregation_query (query = completed_tasks ).avg (
342- property_ref = "hours" ,
343- alias = "total_completed_avg_hours"
343+ property_ref = "hours" , alias = "total_completed_avg_hours"
344344 )
345345
346346 completed_query_result = completed_tasks_query .fetch ()
347347 for aggregation_results in completed_query_result :
348348 for aggregation_result in aggregation_results :
349349 if aggregation_result .alias == "total_completed_avg_hours" :
350- print (f"Total average of hours in completed tasks is { aggregation_result .value } " )
350+ print (
351+ f"Total average of hours in completed tasks is { aggregation_result .value } "
352+ )
351353 # [END datastore_avg_aggregation_query_with_filters]
352354 return tasks
353355
@@ -375,9 +377,11 @@ def multiple_aggregations_query(client):
375377 [
376378 datastore .aggregation .CountAggregation (alias = "count_aggregation" ),
377379 datastore .aggregation .SumAggregation (
378- property_ref = "hours" , alias = "sum_aggregation" ),
380+ property_ref = "hours" , alias = "sum_aggregation"
381+ ),
379382 datastore .aggregation .AvgAggregation (
380- property_ref = "hours" , alias = "avg_aggregation" )
383+ property_ref = "hours" , alias = "avg_aggregation"
384+ ),
381385 ]
382386 )
383387
@@ -389,6 +393,96 @@ def multiple_aggregations_query(client):
389393 return tasks
390394
391395
396+ def explain_analyze_entity (client ):
397+ # [START datastore_query_explain_analyze_entity]
398+ # Build the query with explain_options
399+ # analzye = true to get back the query stats, plan info, and query results
400+ query = client .query (
401+ kind = "Task" , explain_options = datastore .ExplainOptions (analyze = True )
402+ )
403+
404+ # initiate the query
405+ iterator = query .fetch ()
406+
407+ # explain_metrics is only available after query is completed
408+ for task_result in iterator :
409+ print (task_result )
410+
411+ # get the plan summary
412+ plan_summary = iterator .explain_metrics .plan_summary
413+ print (f"Indexes used: { plan_summary .indexes_used } " )
414+
415+ # get the execution stats
416+ execution_stats = iterator .explain_metrics .execution_stats
417+ print (f"Results returned: { execution_stats .results_returned } " )
418+ print (f"Execution duration: { execution_stats .execution_duration } " )
419+ print (f"Read operations: { execution_stats .read_operations } " )
420+ print (f"Debug stats: { execution_stats .debug_stats } " )
421+ # [END datastore_query_explain_analyze_entity]
422+
423+
424+ def explain_entity (client ):
425+ # [START datastore_query_explain_entity]
426+ # Build the query with explain_options
427+ # by default (analyze = false), only plan_summary property is available
428+ query = client .query (kind = "Task" , explain_options = datastore .ExplainOptions ())
429+
430+ # initiate the query
431+ iterator = query .fetch ()
432+
433+ # get the plan summary
434+ plan_summary = iterator .explain_metrics .plan_summary
435+ print (f"Indexes used: { plan_summary .indexes_used } " )
436+ # [END datastore_query_explain_entity]
437+
438+
439+ def explain_analyze_aggregation (client ):
440+ # [START datastore_query_explain_analyze_aggregation]
441+ # Build the aggregation query with explain_options
442+ # analzye = true to get back the query stats, plan info, and query results
443+ all_tasks_query = client .query (kind = "Task" )
444+ count_query = client .aggregation_query (
445+ all_tasks_query , explain_options = datastore .ExplainOptions (analyze = True )
446+ ).count ()
447+
448+ # initiate the query
449+ iterator = count_query .fetch ()
450+
451+ # explain_metrics is only available after query is completed
452+ for task_result in iterator :
453+ print (task_result )
454+
455+ # get the plan summary
456+ plan_summary = iterator .explain_metrics .plan_summary
457+ print (f"Indexes used: { plan_summary .indexes_used } " )
458+
459+ # get the execution stats
460+ execution_stats = iterator .explain_metrics .execution_stats
461+ print (f"Results returned: { execution_stats .results_returned } " )
462+ print (f"Execution duration: { execution_stats .execution_duration } " )
463+ print (f"Read operations: { execution_stats .read_operations } " )
464+ print (f"Debug stats: { execution_stats .debug_stats } " )
465+ # [END datastore_query_explain_analyze_aggregation]
466+
467+
468+ def explain_aggregation (client ):
469+ # [START datastore_query_explain_aggregation]
470+ # Build the aggregation query with explain_options
471+ # by default (analyze = false), only plan_summary property is available
472+ all_tasks_query = client .query (kind = "Task" )
473+ count_query = client .aggregation_query (
474+ all_tasks_query , explain_options = datastore .ExplainOptions ()
475+ ).count ()
476+
477+ # initiate the query
478+ iterator = count_query .fetch ()
479+
480+ # get the plan summary
481+ plan_summary = iterator .explain_metrics .plan_summary
482+ print (f"Indexes used: { plan_summary .indexes_used } " )
483+ # [END datastore_query_explain_aggregation]
484+
485+
392486def main (project_id ):
393487 client = datastore .Client (project_id )
394488
0 commit comments