- Django Design Patterns and Best Practices
- Arun Ravindran
- 292字
- 2021-06-25 21:32:06
Performance and denormalization
Normalization can adversely affect performance. As the number of models increase, the number of joins needed to answer a query also increase. For instance, to find the number of superheroes with the Freeze capability in the USA, you will need to join four tables. Prior to normalization, any information can be found by querying a single table.
You should design your models to keep the data normalized. This will maintain data integrity. However, if your site faces scalability issues, then you can selectively derive data from those models to create denormalized data.
Best Practice:
Normalize while designing, but denormalize while optimizing.
For instance, if counting the sightings in a certain country is very common, then add it as an additional field to the Location model. Now, you can include the other queries using Django object-relational mapping (ORM), unlike a cached value.
However, you need to update this count each time you add or remove a sighting. You need to add this computation to the save method of Sighting, add a signal handler, or even compute using an asynchronous job.
If you have a complex query spanning several tables, such as a count of superpowers by country, then creating a separate denormalized table might improve performance. Typically, this table will be in a faster in-memory database or a cache. As before, we need to update this denormalized table every time the data in your normalized models changes (or you will have the infamous cache-invalidation problem).
Denormalization is surprisingly common in large websites because it is a tradeoff between speed and space. Today, space is cheap, but speed is crucial to user experience. So, if your queries are taking too long to respond, then you might want to consider it.
- Java異步編程實戰
- ASP.NET MVC4框架揭秘
- Hands-On Image Processing with Python
- 基于Java技術的Web應用開發
- 編寫高質量代碼:改善C程序代碼的125個建議
- Serverless computing in Azure with .NET
- 鴻蒙OS應用編程實戰
- Scala編程實戰
- Getting Started with React VR
- Arduino機器人系統設計及開發
- 深入大型數據集:并行與分布化Python代碼
- RESTful Web API Design with Node.js
- R語言:邁向大數據之路
- Puppet Cookbook(Third Edition)
- Neo4j權威指南 (圖數據庫技術叢書)