0

我查看了许多关于 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 个类似的工会。

谁能建议如何更有效地编写它?

4

1 回答 1

0

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

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

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

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

对于地址表,( companyid,origin_dsn,隔离,区域)上的索引

FOR 联系人表上的索引( 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
于 2013-05-23T16:53:16.683 回答