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

Improve query efficiency when calculating ref_counts #79

Merged
merged 1 commit into from May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion sir/schema/__init__.py
Expand Up @@ -76,7 +76,7 @@
F("iso2", "iso_3166_2_codes.code"),
F("iso3", "iso_3166_3_codes.code"),
F("sortname", "aliases.sort_name"),
F("ref_count", ["labels.id", "places.id"] , transformfunc=len, trigger=False),
F("ref_count", ["place_count", "label_count", "artist_count"], transformfunc=tfs.integer_sum, trigger=False),
F("tag", "tags.tag.name"),
F("type", "type.name")
],
Expand Down
9 changes: 5 additions & 4 deletions sir/schema/modelext.py
Expand Up @@ -8,8 +8,8 @@
Instrument, Label, LinkAttribute, LinkAttributeType,
MediumCDTOC, Place, Recording, Release, ReleaseGroup,
ReleaseRaw, ReleaseTag, Series, Work, URL)
from sqlalchemy import exc as sa_exc
from sqlalchemy.orm import relationship
from sqlalchemy import exc as sa_exc, func, select
from sqlalchemy.orm import relationship, column_property
from warnings import simplefilter

# Ignore SQLAlchemy's warnings that we're overriding some attributes
Expand All @@ -35,8 +35,9 @@ class CustomArea(Area):
area_links = relationship("LinkAreaArea",
primaryjoin="Area.id == LinkAreaArea.entity1_id")
tags = relationship("AreaTag")
places = relationship("Place")
labels = relationship("Label")
place_count = column_property(select([func.count(Place.id)]).where(Place.area_id == Area.id))
label_count = column_property(select([func.count(Label.id)]).where(Label.area_id == Area.id))
artist_count = column_property(select([func.count(Artist.id)]).where(Artist.area_id == Area.id))

class CustomArtist(Artist):
area = relationship('CustomArea', foreign_keys=[Artist.area_id])
Expand Down
4 changes: 4 additions & 0 deletions sir/schema/transformfuncs.py
Expand Up @@ -33,6 +33,10 @@
}


def integer_sum(values):
return int(sum(values))


def ended_to_string(ended):
"""
:param ended:
Expand Down