user5042031
user5042031

Reputation:

Clustered index in django

I have two tables in my models:

1)Owner:

OwnerName

2)Car

OwnerKey

CarName

CarPrice

Here while creating a row in Owner table, I also add the Cars for that owner Car table. So all the cars for a particular owner are stored sequentially in the Car table.Now if I want to ask whether should I use cluster indexing or not? Once the cars for a particular owner are saved, no cars are then added for that owner neither any car is deleted, just the price is changed. What should I do for a faster access? And how to implement cluster index via django?

Upvotes: 7

Views: 1742

Answers (1)

Rick James
Rick James

Reputation: 142208

You question is asking for information that requires studying the SQL, not just the Django code.

If you have only a thousand owners and cars, things will probably be fast enough. If you have a million owners or cars, you need indexes, not necessarily "clustered".

A "clustered" key is implemented in MySQL as a PRIMARY KEY. You can have only one per table, and its values must be unique.

I Django, do something like this to get a PK:

column1 = models.IntegerField(primary_key = True)

Please provide the table schema you already have. That is, go beyond Django and get SHOW CREATE TABLE. (What you have provided is too vague; hence my answer is too vague.)

References:

There are no "clustered" indexes other than the PRIMARY KEY. However, a secondary key can have similar performance if it is a "covering index". That term refers to an index that contains all the columns that are found in the SELECT. Also, the columns are ordered correctly.

Let's see your SELECT statements in order to better judge what is needed.

Upvotes: 1

Related Questions