如何优化节点sequalize复杂连接选择并插入

时间:2015-12-12 19:57:07

标签: mysql node.js query-optimization sequelize.js database-performance

我有一些非常复杂的查询需要很长时间,我怀疑是因为它们的复杂连接。我在使用Sequalize ORM的Ubuntu上使用node。

选择展示位置:

SELECT 
`Placement`.`id`, 
`Placement`.`agencyPlacementName`, 
`Placement`.`agencyPlacementId`, 
`Placement`.`start`, 
`Placement`.`end`, 
`Placement`.`comments`, 
`Placement`.`billingGroup`, 
`Placement`.`mediaRate`, 
`Placement`.`contractedQuantity`, 
`Placement`.`excludeGallery`, 
`Placement`.`pubSelect`, 
`Placement`.`IoId`, 
`Placement`.`IoId`, 
`Placement`.`StrategyId`, 
`Placement`.`SalesProductId`, 
`Placement`.`MediaRateTypeId`, 
`Placement`.`FrequencyCapId`, 
`Placement`.`dfpLineItemId`, 
`Placement`.`dfpLastModified`, 
`Placement`.`dfpStatus`, 
`Placement`.`isCarGurus`, 
`Strategy`.`id` AS `Strategy.id`, 
`Strategy`.`name` AS `Strategy.name`, 
`Strategy`.`RequiredDetailTypeId` AS `Strategy.RequiredDetailTypeId`, 
`SalesProduct`.`id` AS `SalesProduct.id`, 
`SalesProduct`.`name` AS `SalesProduct.name`, 
`SalesProduct`.`code` AS `SalesProduct.code`, `SalesProduct`.`canExcludeGallery` AS `SalesProduct.canExcludeGallery`, 
`SalesProduct`.`canPubSelect` AS `SalesProduct.canPubSelect`, 
`CreativeProducts`.`id` AS `CreativeProducts.id`, 
`CreativeProducts`.`name` AS `CreativeProducts.name`, 
`CreativeProducts.PlacementCreativeProducts`.`createdAt` AS `CreativeProducts.PlacementCreativeProducts.createdAt`, 
`CreativeProducts.PlacementCreativeProducts`.`updatedAt` AS `CreativeProducts.PlacementCreativeProducts.updatedAt`, 
`CreativeProducts.PlacementCreativeProducts`.`PlacementId` AS `CreativeProducts.PlacementCreativeProducts.PlacementId`, 
`CreativeProducts.PlacementCreativeProducts`.`CreativeProductId` AS `CreativeProducts.PlacementCreativeProducts.CreativeProductId`, 
`CustomCriteriaPresets`.`id` AS `CustomCriteriaPresets.id`, 
`CustomCriteriaPresets`.`name` AS `CustomCriteriaPresets.name`, 
`CustomCriteriaPresets.PlacementCustomTargeting`.`createdAt` AS `CustomCriteriaPresets.PlacementCustomTargeting.createdAt`, 
`CustomCriteriaPresets.PlacementCustomTargeting`.`updatedAt` AS `CustomCriteriaPresets.PlacementCustomTargeting.updatedAt`, 
`CustomCriteriaPresets.PlacementCustomTargeting`.`PlacementId` AS `CustomCriteriaPresets.PlacementCustomTargeting.PlacementId`, 
`CustomCriteriaPresets.PlacementCustomTargeting`.`CustomCriteriaPresetId` AS `CustomCriteriaPresets.PlacementCustomTargeting.CustomCriteriaPresetId`, 
`DataFees`.`id` AS `DataFees.id`, 
`DataFees`.`amount` AS `DataFees.amount`, 
`DataFees.DataFeeType`.`id` AS `DataFees.DataFeeType.id`, 
`DataFees.DataFeeType`.`name` AS `DataFees.DataFeeType.name`, 
`DataFees.DataFeeType`.`createdAt` AS `DataFees.DataFeeType.createdAt`, 
`DataFees.DataFeeType`.`updatedAt` AS `DataFees.DataFeeType.updatedAt`, 
`DataFees.DataFeeRateType`.`id` AS `DataFees.DataFeeRateType.id`, 
`DataFees.DataFeeRateType`.`name` AS `DataFees.DataFeeRateType.name`, 
`DataFees.DataFeeRateType`.`createdAt` AS `DataFees.DataFeeRateType.createdAt`, 
`DataFees.DataFeeRateType`.`updatedAt` AS `DataFees.DataFeeRateType.updatedAt`, 
`Geotargets`.`id` AS `Geotargets.id`, 
`Geotargets`.`name` AS `Geotargets.name`, 
`Geotargets`.`type` AS `Geotargets.type`, 
`Geotargets`.`parentIds` AS `Geotargets.parentIds`, 
`Geotargets.PlacementGeotargets`.`createdAt` AS `Geotargets.PlacementGeotargets.createdAt`, 
`Geotargets.PlacementGeotargets`.`updatedAt` AS `Geotargets.PlacementGeotargets.updatedAt`, 
`Geotargets.PlacementGeotargets`.`PlacementId` AS `Geotargets.PlacementGeotargets.PlacementId`, 
`Geotargets.PlacementGeotargets`.`GeotargetId` AS `Geotargets.PlacementGeotargets.GeotargetId`, `Publishers`.`id` AS 
`Publishers.id`, `Publishers`.`name` AS `Publishers.name`, 
`Publishers.PlacementPublishers`.`createdAt` AS `Publishers.PlacementPublishers.createdAt`, 
`Publishers.PlacementPublishers`.`updatedAt` AS `Publishers.PlacementPublishers.updatedAt`, 
`Publishers.PlacementPublishers`.`PlacementId` AS `Publishers.PlacementPublishers.PlacementId`, 
`Publishers.PlacementPublishers`.`PublisherId` AS `Publishers.PlacementPublishers.PublisherId`, 
`DeviceTypes`.`id` AS `DeviceTypes.id`, 
`DeviceTypes`.`name` AS `DeviceTypes.name`, 
`DeviceTypes`.`code` AS `DeviceTypes.code`, 
`DeviceTypes.PlacementDeviceTypes`.`createdAt` AS `DeviceTypes.PlacementDeviceTypes.createdAt`, 
`DeviceTypes.PlacementDeviceTypes`.`updatedAt` AS `DeviceTypes.PlacementDeviceTypes.updatedAt`, 
`DeviceTypes.PlacementDeviceTypes`.`PlacementId` AS `DeviceTypes.PlacementDeviceTypes.PlacementId`, 
`DeviceTypes.PlacementDeviceTypes`.`DeviceTypeId` AS `DeviceTypes.PlacementDeviceTypes.DeviceTypeId`, 
`DeviceTypes.CreativeProducts`.`id` AS `DeviceTypes.CreativeProducts.id`, 
`DeviceTypes.CreativeProducts`.`name` AS `DeviceTypes.CreativeProducts.name`, 
`DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts`.`createdAt` AS `DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts.createdAt`, 
`DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts`.`updatedAt` AS `DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts.updatedAt`, 
`DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts`.`DeviceTypeId` AS `DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts.DeviceTypeId`, 
`DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts`.`CreativeProductId` AS `DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts.CreativeProductId`, 
`FrequencyCap`.`numImps` AS `FrequencyCap.numImps`, 
`FrequencyCap`.`numTimeUnits` AS `FrequencyCap.numTimeUnits`, 
`FrequencyCap`.`id` AS `FrequencyCap.id`, 
`FrequencyCap.FrequencyCapTimeUnit`.`id` AS `FrequencyCap.FrequencyCapTimeUnit.id`, 
`FrequencyCap.FrequencyCapTimeUnit`.`name` AS `FrequencyCap.FrequencyCapTimeUnit.name`, 
`FrequencyCap.FrequencyCapTimeUnit`.`createdAt` AS `FrequencyCap.FrequencyCapTimeUnit.createdAt`, 
`FrequencyCap.FrequencyCapTimeUnit`.`updatedAt` AS `FrequencyCap.FrequencyCapTimeUnit.updatedAt`, 
`MediaRateType`.`id` AS `MediaRateType.id`, 
`MediaRateType`.`name` AS `MediaRateType.name` 
FROM `Placements` AS `Placement` 
LEFT OUTER JOIN `Strategies` AS `Strategy` 
ON `Placement`.`StrategyId` = `Strategy`.`id` 
LEFT OUTER JOIN `SalesProducts` AS `SalesProduct` 
ON `Placement`.`SalesProductId` = `SalesProduct`.`id` 
LEFT OUTER JOIN (
    `PlacementCreativeProducts` AS `CreativeProducts.PlacementCreativeProducts` 
    INNER JOIN `CreativeProducts` AS `CreativeProducts` 
    ON `CreativeProducts`.`id` = `CreativeProducts.PlacementCreativeProducts`.`CreativeProductId`
) ON `Placement`.`id` = `CreativeProducts.PlacementCreativeProducts`.`PlacementId` 
LEFT OUTER JOIN (
    `PlacementCustomTargeting` AS `CustomCriteriaPresets.PlacementCustomTargeting` 
    INNER JOIN `CustomCriteriaPresets` AS `CustomCriteriaPresets` 
    ON `CustomCriteriaPresets`.`id` = `CustomCriteriaPresets.PlacementCustomTargeting`.`CustomCriteriaPresetId`
) ON `Placement`.`id` = `CustomCriteriaPresets.PlacementCustomTargeting`.`PlacementId` 
LEFT OUTER JOIN `DataFees` AS `DataFees` 
ON `Placement`.`id` = `DataFees`.`PlacementId` 
LEFT OUTER JOIN `DataFeeTypes` AS `DataFees.DataFeeType` 
ON `DataFees`.`DataFeeTypeId` = `DataFees.DataFeeType`.`id` 
LEFT OUTER JOIN `DataFeeRateTypes` AS `DataFees.DataFeeRateType` 
ON `DataFees`.`DataFeeRateTypeId` = `DataFees.DataFeeRateType`.`id` 
LEFT OUTER JOIN (
    `PlacementGeotargets` AS `Geotargets.PlacementGeotargets` 
    INNER JOIN `Geotargets` AS `Geotargets` 
    ON `Geotargets`.`id` = `Geotargets.PlacementGeotargets`.`GeotargetId`
) ON `Placement`.`id` = `Geotargets.PlacementGeotargets`.`PlacementId` 
LEFT OUTER JOIN (
    `PlacementPublishers` AS `Publishers.PlacementPublishers` 
    INNER JOIN `Publishers` AS `Publishers` 
    ON `Publishers`.`id` = `Publishers.PlacementPublishers`.`PublisherId`
) ON `Placement`.`id` = `Publishers.PlacementPublishers`.`PlacementId` 
LEFT OUTER JOIN (
    `PlacementDeviceTypes` AS `DeviceTypes.PlacementDeviceTypes` 
    INNER JOIN `DeviceTypes` AS `DeviceTypes` 
    ON `DeviceTypes`.`id` = `DeviceTypes.PlacementDeviceTypes`.`DeviceTypeId`
) ON `Placement`.`id` = `DeviceTypes.PlacementDeviceTypes`.`PlacementId` 
LEFT OUTER JOIN (
    `DeviceTypeCreativeProducts` AS `DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts` 
    INNER JOIN `CreativeProducts` AS `DeviceTypes.CreativeProducts` 
    ON `DeviceTypes.CreativeProducts`.`id` = `DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts`.`CreativeProductId`
) ON `DeviceTypes`.`id` = `DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts`.`DeviceTypeId` 
LEFT OUTER JOIN `FrequencyCaps` AS `FrequencyCap` 
ON `Placement`.`FrequencyCapId` = `FrequencyCap`.`id` 
LEFT OUTER JOIN `FrequencyCapTimeUnits` AS `FrequencyCap.FrequencyCapTimeUnit` 
ON `FrequencyCap`.`FrequencyCapTimeUnitId` = `FrequencyCap.FrequencyCapTimeUnit`.`id` 
LEFT OUTER JOIN `MediaRateTypes` AS `MediaRateType` 
ON `Placement`.`MediaRateTypeId` = `MediaRateType`.`id` 
WHERE `Placement`.`id` = 6502;

