mirror of
https://github.com/mongodb/mongo.git
synced 2024-11-30 17:10:48 +01:00
Can pick index with more key specificity than required by sort / find spec
This commit is contained in:
parent
878cffc529
commit
c326d4a508
12
db/jsobj.cpp
12
db/jsobj.cpp
@ -739,6 +739,13 @@ namespace mongo {
|
||||
}
|
||||
return b.done();
|
||||
}
|
||||
|
||||
/**
|
||||
sets element field names to empty string
|
||||
If a field in pattern is missing, it is omitted from the returned
|
||||
object. Unlike extractFieldsDotted, it does not return an empty
|
||||
object on missing pattern field.
|
||||
*/
|
||||
BSONObj BSONObj::extractFieldsUnDotted(BSONObj pattern) const {
|
||||
BSONObjBuilder b;
|
||||
BSONObjIterator i(pattern);
|
||||
@ -747,9 +754,8 @@ namespace mongo {
|
||||
if ( e.eoo() )
|
||||
break;
|
||||
BSONElement x = getField(e.fieldName());
|
||||
if ( x.eoo() )
|
||||
return BSONObj();
|
||||
b.appendAs(x, "");
|
||||
if ( !x.eoo() )
|
||||
b.appendAs(x, "");
|
||||
}
|
||||
return b.obj();
|
||||
}
|
||||
|
@ -566,6 +566,9 @@ namespace mongo {
|
||||
|
||||
/**
|
||||
sets element field names to empty string
|
||||
If a field in pattern is missing, it is omitted from the returned
|
||||
object. Unlike extractFieldsDotted, it does not return an empty
|
||||
object on missing pattern field.
|
||||
*/
|
||||
BSONObj extractFieldsUnDotted(BSONObj pattern) const;
|
||||
|
||||
|
20
db/query.cpp
20
db/query.cpp
@ -58,11 +58,11 @@ namespace mongo {
|
||||
while ( 1 ) {
|
||||
BSONElement ie = i.next();
|
||||
BSONElement se = s.next();
|
||||
if ( ie.eoo() ) {
|
||||
if ( !se.eoo() )
|
||||
return 0;
|
||||
if ( ie.eoo() && !se.eoo() )
|
||||
return 0;
|
||||
if ( ie.eoo() || se.eoo() )
|
||||
return direction;
|
||||
}
|
||||
|
||||
if ( strcmp( ie.fieldName(), se.fieldName() ) != 0 )
|
||||
return 0;
|
||||
|
||||
@ -146,6 +146,11 @@ namespace mongo {
|
||||
|
||||
set<string> queryFields;
|
||||
query.getFieldNames(queryFields);
|
||||
if ( queryFields.size() == 0 ) {
|
||||
DEV out() << "getIndexCursor fail " << ns << '\n';
|
||||
return auto_ptr<Cursor>();
|
||||
}
|
||||
|
||||
// regular query without order by
|
||||
for (int i = 0; i < d->nIndexes; i++ ) {
|
||||
BSONObj idxInfo = d->indexes[i].info.obj(); // { name:, ns:, key: }
|
||||
@ -153,7 +158,12 @@ namespace mongo {
|
||||
set<string> keyFields;
|
||||
idxKey.getFieldNames(keyFields);
|
||||
|
||||
if ( keyFields == queryFields ) {
|
||||
bool subset = true;
|
||||
for( set<string>::iterator j = queryFields.begin(); subset && j != queryFields.end(); ++j )
|
||||
if ( keyFields.count( *j ) == 0 )
|
||||
subset = false;
|
||||
|
||||
if ( subset ) {
|
||||
BSONObj q = query.extractFieldsUnDotted(idxKey);
|
||||
assert(q.objsize() != 0); // guard against a seg fault if details is 0
|
||||
|
||||
|
@ -42,12 +42,14 @@ f.drop();
|
||||
f.ensureIndex( { a: 1, b: 1 } );
|
||||
index( f.find().sort( { a: 1, b: 1 } ) );
|
||||
index( f.find().sort( { a: -1, b: -1 } ) );
|
||||
index( f.find().sort( { a: 1 } ) );
|
||||
index( f.find().sort( { a: 1 } ).hint( { a: 1, b: 1 } ) );
|
||||
noIndex( f.find().sort( { a: 1, b: -1 } ) );
|
||||
noIndex( f.find().sort( { b: 1, a: 1 } ) );
|
||||
|
||||
noIndex( f.find() );
|
||||
noIndex( f.find( { c: 1 } ) );
|
||||
noIndex( f.find( { a: 1 } ) ); // Once we enhance the query optimizer, this should get an index.
|
||||
index( f.find( { a: 1 } ) );
|
||||
index( f.find( { a: 1, b: 1 } ) );
|
||||
index( f.find( { b: 1, a: 1 } ) );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user