2013-07-23 7 views
4

나는 몇 가지 코드를 다음과 같이 있습니다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 대 걸립니다. 조금 검색 한 후, 나는 독자 [ "TrailerId"]이 전체적으로 39s를 차지한다는 것을 알았습니다. 쿼리 자체는 매우 빠르게 실행됩니다!

"TC." "TrailerId"에서 헤더는 0.6s에서 실행하게, 독자 [ "TrailerId은"] 이제 에선 0ms를 취합니다

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

이는 SqlDataReader 개체 인덱서 코드의 버그인가? 왜 두 번째 버전이 첫 번째 버전보다 훨씬 빨리 작동하는지 이해할 수 없습니다.

+0

저장 프로 시저 –

답변

0

"예고편"열의 색인을 주기적으로 벗어나서 사용하려고 시도하십시오. 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 -... 
    } 
} 
0

차례 차례 인덱스를 사용하고 매번 변수를 선언하지 마십시오.
데이터 형식에 특정한 방법을 사용하십시오. trailerID 가정
는 INT32 위치 22.
VAR의 trailerId = 리더 [ "TrailerId"]를

Stopwatch sw = new Stopwatch(); 
Int32 trailerID; 
while (reader.Read()) 
{ 
    sw.Start(); 
    trailerId = reader.GetInt(22); 
    sw.Stop(); 
    Debug.WriteLine(trailerId + "-" + sw.ElapsedMilliseconds);//10s - 8s -... 
} 

;
TrailerId를 찾아 각 루프에 올바른 데이터 유형을 var에 할당해야합니다.

왜 TC가 차이를 만들어 냈는지 확실하지 않습니다.
대체로 var를 지정하기 위해 스키마를 찾는 것과 관련이 있습니다.