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

Avoiding full table scan when looking for inclusions. #677

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions flask_restless/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ def _handle_validation_exception(self, exception):
current_app.logger.exception(str(exception))
return errors_response(400, errors)

def get_all_inclusions(self, instance_or_instances):
def get_all_inclusions(self, instance_or_instances, page_size=None):
"""Returns a list of all the requested included resources
associated with the given instance or instances of a SQLAlchemy
model.
Expand All @@ -1407,13 +1407,18 @@ def get_all_inclusions(self, instance_or_instances):
contains a list of the :exc:`SerializationException` objects
that caused it.

``page_size`` is an integer with the size of the result set.
this is to avoid full table scan to find data to include.
"""
# If `instance_or_instances` is actually just a single instance
# of a SQLAlchemy model, get the resources to include for that
# one instance. Otherwise, collect the resources to include for
# each instance in `instances`.
if isinstance(instance_or_instances, Query):
instances = instance_or_instances
if page_size and isinstance(page_size, int):
instances = instance_or_instances.limit(page_size)
else:
instances = instance_or_instances
to_include = set(chain(map(self.resources_to_include, instances)))
else:
instance = instance_or_instances
Expand Down Expand Up @@ -1577,10 +1582,12 @@ def _get_resource_helper(self, resource, primary_resource=None,
# includer = Includer(resource)
# includes = includer.generate_includes(resource)
# result['includes'] = includes

# Include any requested resources in a compound document.
try:
included = self.get_all_inclusions(resource)
page_size = None
if result and result.get('data', None):
page_size = len(result.get('data'))
included = self.get_all_inclusions(resource, page_size=page_size)
except MultipleExceptions as e:
# By the way we defined `get_all_inclusions()`, we are
# guaranteed that each of the underlying exceptions is a
Expand Down Expand Up @@ -1729,7 +1736,13 @@ def _get_collection_helper(self, resource=None, relation_name=None,
instances = search_items
# Include any requested resources in a compound document.
try:
included = self.get_all_inclusions(instances)
page_size = None
try:
if hasattr(paginated, 'items'):
page_size = len(paginated.items)
except (TypeError, UnboundLocalError):
pass
included = self.get_all_inclusions(instances, page_size=page_size)
except MultipleExceptions as e:
# By the way we defined `get_all_inclusions()`, we are
# guaranteed that each of the underlying exceptions is a
Expand Down