I'm trying to optimize the MySQL DB on a Django app that provides search results for items for users to buy. It has been recommended to me that one option was to vertically split my Items table into multiple tables. After some thought, I've realized I'm using the table for three separate purposes:
- Query rows for results relevant to some search term.
- Display paginated results that are relevant to the search term.
- Direct a user to any external pages they click.
So as far as I can tell, my best option is to split the table according to these needs (Is this a correct assumption?).
At the moment my model looks like this:
class Items(models.Model):
categories = models.CharField(max_length=64)
title = models.CharField(max_length=128)
description = models.CharField(max_length=1024)
thumb = models.CharField(max_length=255, unique=True)
vendor = models.CharField(max_length=16)
url = models.CharField(max_length=255, unique=True)
After a horizontal split, the tables would look something like this:
# Query all fields in this table for the search term
class ItemSearch(models.Model):
categories = models.CharField(max_length=64)
title = models.CharField(max_length=128)
description = models.CharField(max_length=1024)
# Once a set of relevant results has been compiled, query this table to get all information needed to display it on the page.
class ItemDisplay(models.Model):
title = models.CharField(max_length=128)
thumb = models.CharField(max_length=255, unique=True)
vendor = models.CharField(max_length=16)
# foreign_key referencing ItemSearch.id?
# Once a user clicks on an item they want, send them to a RedirectView associated with the products ItemDisplay.id: r'^item/(?P<item_id>[0-9]+)$'
class ItemOut(models.Model):
url = models.CharField(max_length=255, unique=True)
# foreign_key referencing ItemDisplay.id?
Obviously these tables are not currently linked, so once I query ItemSearch, I have no way of finding the associated rows in ItemDisplay, and subsequently doing the same thing for ItemOut.
How can I associate these tables with each other?