我已经完成了他们的解释,解释看起来很安静 全部是针对空的表,所以我不确定如何优化它们? 这是一个问题,它只是拿起PRIMARY键而不是另一个Key吗? 我应该创建复杂的索引吗?我该怎么做才能更快地完成这项工作?

展示位置选择解释

+----+-------------+---------------------------------------------------------+--------+--------------------------------+-------------+---------+-----------------------------------------------------------------------------------+------+-------+
| id | select_type | table                                                   | type   | possible_keys                  | key         | key_len | ref                                                                               | rows | Extra |
+----+-------------+---------------------------------------------------------+--------+--------------------------------+-------------+---------+-----------------------------------------------------------------------------------+------+-------+
|  1 | SIMPLE      | Placement                                               | const  | PRIMARY                        | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | Strategy                                                | const  | PRIMARY                        | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | SalesProduct                                            | const  | PRIMARY                        | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | FrequencyCap                                            | const  | PRIMARY                        | NULL        | NULL    | NULL                                                                              |    1 | NULL  |
|  1 | SIMPLE      | FrequencyCap.FrequencyCapTimeUnit                       | const  | PRIMARY                        | NULL        | NULL    | NULL                                                                              |    1 | NULL  |
|  1 | SIMPLE      | CreativeProducts.PlacementCreativeProducts              | ref    | PRIMARY,CreativeProductId      | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | CreativeProducts                                        | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.CreativeProducts.PlacementCreativeProducts.CreativeProductId              |    1 | NULL  |
|  1 | SIMPLE      | CustomCriteriaPresets.PlacementCustomTargeting          | ref    | PRIMARY,CustomCriteriaPresetId | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | CustomCriteriaPresets                                   | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.CustomCriteriaPresets.PlacementCustomTargeting.CustomCriteriaPresetId     |    1 | NULL  |
|  1 | SIMPLE      | DataFees                                                | ref    | PlacementId                    | PlacementId | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | DataFees.DataFeeType                                    | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.DataFees.DataFeeTypeId                                                    |    1 | NULL  |
|  1 | SIMPLE      | DataFees.DataFeeRateType                                | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.DataFees.DataFeeRateTypeId                                                |    1 | NULL  |
|  1 | SIMPLE      | Geotargets.PlacementGeotargets                          | ref    | PRIMARY,GeotargetId            | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | Geotargets                                              | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.Geotargets.PlacementGeotargets.GeotargetId                                |    1 | NULL  |
|  1 | SIMPLE      | Publishers.PlacementPublishers                          | ref    | PRIMARY,PublisherId            | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | Publishers                                              | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.Publishers.PlacementPublishers.PublisherId                                |    1 | NULL  |
|  1 | SIMPLE      | DeviceTypes.PlacementDeviceTypes                        | ref    | PRIMARY,DeviceTypeId           | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
|  1 | SIMPLE      | DeviceTypes                                             | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.DeviceTypes.PlacementDeviceTypes.DeviceTypeId                             |    1 | NULL  |
|  1 | SIMPLE      | DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts | ref    | PRIMARY,CreativeProductId      | PRIMARY     | 4       | galileo.DeviceTypes.id                                                            |    7 | NULL  |
|  1 | SIMPLE      | DeviceTypes.CreativeProducts                            | eq_ref | PRIMARY                        | PRIMARY     | 4       | galileo.DeviceTypes.CreativeProducts.DeviceTypeCreativeProducts.CreativeProductId |    1 | NULL  |
|  1 | SIMPLE      | MediaRateType                                           | const  | PRIMARY                        | PRIMARY     | 4       | const                                                                             |    1 | NULL  |
+----+-------------+---------------------------------------------------------+--------+--------------------------------+-------------+---------+-----------------------------------------------------------------------------------+------+-------+
21 rows in set (0.00 sec)

0 个答案:

没有答案