From 55a210bced1a54733cf71f59951d95bec584bdff Mon Sep 17 00:00:00 2001 From: Xavier Bustamante Talavera Date: Sun, 4 Nov 2018 23:00:51 +0100 Subject: [PATCH] Add many indexes on FK and very used properties --- ereuse_devicehub/resources/agent/models.py | 2 +- ereuse_devicehub/resources/device/models.py | 4 ++-- ereuse_devicehub/resources/event/models.py | 6 +++--- ereuse_devicehub/resources/lot/models.py | 2 +- ereuse_devicehub/resources/models.py | 2 ++ ereuse_devicehub/resources/tag/model.py | 5 +++-- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/resources/agent/models.py b/ereuse_devicehub/resources/agent/models.py index 6b647dc2..02082843 100644 --- a/ereuse_devicehub/resources/agent/models.py +++ b/ereuse_devicehub/resources/agent/models.py @@ -27,7 +27,7 @@ class JoinedTableMixin: class Agent(Thing): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) - type = Column(Unicode, nullable=False) + type = Column(Unicode, nullable=False, index=True) name = Column(CIText()) name.comment = """ The name of the organization or person. diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 5f72f86a..031a2a53 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -36,7 +36,7 @@ class Device(Thing): id.comment = """ The identifier of the device for this database. """ - type = Column(Unicode(STR_SM_SIZE), nullable=False) + type = Column(Unicode(STR_SM_SIZE), nullable=False, index=True) hid = Column(Unicode(), check_lower('hid'), unique=True) hid.comment = """ The Hardware ID (HID) is the unique ID traceability systems @@ -375,7 +375,7 @@ class Cellphone(Mobile): class Component(Device): id = Column(BigInteger, ForeignKey(Device.id), primary_key=True) - parent_id = Column(BigInteger, ForeignKey(Computer.id)) + parent_id = Column(BigInteger, ForeignKey(Computer.id), index=True) parent = relationship(Computer, backref=backref('components', lazy=True, diff --git a/ereuse_devicehub/resources/event/models.py b/ereuse_devicehub/resources/event/models.py index 656cf76a..f509fdd2 100644 --- a/ereuse_devicehub/resources/event/models.py +++ b/ereuse_devicehub/resources/event/models.py @@ -43,7 +43,7 @@ class JoinedTableMixin: class Event(Thing): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) - type = Column(Unicode, nullable=False) + type = Column(Unicode, nullable=False, index=True) name = Column(CIText(), default='', nullable=False) name.comment = """ A name or title for the event. Used when searching for events. @@ -148,7 +148,7 @@ class Event(Thing): For Add and Remove though, this has another meaning: the components that are added or removed. """ - parent_id = Column(BigInteger, ForeignKey(Computer.id)) + parent_id = Column(BigInteger, ForeignKey(Computer.id), index=True) parent = relationship(Computer, backref=backref('events_parent', lazy=True, @@ -222,7 +222,7 @@ class JoinedWithOneDeviceMixin: class EventWithOneDevice(JoinedTableMixin, Event): - device_id = Column(BigInteger, ForeignKey(Device.id), nullable=False) + device_id = Column(BigInteger, ForeignKey(Device.id), nullable=False, index=True) device = relationship(Device, backref=backref('events_one', lazy=True, diff --git a/ereuse_devicehub/resources/lot/models.py b/ereuse_devicehub/resources/lot/models.py index 38e11df1..16220026 100644 --- a/ereuse_devicehub/resources/lot/models.py +++ b/ereuse_devicehub/resources/lot/models.py @@ -131,7 +131,7 @@ class Path(db.Model): id = db.Column(db.UUID(as_uuid=True), primary_key=True, server_default=db.text('gen_random_uuid()')) - lot_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(Lot.id), nullable=False) + lot_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(Lot.id), nullable=False, index=True) lot = db.relationship(Lot, backref=db.backref('paths', lazy=True, collection_class=set), primaryjoin=Lot.id == lot_id) diff --git a/ereuse_devicehub/resources/models.py b/ereuse_devicehub/resources/models.py index c8e9b7ec..d0dbd2a1 100644 --- a/ereuse_devicehub/resources/models.py +++ b/ereuse_devicehub/resources/models.py @@ -13,12 +13,14 @@ class Thing(db.Model): # todo make updated to auto-update updated = db.Column(db.TIMESTAMP(timezone=True), nullable=False, + index=True, server_default=db.text('CURRENT_TIMESTAMP')) updated.comment = """ When this was last changed. """ created = db.Column(db.TIMESTAMP(timezone=True), nullable=False, + index=True, server_default=db.text('CURRENT_TIMESTAMP')) created.comment = """ When Devicehub created this. diff --git a/ereuse_devicehub/resources/tag/model.py b/ereuse_devicehub/resources/tag/model.py index 1ef2a530..1c6fc0f0 100644 --- a/ereuse_devicehub/resources/tag/model.py +++ b/ereuse_devicehub/resources/tag/model.py @@ -32,12 +32,13 @@ class Tag(Thing): """ device_id = Column(BigInteger, # We don't want to delete the tag on device deletion, only set to null - ForeignKey(Device.id, ondelete=DB_CASCADE_SET_NULL)) + ForeignKey(Device.id, ondelete=DB_CASCADE_SET_NULL), + index=True) device = relationship(Device, backref=backref('tags', lazy=True, collection_class=set), primaryjoin=Device.id == device_id) """The device linked to this tag.""" - secondary = Column(Unicode(), check_lower('secondary')) + secondary = Column(Unicode(), check_lower('secondary'), index=True) secondary.comment = """ A secondary identifier for this tag. It has the same constraints as the main one. Only needed in special cases.