DocumentDB教程
DocumentDB SQL

DocumentDB 索引记录

默认情况下,一旦文档被添加到数据库中,DocumentDB 就会自动索引文档中的每个属性。但是,您可以控制并微调您自己的索引策略,从而在存在永远不需要索引的特定文档和/或属性时减少存储和处理开销。
告诉 DocumentDB 自动索引每个属性的默认索引策略适用于许多常见场景。但您也可以实施自定义策略,精确控制哪些内容被编入索引,哪些内容没有编入索引,以及其他与编入索引相关的功能。
DocumentDB 支持以下类型的索引-
哈希 范围

哈希

哈希索引可以有效地查询相等性,即在搜索给定属性等于精确值的文档时,而不是匹配小于、大于或介于之间的值范围。
您可以使用哈希索引执行范围查询,但 DocumentDB 将无法使用哈希索引来查找匹配的文档,而是需要依次扫描每个文档以确定范围查询是否应该选择它。
您将无法在只有哈希索引的属性上使用 ORDER BY 子句对文档进行排序。

范围

为属性定义的范围索引,DocumentDB 允许针对一系列值有效查询文档。它还允许您使用 ORDER BY 对该属性的查询结果进行排序。
DocumentDB 允许您在任何或所有属性上定义哈希和范围索引,从而实现高效的等式和范围查询,以及 ORDER BY。

索引政策

每个集合都有一个索引策略,它规定了每个文档的每个属性中的数字和字符串使用哪种类型的索引。
您还可以控制文档在添加到集合中时是否自动编入索引。 默认情况下启用自动索引,但您可以在添加文档时覆盖该行为,告诉 DocumentDB 不要索引该特定文档。 您可以禁用自动索引,这样默认情况下,文档在添加到集合时不会被索引。同样,您可以在文档级别覆盖它,并在将特定文档添加到集合时指示 DocumentDB 为其建立索引。这称为手动索引。

包括/排除索引

索引策略还可以定义应该在索引中包含或排除哪些路径。如果您知道文档的某些部分您从不查询并且您查询了某些部分,这将非常有用。
在这些情况下,您可以通过告诉 DocumentDB 仅索引添加到集合中的每个文档的那些特定部分来减少索引开销。

自动索引

我们来看一个简单的自动索引示例。
步骤 1-首先,我们创建一个名为自动索引的集合,并且没有明确提供策略,该集合使用默认索引策略,这意味着在此集合上启用了自动索引。
这里我们对数据库自链接使用基于 ID 的路由,因此在创建集合之前我们不需要知道它的资源 ID 或查询它。我们可以只使用数据库 ID,即 mydb。
第 2 步-现在让我们创建两个文件,它们的姓氏都是 Upston。
private async static Task AutomaticIndexing(DocumentClient client) {
   Console.WriteLine();
   Console.WriteLine("**** Override Automatic Indexing ****");
   // Create collection with automatic indexing
   var collectionDefinition = new DocumentCollection {
      Id = "autoindexing"
   };
	
   var collection = await client.CreateDocumentCollectionAsync("dbs/mydb",
      collectionDefinition);
   // Add a document (indexed)
   dynamic indexedDocumentDefinition = new {
      id = "MARK",
      firstName = "Mark",
      lastName = "Upston",
      addressLine = "123 Main Street",
      city = "Brooklyn",
      state = "New York",
      zip = "11229",
   };
	
   Document indexedDocument = await client
      .CreateDocumentAsync("dbs/mydb/colls/autoindexing", indexedDocumentDefinition);
		
   // Add another document (request no indexing)
   dynamic unindexedDocumentDefinition = new {
      id = "JANE",
      firstName = "Jane",
      lastName = "Upston",
      addressLine = "123 Main Street",
      city = "Brooklyn",
      state = "New York",
      zip = "11229",
   };
	
   Document unindexedDocument = await client
      .CreateDocumentAsync("dbs/mydb/colls/autoindexing", unindexedDocumentDefinition,
      new RequestOptions { IndexingDirective = IndexingDirective.Exclude });
   //Unindexed document won't get returned when querying on non-ID (or selflink) property
   var doeDocs = client.CreateDocumentQuery("dbs/mydb/colls/autoindexing", "SELECT *
      FROM c WHERE c.lastName = 'Doe'").ToList();
		
   Console.WriteLine("Documents WHERE lastName = 'Doe': {0}", doeDocs.Count);
   // Unindexed document will get returned when using no WHERE clause
   var allDocs = client.CreateDocumentQuery("dbs/mydb/colls/autoindexing",
      "SELECT * FROM c").ToList();
   Console.WriteLine("All documents: {0}", allDocs.Count);
	
   // Unindexed document will get returned when querying by ID (or self-link) property
	
   Document janeDoc = client.CreateDocumentQuery("dbs/mydb/colls/autoindexing",
      "SELECT * FROM c WHERE c.id = 'JANE'").AsEnumerable().FirstOrDefault();
   Console.WriteLine("Unindexed document self-link: {0}", janeDoc.SelfLink);
	
   // Delete the collection
	
   await client.DeleteDocumentCollectionAsync("dbs/mydb/colls/autoindexing");
}
对于 Mark Upston 来说,第一个被添加到集合中,然后根据默认索引策略立即自动索引。
但是当 Mark Upston 的第二个文档被添加时,我们已经通过 IndexingDirective.Exclude 传递了请求选项,它明确指示 DocumentDB 不索引这个文档,尽管集合的索引策略。
最后我们对这两个文档有不同类型的查询。
步骤 3-让我们从 CreateDocumentClient 调用 AutomaticIndexing 任务。
private static async Task CreateDocumentClient() {
   // Create a new instance of the DocumentClient 
   using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) { 
      await AutomaticIndexing(client); 
   } 
}
当上面的代码编译执行后,你会得到如下输出。
**** Override Automatic Indexing **** 
Documents WHERE lastName = 'Upston': 1 
All documents: 2 
Unindexed document self-link: dbs/kV5oAA==/colls/kV5oAOEkfQA=/docs/kV5oAOEkfQACA 
AAAAAAAAA==/
如您所见,我们有两个这样的文档,但查询仅返回一个用于 Mark 的文档,因为用于 Mark 的一个文档未编入索引。如果我们再次查询,没有 WHERE 子句来检索集合中的所有文档,那么我们会得到一个包含两个文档的结果集,这是因为没有 WHERE 子句的查询总是返回未索引的文档。
我们还可以通过 ID 或自链接检索未索引的文档。因此,当我们通过他的 ID MARK 查询 Mark 的文档时,我们看到 DocumentDB 返回了该文档,即使它没有在集合中编入索引。

