CosmosDb字符串搜索

时间:2018-10-27 07:11:31

标签: c# sql-server azure-cosmosdb

我在将SQL Server查询转换为CosmosDb查询时遇到麻烦。麻烦的是该搜索字段了。我在SQL Server中的原始表用于检索地址。当我在搜索框中键入一个方法时,该方法在SQL Server中称为存储过程,该方法使用格式地址搜索表中的字段。注意通配符。

ALTER  PROC [dbo].[CBAddressGet]
@TEST  VARCHAR(250)
AS
set concat_null_yields_null off 
set @test = @test + '%'
select ID,address, address0,address1,address2,town,postCode,lat,lon  from 
roads_db
where address  like @test
order by ADDRESS 

该表包含50,000行,并且查询在500毫秒内迅速返回。 我将表上传到了COsmosDb。现在,如果我尝试使用地址查询= '31 Village Road HarperVille'搜索带有文档的CosmosDb,例如:

Stopwatch sw = new Stopwatch();
sw.Start();
string sql = "SELECT TOP 3 * from c WHERE c.docType = 'address' AND  STARTSWITH(c.addresskey,'31 Vill')"; 
var results =
 docClient.CreateDocumentQuery<Address>(UriFactory.CreateDocumentCollectionUri("ct", "ops"),
           sql).AsEnumerable().ToList();
sw.Stop();
System.Console.WriteLine(sw.ElapsedMilliseconds);

这样的搜索最多需要45秒。某个地方一定有严重的错误。我的索引政策有误吗?

{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
    {
        "path": "/*",
        "indexes": [
            {
                "kind": "Range",
                "dataType": "Number",
                "precision": -1
            },
            {
                "kind": "Hash",
                "dataType": "String",
                "precision": -1
            }
        ]
    },
    {
        "path": "/bookedAt/?",
        "indexes": [
            {
                "kind": "Range",
                "dataType": "String",
                "precision": -1
            },
            {
                "kind": "Range",
                "dataType": "Number",
                "precision": -1
            }
        ]
    },
    {
        "path": "/bookingTime/?",
        "indexes": [
            {
                "kind": "Range",
                "dataType": "String",
                "precision": -1
            },
            {
                "kind": "Range",
                "dataType": "Number",
                "precision": -1
            }
        ]
    },
    {
        "path": "/timeMark/?",
        "indexes": [
            {
                "kind": "Range",
                "dataType": "String",
                "precision": -1
            },
            {
                "kind": "Range",
                "dataType": "Number",
                "precision": -1
            }
        ]
    }
],
"excludedPaths": []

}

数据库上的数据大小为5Gb。

1 个答案:

答案 0 :(得分:1)

我来自CosmosDB工程团队。请确保您在索引策略中定义的addressKey上具有范围索引。如果该字段未进行范围索引,则STARTSWITH效率不高。