|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "9d2c1c5d", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "[](https://mongodb-developer.github.io/aggregation-pipeline-lab/)" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "3b936925-e295-489a-b508-2b99c0160217", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Aggregation Pipeline: explain()\n", |
| 17 | + " " |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "id": "4762f21c", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "## What does `explain()` do?\n", |
| 26 | + "\n", |
| 27 | + "`explain()` shows how MongoDB executes a query or aggregation pipeline. It is useful for understanding whether MongoDB is using an index and how much work the query performs.\n", |
| 28 | + "\n", |
| 29 | + "In this example, we will focus on two important fields:\n", |
| 30 | + "\n", |
| 31 | + "- `Index usage`: shows whether the query uses an index\n", |
| 32 | + "- `Execution time (ms)`: shows how long the query took to run\n", |
| 33 | + "\n", |
| 34 | + "We will run the same aggregation before and after creating an index, then compare the results." |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "markdown", |
| 39 | + "id": "dependent-boundary", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "## Startup code" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "id": "66ef28c4-f86b-4576-839e-100a7ae022c7", |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "// Import the MongoDB Driver using Maven\n", |
| 53 | + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", |
| 54 | + "%maven org.slf4j:slf4j-simple:1.7.36\n", |
| 55 | + "\n", |
| 56 | + "import com.mongodb.ExplainVerbosity;\n", |
| 57 | + "import com.mongodb.client.AggregateIterable;\n", |
| 58 | + "import com.mongodb.client.MongoClient;\n", |
| 59 | + "import com.mongodb.client.MongoClients;\n", |
| 60 | + "import com.mongodb.client.MongoCollection;\n", |
| 61 | + "import com.mongodb.client.MongoDatabase;\n", |
| 62 | + "import com.mongodb.client.model.Indexes;\n", |
| 63 | + "import org.bson.Document;\n", |
| 64 | + "\n", |
| 65 | + "import java.util.List;\n", |
| 66 | + "\n", |
| 67 | + "import static com.mongodb.client.model.Aggregates.match;\n", |
| 68 | + "import static com.mongodb.client.model.Filters.and;\n", |
| 69 | + "import static com.mongodb.client.model.Filters.eq;\n", |
| 70 | + "\n", |
| 71 | + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", |
| 72 | + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", |
| 73 | + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", |
| 74 | + "\n", |
| 75 | + "// Set your connection String\n", |
| 76 | + "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", |
| 77 | + "\n", |
| 78 | + "// Define our database and collection.\n", |
| 79 | + "MongoClient mongoClient = null;\n", |
| 80 | + "try {\n", |
| 81 | + " mongoClient = MongoClients.create(connectionString);\n", |
| 82 | + "} catch (Exception e) {\n", |
| 83 | + " System.out.println(e);\n", |
| 84 | + "}\n", |
| 85 | + "\n", |
| 86 | + "MongoDatabase library = mongoClient.getDatabase(\"library\");\n", |
| 87 | + "MongoCollection<Document> books = library.getCollection(\"books\");" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "id": "d98078a6", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## Before creating the index\n", |
| 96 | + "\n", |
| 97 | + "First, we run the aggregation with `explain()` to check the execution time and whether MongoDB uses an index." |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": null, |
| 103 | + "id": "ab8508aa", |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "var filter = and(\n", |
| 108 | + " eq(\"title\", \"Collins Gem Italian Dictionary, 5e\"),\n", |
| 109 | + " eq(\"year\", 2001)\n", |
| 110 | + ");\n", |
| 111 | + "\n", |
| 112 | + "var explainResult = books.aggregate(\n", |
| 113 | + " List.of(match(filter))\n", |
| 114 | + ").explain(ExplainVerbosity.EXECUTION_STATS);\n", |
| 115 | + "\n", |
| 116 | + "System.out.println(\"Execution time (ms): \" +\n", |
| 117 | + " explainResult.get(\"executionStats\", Document.class).get(\"executionTimeMillis\"));\n", |
| 118 | + "\n", |
| 119 | + "System.out.println(\"Index usage: \" +\n", |
| 120 | + " explainResult.get(\"queryPlanner\", Document.class)\n", |
| 121 | + " .get(\"winningPlan\", Document.class)\n", |
| 122 | + " .toJson()\n", |
| 123 | + " .contains(\"IXSCAN\"));" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "id": "c15e4530", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "## Create an index\n", |
| 132 | + "\n", |
| 133 | + "Now, we create an index on the fields used in the filter and list the indexes before and after the operation." |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": null, |
| 139 | + "id": "31a0c996", |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
| 142 | + "source": [ |
| 143 | + "System.out.println(\"## Listing indexes\");\n", |
| 144 | + "books.listIndexes().forEach(System.out::println);\n", |
| 145 | + "\n", |
| 146 | + "System.out.println(\"Creating new index...\");\n", |
| 147 | + "books.createIndex(Indexes.ascending(\"title\", \"year\"));\n", |
| 148 | + "\n", |
| 149 | + "System.out.println(\"## Listing indexes\");\n", |
| 150 | + "books.listIndexes().forEach(System.out::println);" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "id": "7604272c", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "## After creating the index\n", |
| 159 | + "\n", |
| 160 | + "Finally, we run the same aggregation again and compare the execution time and index usage." |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "id": "aea847d7", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [ |
| 170 | + "var explainResult = books.aggregate(\n", |
| 171 | + " List.of(match(filter))\n", |
| 172 | + ").explain(ExplainVerbosity.EXECUTION_STATS);\n", |
| 173 | + "\n", |
| 174 | + "System.out.println(\"Execution time (ms): \" +\n", |
| 175 | + " explainResult.get(\"executionStats\", Document.class).get(\"executionTimeMillis\"));\n", |
| 176 | + "\n", |
| 177 | + "System.out.println(\"Index usage: \" +\n", |
| 178 | + " explainResult.get(\"queryPlanner\", Document.class)\n", |
| 179 | + " .get(\"winningPlan\", Document.class)\n", |
| 180 | + " .toJson()\n", |
| 181 | + " .contains(\"IXSCAN\"));" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "markdown", |
| 186 | + "id": "f756ad70", |
| 187 | + "metadata": {}, |
| 188 | + "source": [ |
| 189 | + "## Summary\n", |
| 190 | + "\n", |
| 191 | + "With `explain()`, we can compare how MongoDB executes the same aggregation before and after creating an index. For basic performance analysis, two useful things to check are index usage and execution time." |
| 192 | + ] |
| 193 | + } |
| 194 | + ], |
| 195 | + "metadata": { |
| 196 | + "kernelspec": { |
| 197 | + "display_name": "Java", |
| 198 | + "language": "java", |
| 199 | + "name": "java" |
| 200 | + }, |
| 201 | + "language_info": { |
| 202 | + "codemirror_mode": "java", |
| 203 | + "file_extension": ".jshell", |
| 204 | + "mimetype": "text/x-java-source", |
| 205 | + "name": "java", |
| 206 | + "pygments_lexer": "java", |
| 207 | + "version": "21.0.5+11-LTS" |
| 208 | + } |
| 209 | + }, |
| 210 | + "nbformat": 4, |
| 211 | + "nbformat_minor": 5 |
| 212 | +} |
0 commit comments