Add many indexes on FK and very used properties

This commit is contained in:
Xavier Bustamante Talavera 2018-11-04 23:00:51 +01:00
parent 5bc72fbe8b
commit 55a210bced
6 changed files with 12 additions and 9 deletions

View file

@ -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.

View file

@ -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,

View file

@ -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,

View file

@ -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)

View file

@ -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.

View file

@ -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.