Skip to content
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

Add efficient count method for aiida.orm.groups.Group #2567

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Mar 4, 2019

Fixes #1948

Before the only way to get the number of entries in a Group was
through the iterator nodes and call len() on it. However, this is
almost always not the fastest way. Given that the count is used often in
for example verdi group list -C it is worthwhile to have a count
method that operates as direct on the database layer as possible.

@sphuber sphuber requested a review from giovannipizzi March 4, 2019 17:47
@coveralls
Copy link

coveralls commented Mar 4, 2019

Coverage Status

Coverage decreased (-0.3%) to 70.089% when pulling 21934a9 on sphuber:fix_1948_efficient_group_count into 28b0c82 on aiidateam:provenance_redesign.

"""
from aiida.backends.sqlalchemy import get_scoped_session
session = get_scoped_session()
return session.query(self.MODEL_CLASS).join(self.MODEL_CLASS.dbnodes).filter(DbGroup.id == self.pk).count()
Copy link
Member

Choose a reason for hiding this comment

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

Can't this be just piped to len(self.nodes)? This, below, should do the same thing? (see len in the property returned by nodes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what I was originally using, but at least for the SqlA implementation it was a factor 8 slower on my production database

Copy link
Member

Choose a reason for hiding this comment

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

ok

Before the only way to get the number of entries in a `Group` was
through the iterator `nodes` and call `len()` on it. However, this is
almost always not the fastest way. Given that the count is used often in
for example `verdi group list -C` it is worthwhile to have a `count`
method that operates as direct on the database layer as possible.
@sphuber sphuber force-pushed the fix_1948_efficient_group_count branch from 3d18ee7 to 21934a9 Compare March 4, 2019 22:52
@sphuber sphuber merged commit 2c54be6 into aiidateam:provenance_redesign Mar 5, 2019
@sphuber sphuber deleted the fix_1948_efficient_group_count branch March 5, 2019 06:40
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.

3 participants