优化此 SQL 查询?

Optimize this SQL query?

我正在使用 MySQL 数据库。我有两个表 -

  1. Product_Data(它有一些列包括 PK productDataId)
  2. Product_Data_Link(它有 5 列 Id(PK)、productDataId(FK)、LinkTypeId、IsActive、ProductDataLinkUrl)

我是 运行 在针对多个产品的一次服务调用中进行此查询。有没有办法优化它以快速执行。

select
    link1.ProductDataLinkUrl as ProductDataUrl1,
    link2.ProductDataLinkUrl as ProductDataUrl2,
    link3.ProductDataLinkUrl as ProductDataUrl3 ,
    link4.ProductDataLinkUrl as ProductDataUrl4 
FROM
    product_data pd 
left join
    product_data_links link1 
        on link1.ProductDataId = pd.ProductDataId 
        and link1.ProductDataLinkTypeId = 1 
        and link1.ProductDataLinkIsActive = 1 
left join
    product_data_links link2 
        on link2.ProductDataId = pd.ProductDataId 
        and link2.ProductDataLinkTypeId = 2 
        and link2.ProductDataLinkIsActive = 1 
left join
    product_data_links link3 
        on link3.ProductDataId = pd.ProductDataId 
        and link3.ProductDataLinkTypeId = 3 
        and link3.ProductDataLinkIsActive = 1 
left join
    product_data_links link4 
        on link4.ProductDataId = pd.ProductDataId 
        and link4.ProductDataLinkTypeId = 4 
        and link4.ProductDataLinkIsActive = 1 
WHERE
    pd.ProductDataId = 99999

您的查询应该适用于以下索引:

  • product_data(ProductDataId)
  • product_data_links(ProductDataId, ProductDataLinkTypeId, ProductDataLinkIsActive)

在某些情况下,使用 group by:

会更快
  select max(case when ProductDataLinkTypeId = 1 then ProductDataLinkUrl end) as ProductDataUrl1,
         max(case when ProductDataLinkTypeId = 2 then ProductDataLinkUrl end) as ProductDataUrl2,
         max(case when ProductDataLinkTypeId = 3 then ProductDataLinkUrl end) as ProductDataUrl3,
         max(case when ProductDataLinkTypeId = 4 then ProductDataLinkUrl end) as ProductDataUrl4
  from product_data_links pdl
  where ProductDataId = 99999 and
        ProductDataLinkIsActive = 1 and
        ProductDataLinkTypeId in (1, 2, 3, 4);

您需要上面的第二个索引用于此查询。

我认为添加具有公共相关字段的索引很重要:newIndex(ProductDataId, ProductDataLinkTypeId, ProductDataLinkIsActive, ProductDataLinkUrl)

然后,我喜欢子查询,因为它只带来必填字段,而不是通过连接带来完整的 table。

(select 
    (select l1.ProductDataLinkUrl from product_data_links l1 where l1.ProductDataId = pd.ProductDataId 
        and l1.ProductDataLinkTypeId = 1 
        and l1.ProductDataLinkIsActive = 1 ) as ProductDataUrl1, 

    (select l2.ProductDataLinkUrl from product_data_links l2 where l2.ProductDataId = pd.ProductDataId 
        and l2.ProductDataLinkTypeId = 2
        and l2.ProductDataLinkIsActive = 1 ) as ProductDataUrl2, 

    (select l3.ProductDataLinkUrl from product_data_links l3 where l3.ProductDataId = pd.ProductDataId 
        and l3.ProductDataLinkTypeId = 3 
        and l3.ProductDataLinkIsActive = 1 ) as ProductDataUrl3,

    (select l4.ProductDataLinkUrl from product_data_links l4 where l4.ProductDataId = pd.ProductDataId 
        and l4.ProductDataLinkTypeId = 4 
        and l4.ProductDataLinkIsActive = 1 ) as ProductDataUrl4
FROM
    product_data pd 
WHERE
    pd.ProductDataId = 99999)

我会在重构的每个步骤后测量时间和结果组。

祝你好运!