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秒という驚異的な時間がかかります(低速で低スペックのラップトップで)
このクエリは、それぞれ約 1.5 秒かかる 2 つの非常によく似たクエリで構成されています。
私のアプリでは、最大 4 つの類似の共用体が必要になる可能性があります。
より効率的に書く方法を誰かが提案できますか?