简体   繁体   English

创建左联接还是向某些表添加一些额外的列?

[英]Create a left join or add a few extra columns to a certain table?

I'm wondering what would be the best solution (from a performance perspective). 我想知道什么是最好的解决方案(从性能的角度来看)。 I have an Orders table which can have a specific OrderShipment relation. 我有一个Orders表,它可以具有特定的OrderShipment关系。 The OrderShipment can eighter be a normal shipping or a delivery at a servicepoint (in the Netherlands, just a drop-off point when you're nor at home like a supermarket, videostore etc). OrderShipment可以是正常的运输,也可以是在服务点(在荷兰,这是您不在家或不在家时(例如,超市,音像店等)的下车点)进行的送货。

Currently I'm using a HasOrderShipment field in the Order-table to determine whether or not a second query must be executed to get the Shipment. 目前,我正在使用Order-table中的HasOrderShipment字段来确定是否必须执行第二个查询才能获得Shipment。

When a customer chooses delivery at a servicepoint I need to store the addressdetails of this servicepoint. 当客户选择在服务点交货时,我需要存储该服务点的地址详细信息。 Now my question is, from a performance perspective, should I extend the OrderShipment table with adressfields (leaving those fields NULL in all other cases except for servicepoint delivery) or create a HasOrderShipmentServicePointAddress table where the addressdetails are stored. 现在,我的问题是,从性能的角度来看,我应该使用adressfields扩展OrderShipment表(在除服务点交付以外的所有其他情况下,将这些字段保留为NULL)还是创建一个其中存储了地址细节的HasOrderShipmentServicePointAddress表。

The ordertable is about 5.2m rows and grows with about 21.500 orders per day (avg from 1-jan-11 / 23-jan-11). 订单表约有520万行,并且每天以约21.500笔订单增长(11年1月1日至11月23日平均)。

Well, with tables that small, I wouldn't personally worry too much about it (And yes, 5 million rows is pretty small in database terms). 好吧,对于这么小的表,我个人不会对此太担心(是的,从数据库角度来说,500万行是非常小的)。 Is your query too slow now? 您的查询现在太慢了吗? If so, optimize it. 如果是这样,请对其进行优化。 If not, don't worry about it. 如果没有,请不要担心。

Now, as far as your specific question, if you have enough data, you might get away with building a summary table. 现在,就您的特定问题而言,如果您有足够的数据,则可能无需构建汇总表。 Since MySQL doesn't support materialized views (views that are indexed and stored on the disk), adding a view won't really help much in the performance terms (but it will help readability). 由于MySQL不支持实体化视图(被索引并存储在磁盘上的视图),因此在性能方面添加视图实际上并没有多大帮助(但它将有助于提高可读性)。 Instead, you could add a summary table. 相反,您可以添加一个摘要表。 The summary table would be the result of a specific query. 摘要表将是特定查询的结果。 So instead of adding the extra columns to the normalized table, you'd copy the table and add the columns to the new one, and then use triggers to keep it updated. 因此,您无需复制多余的列到规范化表中,而是将表复制并将列添加到新表中,然后使用触发器保持其更新。 The reason I do this in a separate table is that you still have the normalized tables to enforce referential integrity and for the queries that are more efficient with normalized data. 我在单独的表中执行此操作的原因是,您仍然具有规范化的表来强制执行参照完整性,并且对于使用规范化的数据更有效的查询也是如此。 But you can use the summary table for queries where you need to do complex joins (you're basically pre-computing the join). 但是您可以将汇总表用于需要进行复杂联接的查询(基本上是在预先计算联接)。

But again, I stress, don't even think of doing something like this unless you have to. 但是我要强调,除非您必须这样做,否则不要考虑做这样的事情。 Try the normal routes for optimization (indexes, EXPLAIN , rewriting queries, more ram, etc) first. 首先尝试正常路线进行优化(索引, EXPLAIN ,重写查询,更多内存等)。 Use a summary table as a last resort... 将汇总表用作最后的选择...

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

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