简体   繁体   English

我应该对表格进行非规范化,还是应该对FK列进行非规范化

[英]Should I denormalize my table, should I denormalize a FK column

I have a table with a column created_time (DateTime) and a foreign_key i need to retrieve all the rows with x foreign_key that the last row created with x foreign_key was lets say over 2 minutes (and retrieve all the rows with that date) 我有一个带有一列created_time (DateTime)和一个foreign_key的表,我需要检索所有带有x foreign_key的行,而用x foreign_key创建的最后一行要说2分钟以上(并检索具有该日期的所有行)

My question: what is better for performance, to add a column that stores the last row created date every time i create a row, or to use a more complex query to retrieve those rows with the last row joined? 我的问题:在每次创建行时添加存储最后创建日期的行的列,或者使用更复杂的查询来检索最后一行连接的行,对性能有何好处?

Essentially the question is "Should I denormalize the created_time?" 本质上,问题是“我应该对created_time进行非规范化吗?” The answer here is that from performance point of view the difference with correct indexes set is negligible and if denormalized this will lead to data duplication across your schema which may trigger one day other problems. 从性能的角度来看,答案是可以忽略的 ,正确设置索引的差异可以忽略不计,并且如果将其规范化,则将导致整个模式中的数据重复,这可能会引发一天的其他问题。 From this point of view my recommendation is to not do it and use the expressive powers of SQL to retrieve what you need. 从这个角度来看,我的建议是不要这样做,而要使用SQL的表达能力来检索您需要的内容。

By denormalizing the created_time you are heading towards flat tables design and will end up creating a NoSQL DB , which I believe is not an approach you would like to follow .Since you are using SQL , you should use the SQL Query power with EXPLAIN keyword and notice the time it takes to run your query . 通过对created_time进行非规范化,您将朝着平面表设计的方向发展,最终将创建NoSQL DB,我认为这不是您要遵循的方法。由于您使用的是SQL,因此应使用带有EXPLAIN关键字的SQL Query power和注意运行查询所花费的时间。 I believe it won't take much time to process all your rows. 我相信将不会花费很多时间来处理所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM