【问题标题】:Is using a TableEntity with only the desired property equivalent to a projection query?是否使用仅具有与投影查询等效的所需属性的 TableEntity?
【发布时间】:2014-03-19 13:07:06
【问题描述】:

我有一系列由 TableEntity 表示的Reports。它有许多不同的属性,但通常我只需要ReportID 属性。

public class ReportEntity : TableEntity
{
    public ReportIDEntity()
    {}

    internal ReportIDEntity(Guid reportID, .../*other properties*/)
    {
        ReportID = reportID;
        //assign other properties
    }

    public Guid ReportID { get; set; }

    //other properties
}

我为此编写了一个投影查询,这样我就不必为了获取 ID 而检索整个实体:

const String __ID_COLUMN = "ReportID"; //yuck
IEnumerable<Guid> ids =
_cloudTable_.ExecuteQuery(
new TableQuery<DynamicTableEntity>().Where(
    TableQuery.GenerateFilterCondition(
        "PartitionKey", QueryComparisons.Equal, partitionKey))
    .Select(new[] { __ID_COLUMN }),
    (key, rowKey, timestamp, properties, etag) => properties[__ID_COLUMN].GuidValue.Value);

但是,这很丑(我需要将属性名称指定为字符串来检索它,而且代码很长)。

如果我创建一个只有 ReportID 属性的 TableEntity 并查询它怎么办? 这会检索所有数据,还是会像投影查询一样精简(带宽方面)?

public class ReportIDEntity : TableEntity
{
    public ReportIDEntity()
    {}

    internal ReportIDEntity(Guid reportID)
    {
        ReportID = reportID;
    }

    public Guid ReportID { get; set; }
}

public class ReportEntity : ReportIDEntity
{
    public ReportEntity()
    {}

    internal ReportEntity(Guid reportID, .../*other properties*/)
     : base(reportID)
    {
        //assign other properties
    }
    //other properties
}

然后查询将是:

IEnumerable<Guid> reportEntities =
_cloudTable_.ExecuteQuery(
new TableQuery<ReportIDEntity>().Where(
    TableQuery.GenerateFilterCondition(
        "PartitionKey", QueryComparisons.Equal, partitionKey)))
.Select(e => e.ReportID);

【问题讨论】:

  • 相信查询会返回所有属性。确认这一点的一种简单方法是使用 fiddler 之类的东西来拦截查询和返回的内容。
  • @hocho 我用提琴手测试过,是的,它确认了接受的答案。

标签: c# azure azure-storage azure-table-storage


【解决方案1】:

要回答您的问题,使用Query Projection 效率更高,因为它是服务器端操作,并且表服务将仅返回仅包含ReportID 属性(属性)的实体,因此网络上的数据流较少。当您使用不带投影的查询时,将返回所有属性,并且在反序列化过程中,客户端将丢弃除ReportID 之外的所有其他属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 2017-02-16
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    相关资源
    最近更新 更多