如何优化包含连接和子查询的sql联合

时间:2013-05-23 13:58:54

标签: mysql php-5.3

我看了很多关于sql优化的评论,但我找不到答案。

我的应用使用此sql ...

SELECT * FROM ((
  SELECT DISTINCT 
      c.companyname,
      c.tradingas,
      a.quarantined,
      c.companyid,
      a.address1,
      a.city,
      a.postcode,
      a.region,
      c.origin_dsn,
      d.title,
      d.firstname,
      d.surname,
      d.position,
      a.telephoneno,
      a.companyemail,
      addresstypes,
      markets 
    FROM companies c
    join addresses a on c.companyid = a.companyid
    join contacts d on c.companyid = d.companyid
    where d.origin_dsn = 'ifd'
      and primarycontact = 1
      and d.clientid = 0
      and c.origin_dsn = 'ifd' and
      a.origin_dsn = 'ifd' and
      c.companyid in (
        select distinct companyid
        from products
        where description IN ('Windows and Doors','Vertical Sliders','Bi-Fold Doors')
          AND type IN ('wd:f','wd:b')
          AND material = 'PVCu'
          AND origin_dsn = 'ifd'
  ))
  UNION (
    SELECT DISTINCT
      c.companyname,
      c.tradingas,
      a.quarantined,
      c.companyid,
      a.address1,
      a.city,
      a.postcode,
      a.region,
      c.origin_dsn,
      d.title,
      d.firstname,
      d.surname,
      d.position,
      a.telephoneno,
      a.companyemail,
      addresstypes,
      markets
    FROM companies c
    join addresses a on c.companyid = a.companyid
    join contacts d on c.companyid = d.companyid
    where d.origin_dsn = 'ifd'
      and primarycontact = 1
      and d.clientid = 0
      and c.origin_dsn = 'ifd'
      and a.origin_dsn = 'ifd'
      and c.companyid in (
        select distinct companyid
        from products
        where type IN ('cr:f','cr:b')
          AND origin_dsn = 'ifd')
      )
  ) as t
  where t.quarantined = 0
    and t.origin_dsn = 'ifd'
    and t.region IN (
      'Northern Counties','North West','Yorkshire',
      'East Midlands','West Midlands','South West',
      'Home Counties','Southern Counties','Greater London',
      'Scotland','Wales','Northern Ireland')
  order by companyname

在mysql查询浏览器中运行需要惊人的3.5秒(在我的慢速,低规格笔记本电脑上)

此查询由2个非常相似的查询组成,每个查询大约需要1.5秒。

我的应用最多可能需要4个类似的工会。

有人能建议如何更有效地写出来吗?

1 个答案:

答案 0 :(得分:0)

我会将您的公司产品标准移至预查询FIRST ... 然后加入以获取其他详细信息......因此,您的公司ID只能在前期选择一次。 PreQuery,您可以加入我在此处采样的多个条件......

where 
      ( first set of criteria )
   OR ( second set of criteria )
   OR ( third set of criteria )

(但是,你的两个标准都使用origin_dsn ='ifd',这可能会简化一次,但我不知道你的标准是如何构建的。)

为了帮助优化查询,我会确保在您的Products表上 (Origin_DSN,type,材料)上的索引

对于地址表,(companyid,origin_dsn,quarantined,region)上的索引

对于联系人表,索引(companyid,origin_dsn,clientid)

像...一样的东西。

SELECT DISTINCT 
      c.companyname,
      c.tradingas,
      a.quarantined,
      c.companyid,
      a.address1,
      a.city,
      a.postcode,
      a.region,
      c.origin_dsn,
      d.title,
      d.firstname,
      d.surname,
      d.position,
      a.telephoneno,
      a.companyemail,
      addresstypes,
      markets 
   FROM 
      ( select distinct 
              p.companyid
           from 
              products p
           where 
                 (     p.origin_dsn = 'ifd' 
                   AND p.`type` IN ('cr:f','cr:b') )
              OR (     p.origin_dsn = 'ifd' 
                   AND p.`type` IN ('wd:f','wd:b')
                   AND p.material = 'PVCu' 
                   AND p.description IN ('Windows and Doors','Vertical Sliders','Bi-Fold Doors') ) ) as PreQuery

         JOIN companies c
            ON PreQuery.CompanyID = c.CompanyID


            join addresses a 
               on c.companyid = a.companyid
              and c.origin_dsn = a.origin_dsn 
              and a.quarantined = 0
              and a.Region in ( 'Northern Counties',
                                'North West',
                                'Yorkshire',
                                'East Midlands',
                                'West Midlands',
                                'South West',
                                'Home Counties',
                                'Southern Counties',
                                'Greater London',
                                'Scotland',
                                'Wales',
                                'Northern Ireland' )
            join contacts d 
               on c.companyid = d.companyid
              AND c.origin_dsn = d.origin_dsn 
              and d.clientid = 0
   where 
          primarycontact = 1
      and c.origin_dsn = 'ifd' 
   order by 
      c.companyname