{"section":"tutorials","requestedLocale":"en","requestedSlug":"indexes-in-master-data","locale":"en","slug":"indexes-in-master-data","path":"docs/en/tutorials/master-data/master-data-v1-settings/indexes-in-master-data.md","branch":"main","content":"An index in Master Data is a reference to one or more fields used to optimize queries on a data entity. Using indexes allows you to search for records more quickly.\n\nTo illustrate, imagine the index of a book, which lists the themes contained in the book and the page number for each theme. To search, instead of having to go through all the pages of the book to find the result you want, you can access the index and go straight to the theme you want. Likewise, the index in Master Data provides a more efficient method for finding documents.\n\n## Configuring indexes\n\nFollow the instructions below to define one or more fields as indexes in Master Data v1:\n\n1. In the VTEX Admin, go to **Store Settings > Storefront > Master Data**.\n2. Click the gear icon <i class=\"fas fa-cog\"></i> in the application where you want to add the index.\n3. Click **Data structure**.\n4. Click the **Index** tab.\n5. Click `Add`.\n6. Complete the following fields:\n\n    * **Name:** Name of the index.\n    * **Unique values:** Option to block new records with repeated values for the index(es) created. See the section [Restricting values per document (alternative key)](#restricting-values-per-document-alternative-key) for more details.\n    * **Data entity:** Name of the data entity containing the fields to be configured as indexes.\n    * **Fields:** Name of one or more fields that will be configured as indexes. If you add more than one field name, separate them with a comma. Example: `firstName,document`.\n7. Click `Save`.\n\n## Use cases\n\nLearn more about the main scenarios for using indexes in Master Data in the following sections.\n\n### Queries of up to a thousand documents\n\nThe use of indexes is recommended for searches that return results of up to a thousand documents. If searches for an index frequently return more than a thousand documents, you can obtain better search performance by indexing the field, i.e., [configuring it as a filter in the data entity](/en/docs/tutorials/filtering-data-on-master-data).\n\n### Restricting values per document (alternative key)\n\nThe index configuration allows you to block one or more fields to prevent records with repeated values. These fields act as alternative keys, providing alternative values to identify a record.\n\nThe alternative key is configured via the **Unique values** field in [Index Settings](#configuring-indexes). Its behavior varies depending on the number of fields included in the index. The following sections show the alternative key's behavior in each case.\n\n#### Only one field in the index\n\nIf only one field is configured as an index, the alternative key will not allow a value that has already been entered in that field to be entered again.\n\nFor example, the **Customer** table has two columns: **ID** and **Name**. To make searching easier, the **Name** field (`firstName`) can be configured as an index and marked as **Unique values**, as illustrated below:\n\n![indices-1-en](https://cdn.statically.io/gh/vtexdocs/help-center-content/refs/heads/main/docs/en/tutorials/master-data/master-data-v1-settings/indexes-in-master-data_1.png)\n\nAs a result, it will not be possible to enter a name that has already been entered in **Customer**:\n\n![indices-2-en](https://cdn.statically.io/gh/vtexdocs/help-center-content/refs/heads/main/docs/en/tutorials/master-data/master-data-v1-settings/indexes-in-master-data_2.png)\n\nIn the example above, as there is already a record with the **Name** equal to **Mary**, you cannot add another one with the same value.\n\n> ⚠️ Use the **Unique values** option with caution. If a value that you want to allow to be repeated is configured as unique, the system will block duplicate records, preventing another one from being entered.\n\n#### Multiple fields in the index\n\nIf there is more than one field in the index, the alternative key will prevent a set of values from being repeated in the selected fields.\n\nFor example, imagine that the **Customer** entity has three fields: **ID**, **Name** and **Document**. In the index configuration, under **Fields**, you enter Name (`firstName`) and Document (`document`), as shown below:\n\n![indices-3-en](https://cdn.statically.io/gh/vtexdocs/help-center-content/refs/heads/main/docs/en/tutorials/master-data/master-data-v1-settings/indexes-in-master-data_3.png)\n\nIn this case, the **Name** value can be repeated, or the **Document** value can be repeated, but the combination of the two values cannot be repeated. In other words, there can be no record that repeats the same pair of values (such as identical **Name** and **Document**).\n\n![indices-4-en](https://cdn.statically.io/gh/vtexdocs/help-center-content/refs/heads/main/docs/en/tutorials/master-data/master-data-v1-settings/indexes-in-master-data_4.png)\n\nIn the example above, there is already a record with the **Name** as **Mary**. You can include another record with the same **Name** as long as the **Document** value is different.\n\n> ⚠️ If the same combination of values repeats, the record will not be saved. Use the **Unique values** option only when you are certain that a set of values will not be repeated in different records."}