From 4d3100a66d881093cb1d7ae1a1af481845585012 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 26 Feb 2009 15:09:44 -0500 Subject: [PATCH] More accurate nScanned recording with plan --- db/queryoptimizer.cpp | 8 +++++++- dbtests/queryoptimizertests.cpp | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/db/queryoptimizer.cpp b/db/queryoptimizer.cpp index 451114d2575..d4da09bd532 100644 --- a/db/queryoptimizer.cpp +++ b/db/queryoptimizer.cpp @@ -302,19 +302,24 @@ namespace mongo { } int nScanned = 0; + int nScannedBackup = 0; while( 1 ) { ++nScanned; unsigned errCount = 0; + bool first = true; for( vector< shared_ptr< QueryOp > >::iterator i = ops.begin(); i != ops.end(); ++i ) { QueryOp &op = **i; nextOp( op ); if ( op.complete() ) { + if ( first ) + nScanned += nScannedBackup; if ( plans_.mayRecordPlan_ && op.mayRecordPlan() ) op.qp().registerSelf( nScanned ); return *i; } if ( op.error() ) ++errCount; + first = false; } if ( errCount == ops.size() ) break; @@ -332,7 +337,8 @@ namespace mongo { } plans_.mayRecordPlan_ = true; plans_.usingPrerecordedPlan_ = false; - // TODO Don't write wrong nScanned if we complete on another plan. + nScannedBackup = nScanned; + nScanned = 0; } } return ops[ 0 ]; diff --git a/dbtests/queryoptimizertests.cpp b/dbtests/queryoptimizertests.cpp index d811fe59223..c39d322dbd2 100644 --- a/dbtests/queryoptimizertests.cpp +++ b/dbtests/queryoptimizertests.cpp @@ -796,6 +796,7 @@ namespace QueryOptimizerTests { ScanOnlyTestOp op; s.runOp( op ); ASSERT( fromjson( "{$natural:1}" ).woCompare( indexForPattern( ns(), s.fbs().pattern( BSON( "b" << 1 ) ) ) ) == 0 ); + ASSERT_EQUALS( 1, nScannedForPattern( ns(), s.fbs().pattern( BSON( "b" << 1 ) ) ) ); QueryPlanSet s2( ns(), BSON( "a" << 4 ), BSON( "b" << 1 ) ); TestOp op2; @@ -852,6 +853,7 @@ namespace QueryOptimizerTests { theDataFileMgr.insert( ns(), one ); deleteObjects( ns(), BSON( "a" << 1 ), false ); ASSERT( BSON( "a" << 1 ).woCompare( indexForPattern( ns(), FieldBoundSet( ns(), BSON( "a" << 1 ) ).pattern() ) ) == 0 ); + ASSERT_EQUALS( 2, nScannedForPattern( ns(), FieldBoundSet( ns(), BSON( "a" << 1 ) ).pattern() ) ); } }; @@ -908,6 +910,7 @@ namespace QueryOptimizerTests { assembleRequest( ns(), QUERY( "b" << 99 << "a" << GTE << 0 ).obj, 2, 0, 0, 0, m2 ); runQuery( m2, ss ); ASSERT( BSON( "a" << 1 ).woCompare( indexForPattern( ns(), FieldBoundSet( ns(), BSON( "b" << 0 << "a" << GTE << 0 ) ).pattern() ) ) == 0 ); + ASSERT_EQUALS( 2, nScannedForPattern( ns(), FieldBoundSet( ns(), BSON( "b" << 0 << "a" << GTE << 0 ) ).pattern() ) ); } };