手动索引

让我们看一个通过覆盖自动索引来手动索引的简单示例。
步骤 1-首先,我们将创建一个名为 manualindexing 的集合,并通过显式禁用自动索引来覆盖默认策略。这意味着,除非我们另有要求,否则添加到此集合中的新文档将不会被索引。
private async static Task ManualIndexing(DocumentClient client) {
   Console.WriteLine();
   Console.WriteLine("**** Manual Indexing ****");
   // Create collection with manual indexing
   var collectionDefinition = new DocumentCollection {
      Id = "manualindexing",
      IndexingPolicy = new IndexingPolicy {
         Automatic = false,
      },
   };
	
   var collection = await client.CreateDocumentCollectionAsync("dbs/mydb",
      collectionDefinition);
		
   // Add a document (unindexed)
   dynamic unindexedDocumentDefinition = new {
      id = "MARK",
      firstName = "Mark",
      lastName = "Doe",
      addressLine = "123 Main Street",
      city = "Brooklyn",
      state = "New York",
      zip = "11229",
   }; 
	
   Document unindexedDocument = await client
      .CreateDocumentAsync("dbs/mydb/colls/manualindexing", unindexedDocumentDefinition);
  
   // Add another document (request indexing)
   dynamic indexedDocumentDefinition = new {
      id = "JANE",
      firstName = "Jane",
      lastName = "Doe",
      addressLine = "123 Main Street",
      city = "Brooklyn",
      state = "New York",
      zip = "11229",
   };
	
   Document indexedDocument = await client.CreateDocumentAsync
      ("dbs/mydb/colls/manualindexing", indexedDocumentDefinition, new RequestOptions {
      IndexingDirective = IndexingDirective.Include });
   //Unindexed document won't get returned when querying on non-ID (or selflink) property
   var doeDocs = client.CreateDocumentQuery("dbs/mydb/colls/manualindexing",
      "SELECT * FROM c WHERE c.lastName = 'Doe'").ToList();
   Console.WriteLine("Documents WHERE lastName = 'Doe': {0}", doeDocs.Count);
	
   // Unindexed document will get returned when using no WHERE clause
	
   var allDocs = client.CreateDocumentQuery("dbs/mydb/colls/manualindexing",
      "SELECT * FROM c").ToList();
   Console.WriteLine("All documents: {0}", allDocs.Count);
	
   // Unindexed document will get returned when querying by ID (or self-link) property
	
   Document markDoc = client
      .CreateDocumentQuery("dbs/mydb/colls/manualindexing",
      "SELECT * FROM c WHERE c.id = 'MARK'")
      .AsEnumerable().FirstOrDefault();
   Console.WriteLine("Unindexed document self-link: {0}", markDoc.SelfLink);
   await client.DeleteDocumentCollectionAsync("dbs/mydb/colls/manualindexing");
}
第 2 步-现在我们将再次创建与以前相同的两个文档。这次我们不会为 Mark 的文档提供任何特殊的请求选项,因为集合的索引政策,该文档不会被索引。
第 3 步-现在当我们为 Mark 添加第二个文档时,我们使用 RequestOptions 和 IndexingDirective.Include 来告诉 DocumentDB 它应该索引这个文档,它覆盖了集合的索引策略,它说它不应该。
最后我们对这两个文档有不同类型的查询。
步骤 4-让我们从 CreateDocumentClient 调用 ManualIndexing 任务。
private static async Task CreateDocumentClient() {
   // Create a new instance of the DocumentClient 
   using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
      await ManualIndexing(client); 
   } 
}
当上面的代码被编译和执行时,你会收到以下输出。
**** Manual Indexing **** 
Documents WHERE lastName = 'Upston': 1 
All documents: 2 
Unindexed document self-link: dbs/kV5oAA==/colls/kV5oANHJPgE=/docs/kV5oANHJPgEBA 
AAAAAAAAA==/
同样,查询只返回两个文档中的一个,但这一次,它返回 Jane Doe,我们明确要求将其编入索引。但是和以前一样,没有 WHERE 子句的查询会检索集合中的所有文档,包括 Mark 的未索引文档。我们还可以通过其 ID 查询未编入索引的文档,即使它没有编入索引,DocumentDB 也会返回该 ID。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4