官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 韶关市| 旬阳县| 汽车| 北碚区| 延长县| 台东市| 金平| 通辽市| 沁水县| 嘉善县| 连城县| 桦川县| 台前县| 濉溪县| 上杭县| 台东市| 天门市| 宜丰县| 弥渡县| 佛山市| 伊宁市| 荆州市| 西藏| 高要市| 泽库县| 南阳市| 南川市| 连山| 伊川县| 和政县| 湖口县| 垦利县| 桓台县| 怀仁县| 安新县| 宜都市| 延津县| 洞口县| 金堂县| 迁西县| 奈曼旗|