Skip to content

add support for QuerySet.explain() #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2024

Conversation

timgraham
Copy link
Collaborator

Example output:

>>> print(Question.objects.explain())
explainVersion: '1'
queryPlanner: {   'indexFilterSet': False,
    'maxIndexedAndSolutionsReached': False,
    'maxIndexedOrSolutionsReached': False,
    'maxScansToExplodeReached': False,
    'namespace': 'mysite.polls_question',
    'optimizedPipeline': True,
    'parsedQuery': {},
    'rejectedPlans': [],
    'winningPlan': {'stage': 'EOF'}}
executionStats: {   'allPlansExecution': [],
    'executionStages': {   'advanced': 0,
                           'executionTimeMillisEstimate': 0,
                           'isEOF': 1,
                           'nReturned': 0,
                           'needTime': 0,
                           'needYield': 0,
                           'restoreState': 0,
                           'saveState': 0,
                           'stage': 'EOF',
                           'works': 1},
    'executionSuccess': True,
    'executionTimeMillis': 0,
    'nReturned': 0,
    'totalDocsExamined': 0,
    'totalKeysExamined': 0}
command: {   '$db': 'mysite',
    'aggregate': 'polls_question',
    'cursor': {},
    'pipeline': [{'$match': {'$expr': {}}}]}
serverInfo: {   'gitVersion': 'b6513ce0781db6818e24619e8a461eae90bc94fc',
    'host': 'westbrook',
    'port': 27017,
    'version': '7.0.12'}
serverParameters: {   'internalDocumentSourceGroupMaxMemoryBytes': 104857600,
    'internalDocumentSourceSetWindowFieldsMaxMemoryBytes': 104857600,
    'internalLookupStageIntermediateDocumentMaxSizeBytes': 104857600,
    'internalQueryFacetBufferSizeBytes': 104857600,
    'internalQueryFacetMaxOutputDocSizeBytes': 104857600,
    'internalQueryFrameworkControl': 'trySbeRestricted',
    'internalQueryMaxAddToSetBytes': 104857600,
    'internalQueryMaxBlockingSortMemoryUsageBytes': 104857600,
    'internalQueryProhibitBlockingMergeOnMongoS': 0}

Fixes #28

# Explain the pipeline.
kwargs = {}
if verbosity := options.get("verbosity"):
kwargs["verbosity"] = verbosity
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kwargs will only have a field or zero. could verbosity be passed in a different way to database.command?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be verbosity = options.get("verbosity") and change the **kwargs line to **{"verbosity": verbosity} if verbosity else {}.

The current approach seems more extensible. For example, there's a comment kwarg that I didn't implement.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was trivial to support comment by adding a loop.

@timgraham timgraham merged commit 143079b into mongodb:main Aug 20, 2024
3 checks passed
@timgraham timgraham deleted the QuerySet.explain branch August 20, 2024 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement QuerySet.explain()
2 participants