{"title":"Table Keys and Performance in Business Central","url":"https://jonas-paprotka.de/docs/dev-itpro/administration/optimize-sql-table-keys-and-performance/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/administration/optimize-sql-table-keys-and-performance, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/administration/optimize-sql-table-keys-and-performance","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"AL code searches through a subset of the record in a table, consider what keys define the tables and write code that optimizes the keys.","date":"2021-04-01","meta":{"topic":"how-to"},"outline":[{"level":1,"title":"Table Keys and Performance in Business Central","slug":"table-keys-and-performance-in-business-central","at":0,"chars":2329},{"level":2,"title":"Defining Keys to Improve Performance","slug":"defining-keys-to-improve-performance","at":357,"chars":1797},{"level":2,"title":"Related information","slug":"related-information","at":2154,"chars":175}],"text":"# Table Keys and Performance in Business Central\nWhen you write AL code that searches through a subset of the records in a table, you must consider what keys are defined for the table and write code that optimizes for the keys. For example, the entries for a specific customer are usually a small subset of a table containing entries for all the customers.\n## Defining Keys to Improve Performance\nThe time that it takes to complete a loop through a subset of records depends on the size of the subset. If a subset cannot be located and read efficiently, then performance deteriorates.\nTo maximize performance, you must define the keys in the table that support the code that you run. You must then specify these keys correctly in your code.\nFor example, to retrieve the entries for a specific customer, you apply a filter to the Customer No. field in the Cust. Ledger Entry table. To run the code efficiently on Microsoft SQL Server, you must define a key in the table that has Customer No. as the first field.\nThe table could have the following keys:\n Entry No.\n Customer No.,Posting Date\n  The following is an example of code that finds a subset of records.\n```\nSETRANGE(\"Customer No.\",'1000');\nIF FIND('-') THEN\nREPEAT\nUNTIL NEXT = 0;\n```\nSQL Server automatically chooses which index to use in order to retrieve data in the most efficient way. SQL Server calculates the cost of retrieving data using different indexes and then chooses the path that has the smallest cost. For Business Central, that calculation is based only on the statistical distribution of values in a column.\nFor example, if a table contains 1000 rows and a column in the table contains either the value 0 or the value 1, then that column is said to have a low selectivity. If instead a column contained the values ranging from 1 to 500 then the column is said to have a high selectivity. In the following code example, SQL Server chooses an index that contains the HighSelectivityColumn and then sorts the rows by the LowSelectivityColumn.\n```\nSETCURRENTKEY(\"LowSelectivityColumn\");\nSETFILTER(\"LowSelectivityColumn\",'1');\nSETFILTER(\"HighSelectivityColumn\",'777');\nFIND('-')\n\n```\n## Related information\noptimize-sql-data-access\noptimize-sql-bulk-inserts\noptimize-sql-al-database-methods-and-performance-on-server\noptimize-sql-query-objects-and-performance"}