c# - SqlDataReader 索引器性能

标签 c# .net performance sqldatareader

我有这样的代码:

using (var cmd = TransicsCommand.GetFleetCommand())
{
    cmd.CommandText = @"
                        SELECT dr.DeviceId, dr.DeviceTruckRelationId, dr.TruckId, dr.RelationCreatedOn,
                        dl.DriverLoginId, dl.DriverId, dl.UserType, dl.LoginType, dl.SecondsSince DriverLoginCreated,
                        Action.ActionId, Action.ActionTimestamp, Action.UserType actionusertype, Action.TripreportId,
                        DeviceHeaderData.DeviceHeaderid, DeviceHeaderData.Odo, DeviceHeaderData.X, DeviceHeaderData.Y,
                        DeviceHeaderData.ValidPosition, DeviceHeaderData.Tfu,
                        DeviceHeaderData.FuelPercentage, DeviceHeaderData.Speed, 
                        InstructionsetAction.VersionId,
                        tc.CouplingId, tc.TrailerId, tc.CouplingEvent, tc.TrailerEntry, tc.SecondsSince
                        FROM TripReport.Action Action
                        INNER JOIN DeviceHeader.Data DeviceHeaderData ON Action.DeviceHeaderId = DeviceHeaderData.DeviceHeaderId
                        INNER JOIN Instructionset.Action InstructionsetAction  ON InstructionsetAction.ActionId = Action.ActionId
                        INNER JOIN DeviceHeader.Truck dht ON Action.DeviceHeaderId = dht.DeviceHeaderId
                        INNER JOIN Device.TruckRelation dr ON dht.DeviceRelationId = dr.DeviceTruckRelationId 
                        LEFT OUTER JOIN [DeviceHeader].[LoginSession] dhls ON dhls.DeviceHeaderId = dht.DeviceHeaderId
                        LEFT OUTER JOIN [LogIn].DriverLogin as dl ON dhls.DriverLoginId = dl.DriverLoginId
                        LEFT OUTER JOIN [DeviceHeader].[TrailerCoupling] dhtc ON dhtc.DeviceHeaderId = dht.DeviceHeaderId
                        LEFT OUTER JOIN [Trailer].[Coupling] as tc ON dhtc.CouplingId = tc.CouplingId ";

    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {   
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var trailerId = reader["TrailerId"];
            sw.Stop();
            Debug.WriteLine(trailerId + "-" + sw.ElapsedMilliseconds);//10s - 8s -...
        }
    }
}

这段代码需要 40 秒。搜索了一下,发现规则reader["TrailerId"]一共耗时39s,查询本身运行速度非常快!

删除“TC”。来自“TrailerId”的 header 使其在 0.6 秒内运行,阅读器 [“TrailerId”] 现在只需要 0 毫秒:

SELECT ..., tc.CouplingId, TrailerId,...

这是 sqldatareader 索引器代码中的错误吗?我不明白为什么第二个版本比第一个版本运行得快得多。

最佳答案

尝试从循环中获取“TrailerId”列的索引并在内部使用它; afaik 它让这个数字在记录中寻找每个,比如

using (var reader = cmd.ExecuteReader())
{
    int idx = -1;
    while (reader.Read())
    {   
        if (idx==-1) idx = reader.GetOrdinal("TrailerId");
        Stopwatch sw = new Stopwatch();
        sw.Start();
        var trailerId = reader[idx];
        sw.Stop();
        Debug.WriteLine(trailerId + "-" + sw.ElapsedMilliseconds);//10s - 8s -...
    }
}

关于c# - SqlDataReader 索引器性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17810660/

相关文章:

c - C 嵌入式软件中的查找表与开关

Silverlight GUI 中的 C# 代码验证

c# - 在 C# 应用程序之间流式传输数据

.net - 将 VB6 容器类升级到 VB.NET

c# - .NET 引用(未找到方法)

mysql - 加快 MySQL 查询速度

c# - Entity Framework 中的简单查询性能非常差

c# - Microsoft Sam,SAPI 替代品

c# - 将泛型方法中的类型转换为不同的类型以进行内部泛型方法调用

c# - 从代码生成映射 View - EF6