How Django customizes the order in which model creates database indexes

  • 2021-06-28 13:35:31
  • OfStack

First of all, this is a question of chicken or egg first. Most of the time, the data comes from excel. When more and more data are available and more points need to be analyzed, it is obviously a little hard to manage through excel.

excel needs to be imported into the database at this time, but the index created by model is not the order in which we wrote it. Especially when models.ForeignKey exists, we have to create class for ForeignKey before we create the total table.

To ensure that the database index is consistent with excel's index 1, we need to do a series of processing on the index field in model (which may be a wild road).

First of all, of course, create ForeignKey. This part of class should be written before, otherwise, class of the total table will prompt that the referenced ForeignKey is undefined (this part is not exemplified).

Next, create a summary table. Don't rush to fill in the index fields in the summary table, just create the header of the summary table, as shown below:


@python_2_unicode_compatible
class ZZ_Demo(models.Model): # Here's the ZZ The name at the beginning is to make this class Create at last 
###### Don't write anything in the middle ######
 class Meta:
 verbose_name = ' This is a Demo'
 verbose_name_plural = ' This is a Demo' # These are Django admin Shown in   Ignorable 

 def __str__(self):
 return self.name

Then execute at the terminal:


python manage.py makemigrations
python manage.py migrate

This process will see the system create the data table.

After creating the index field in the general table, you can be sure that model created the index in alphabetical order. If there is no other models.ForeignKey, you can control the order of index creation directly in alphabetical order, as shown in the following figure:


@python_2_unicode_compatible
class ZZ_Demo(models.Model):
 ab_productline = models.ForeignKey(ProductLine, verbose_name=u' product line ',default=1,null=True)
 ac_name = models.CharField(max_length=50, verbose_name = u' Failure Name ',null=True)
 ......
 bn_casetags = models.CharField(max_length=50, verbose_name = u' Key word   Label   Remarks ',default=None,blank=True,null=True)

 class Meta:
 verbose_name = ' This is a Demo'
 verbose_name_plural = ' This is a Demo'

 def __str__(self):
 return self.be_casenumber

This creates an alphabetical index that your little friends can try.

Setting Joint Constraints and Indexes in Django model

Several fields of a table are jointly constrained and indexed in Django model, for example, in a shopping cart table, the logged-in user and commodity fields together represent only one record.

Lift a chestnut:

Shopping Cart Table in Django model


class Cart(models.Model):
 user = models.ForeignKey(
 MyUser,
 verbose_name=" user "
 )
 goods = models.ForeignKey(
 Goods,
 verbose_name=" commodity "
 )
 num = models.IntegerField(
 verbose_name=" Quantity of Goods "
 )
 is_select = models.BooleanField(
 default=True,
 verbose_name=" Selected state "
 )
 
 class Meta:
 #  Joint Constraint   among goods and user Cannot Repeat 
 unique_together = ["goods", "user"]
 #  Joint Index 
 index_together = ["user", "goods"]

unique_together = ['goods','user'] denotes a joint constraint, where'goods'and'user' denote a non-repeatable, non-identical condition.

index_together = ['user','goods'] represents a federated index where'goods'and'user' jointly synchronize queries to improve efficiency.

Advantages of Joint Indexing

Example SQL: select * from person where a=100 and b=100 and c=1000;

&8203;Suppose your data has 10 million filters per condition that save 10% of the data

&8203;1 If three single indexes take a's index first to find the remaining 1 million data, then b's condition to find the remaining 100,000 then c's condition to find the last 10,000 data

&8203;2 If it's a joint index, he gets 10 million data * 10% * 10% * 10% directly

&8203;Joint indexes are also indexed for their combinations


Related articles: