20211230(1億件を考慮)

7810 ワード

1.学習時間全体


午後04:00から午前1:00まで

2.学習目標


長期的な発展.

3.学習方法


正式な文書を破る.

4.次の学習目標


5.学習内容の詳細


索引の作成


取引明細はデータ削除を変更できません.
変更が少なく、クエリーが多い場合に有効なインデックスを利用できます.
B-Treeなどのアルゴリズムが使用されているため、一意の値を持つトランザクションID、すなわち高速なプロジェクトに適しています.
  • サーバで実行されるクエリ
  • SELECT "transaction_index_history"."id", "transaction_index_history"."account_id", 
    "transaction_index_history"."balance", "transaction_index_history"."amount", 
    "transaction_index_history"."t_type", 
    "transaction_index_history"."description", 
    "transaction_index_history"."created_at" FROM "transaction_index_history" 
    WHERE ("transaction_index_history"."account_id" = 1
    AND "transaction_index_history"."created_at" BETWEEN 2021-12-29 00:00:00
    AND 2021-12-30 00:00:00)
  • ,500万データ時の速度(日付別フィルタリング)
  • デフォルト検索
    1070ms
  • 検索日は

  • 1965ms
  • 入金区分検索
    1070ms

  • 表の変更
    from django.db import models
    from account.models import Account
    
    # Create your models here.
    class Transaction(models.Model):
        account = models.ForeignKey(Account, on_delete=models.CASCADE)
        balance = models.PositiveBigIntegerField()
        amount = models.PositiveBigIntegerField()
        t_type = models.CharField(max_length=50)
        description = models.CharField(max_length=50)
        created_at = models.DateTimeField(auto_now_add=True)
    
    ## 추가
        class Meta:
            db_table = 'transaction_index_history'
            indexes = [
                models.Index(fields=['account_id', 't_type']),
                models.Index(fields=['account_id', 'created_at']),
            ]
  • python manage.py makemigrations
    python manage.py migrate           
    

  • 生成されたファイル


  • データベースの変更
    インデックスが作成されました


  • インデックス後の500万個のデータ

  • デフォルトの検索
    212ms


  • 日付による検索
    715ms


  • 入金区分の取得
    203ms