By default, the query does not get executed and the explain metrics object
contains only the planning statisticsV1\ExplainMetrics.
If the request was configured with the ExplainOptions objectAnalyzeattribute to true
the request then also gets executed, including the ExecutionStats on the ExplainMetrics
object
Contrary to looping through the result of run query, this method caches the first metrics
to avoid variations when analyzing said metrics.
Example:
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Datastore\V1\ExplainOptions;
$datastore = new DatastoreClient();
explainOptions = (new ExplainOptions())->setAnalyze(false);
$queryOptions = [
'explainOptions' => $explainOptions
];
// The query does not get executed
$res = $datastore->runQuery($query, $queryOptions);
$explainMetrics = $res->getExplainMetrics();
// This is populated
$explainMetrics->planningSummary
// This is not populated
$explainMetrics->executionStats
Example:
explainOptions = (new ExplainOptions())->setAnalyze(true);
$queryOptions = [
'explainOptions' => $explainOptions
];
// The query does not get executed
$res = $datastore->runQuery($query, $queryOptions);
$explainMetrics = $res->getExplainMetrics();
// This is populated
$explainMetrics->getPlanSummary()
// This is also populated
$explainMetrics->getExecutionStats()
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Datastore Client - Class EntityIterator (1.33.1)\n\nVersion latestkeyboard_arrow_down\n\n- [1.33.1 (latest)](/php/docs/reference/cloud-datastore/latest/EntityIterator)\n- [1.33.0](/php/docs/reference/cloud-datastore/1.33.0/EntityIterator)\n- [1.32.3](/php/docs/reference/cloud-datastore/1.32.3/EntityIterator)\n- [1.31.0](/php/docs/reference/cloud-datastore/1.31.0/EntityIterator)\n- [1.30.0](/php/docs/reference/cloud-datastore/1.30.0/EntityIterator)\n- [1.29.2](/php/docs/reference/cloud-datastore/1.29.2/EntityIterator)\n- [1.28.2](/php/docs/reference/cloud-datastore/1.28.2/EntityIterator)\n- [1.26.0](/php/docs/reference/cloud-datastore/1.26.0/EntityIterator)\n- [1.25.0](/php/docs/reference/cloud-datastore/1.25.0/EntityIterator)\n- [1.24.4](/php/docs/reference/cloud-datastore/1.24.4/EntityIterator)\n- [1.23.0](/php/docs/reference/cloud-datastore/1.23.0/EntityIterator)\n- [1.22.1](/php/docs/reference/cloud-datastore/1.22.1/EntityIterator)\n- [1.21.2](/php/docs/reference/cloud-datastore/1.21.2/EntityIterator)\n- [1.19.0](/php/docs/reference/cloud-datastore/1.19.0/EntityIterator)\n- [1.18.1](/php/docs/reference/cloud-datastore/1.18.1/EntityIterator)\n- [1.17.1](/php/docs/reference/cloud-datastore/1.17.1/EntityIterator) \nReference documentation and code samples for the Cloud Datastore Client class EntityIterator.\n\nIterates over a set of [Entity](/php/docs/reference/cloud-datastore/latest/Entity) items.\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Datastore\n\nMethods\n-------\n\n### moreResultsType\n\nSee also:\n\n- [MoreResultsType Documentation](https://cloud.google.com/datastore/docs/reference/rest/v1/projects/runQuery#MoreResultsType)\n\n### getExplainMetrics\n\nReturns a ExplainMetrics object from the query.\n\nBy default, the query does not get executed and the explain metrics object\ncontains only the planning statistics [V1\\\\ExplainMetrics](/php/docs/reference/cloud-datastore/latest/V1.ExplainMetrics).\n\nIf the request was configured with the ExplainOptions object `Analyze` attribute to true\nthe request then also gets executed, including the ExecutionStats on the ExplainMetrics\nobject\n\nContrary to looping through the result of run query, this method caches the first metrics\nto avoid variations when analyzing said metrics.\n\nExample: \n\n use Google\\Cloud\\Datastore\\DatastoreClient;\n use Google\\Cloud\\Datastore\\V1\\ExplainOptions;\n\n $datastore = new DatastoreClient();\n\n explainOptions = (new ExplainOptions())-\u003esetAnalyze(false);\n $queryOptions = [\n 'explainOptions' =\u003e $explainOptions\n ];\n\n // The query does not get executed\n $res = $datastore-\u003erunQuery($query, $queryOptions);\n\n $explainMetrics = $res-\u003egetExplainMetrics();\n\n // This is populated\n $explainMetrics-\u003eplanningSummary\n\n // This is not populated\n $explainMetrics-\u003eexecutionStats\n\nExample: \n\n explainOptions = (new ExplainOptions())-\u003esetAnalyze(true);\n $queryOptions = [\n 'explainOptions' =\u003e $explainOptions\n ];\n\n // The query does not get executed\n $res = $datastore-\u003erunQuery($query, $queryOptions);\n\n $explainMetrics = $res-\u003egetExplainMetrics();\n\n // This is populated\n $explainMetrics-\u003egetPlanSummary()\n\n // This is also populated\n $explainMetrics-\u003egetExecutionStats()"]]