1

所以我在我的 DL 中的 CRUD 方法中有以下内容,它带回了一个照片 blob:

    public static MemberPhoto RetrievePhoto(string customerID)
    {
        Database db = DatabaseFactory.CreateDatabase(Config.DbConnectionString);

        using (DbCommand cmd = db.GetSqlStringCommand(string.Format(@" SELECT  customerID,
                                                                                p.Photo as PhotoBlob

                                                                         FROM 
                                                                         SomeTable t

                                                                         WHERE 
                                                                         t.PhotoID = (SELECT MAX(t.PhotoID) FROM SomeTable t
                                                                                      WHERE t.customerID = @{0} 
                                                                                      GROUP BY t.CustomerID)", "customerID")))
        {
            try
            {
                db.AddInParameter(cmd, "@customerID", DbType.String, customerID);

                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    if (reader.Read())
                    {
                        Photo photo = new Photo();
                        photo.LoadFromDataReader(reader);
                        return photo;
                    }
                }
            }

所以它比我直接从管理工作室运行慢大约 10 秒:

SELECT  customerID,
    t.Photo as PhotoBlob

    FROM 
    SomeTable t

    WHERE 
    t.PhotoID = (SELECT MAX(t.PhotoID) FROM SomeTable t
              WHERE t.customerID = '0000900595555' 
              GROUP BY t.CustomerID)

我不知道是不是因为读者需要一段时间才能阅读 blob 并将其带回来还是什么?如果这是问题,你如何加快速度?它在数据库的瞬间,我运行该查询,这是一个毫秒的结果计时。

更新:

更多信息; 表中的字段类型是该 BLOB 的 SQL Sever 中的 Image 类型。

我也运行了我的单元测试,同样的时间,即使它击中了我的 LoadFromReader:

[Test]
public void GetMemberPhoto_ByContractNumber_ReturnsAValidMemberPhoto()
{
    // Arrange
    const string customerID = "0044664176";

    // Act
    DTO.MemberPhoto memberPhoto = MemberPhotoCRUD.RetrieveMemberPhotoFromBlob(customerID);

    //Assert
    Assert.IsNotNull(memberPhoto);
    Assert.IsTrue(memberPhoto.CustomerID > 0);
    Assert.IsNotNull(memberPhoto.PhotoBinary.Length > 0);
}
4

2 回答 2

1

The select is wrong. Do not use string.Format and change WHERE t.customerID = @{0} to WHERE t.customerID = @customerID.

Now comment out the photo.LoadFromDataReader(reader); and run your test. If the time is close to management studio's then the issue is in your LoadFromDataReader method.

于 2012-11-08T06:07:41.723 回答
1

您正在返回图像的 blob。SSMS 实际上并没有获取所有数据 - 它会截断、和的大值[n][text],因为您通常不直接使用这些结果。听起来这是一个带宽问题。检查 blob 的大小 - 如果它非常大,这很可能。[n][varchar](max)imagevarbinary(max)

异常表现的其他可能性:

  • 不同的隔离级别
  • 不同的SET选择
  • 参数嗅探

另外-关于的评论string.Format是正确的;它不会对您造成任何伤害,但也没有任何有用的目的。就我个人而言,我只是@customerID直接写到查询中。否则它就像string.Format("this and {0}", "that")- 你不妨只使用"this and that".

也可能是在客户端处理图像的速度很慢。为了进行直接比较,您只需将数据获取为(例如) a byte[]。您的 SSMS 比较不会将数据处理为Photo对象。可能是是缓慢的操作,所以我会分析photo.LoadFromDataReader(reader).

于 2012-11-08T07:22:45.903 回答