レポートツールについて、mysqlで次のクエリを作成しました。
インライン選択です。クエリは正しい結果を返しますが、実行に時間がかかります。誰かがそれをより効率的にするために別の方法を提案したり、クエリを書いたりできますか?
SELECT z.name1, (
SELECT COUNT( AES_DECRYPT( l.answertext, "aaa" ) )
FROM household_data l
INNER JOIN sms_household m
ON l.prim_key = m.hhid
INNER JOIN sms_psu n
ON n.psu = m.psu
AND n.state = m.state
AND n.district = m.district
INNER JOIN (
SELECT p.prim_key, p.fieldname
FROM household_data p
WHERE p.basicname = 'Q05'
AND AES_DECRYPT( p.answertext, "aaa" ) =2
) women
ON l.prim_key = women.prim_key
AND SUBSTR( l.fieldname, 5, 1 ) = SUBSTR( women.fieldname, 5, 1 )
WHERE l.basicname = 'Q08' AND AES_DECRYPT( l.answertext, "aaa" ) = 14
AND z.psu = n.psu
[AND n.state IN ( {state} )]
[AND n.district IN ( {district} )]
) female14, (
SELECT COUNT( AES_DECRYPT( l.answertext, "aaa" ) )
FROM household_data l
INNER JOIN sms_household m ON l.prim_key = m.hhid
INNER JOIN sms_psu n
ON n.psu = m.psu
AND n.state = m.state
AND n.district = m.district
INNER JOIN (
SELECT p.prim_key, p.fieldname
FROM household_data p
WHERE p.basicname = 'Q05'
AND AES_DECRYPT( p.answertext, "aaa" ) =2
) women
ON l.prim_key = women.prim_key
AND SUBSTR( l.fieldname, 5, 1 ) = SUBSTR( women.fieldname, 5, 1 )
WHERE l.basicname = 'Q08' AND AES_DECRYPT( l.answertext, "aaa" ) = 15
AND z.psu = n.psu
[AND n.state IN ( {state} )]
[AND n.district IN ( {district} )]
) female15, (
SELECT COUNT( AES_DECRYPT( l.answertext, "aaa" ) )
FROM household_data l
INNER JOIN sms_household m
ON l.prim_key = m.hhid
INNER JOIN sms_psu n
ON n.psu = m.psu
AND n.state = m.state
AND n.district = m.district
INNER JOIN (
SELECT p.prim_key, p.fieldname
FROM household_data p
WHERE p.basicname = 'Q05'
AND AES_DECRYPT( p.answertext, "aaa" ) =2
) women
ON l.prim_key = women.prim_key
AND SUBSTR( l.fieldname, 5, 1 ) = SUBSTR( women.fieldname, 5, 1 )
WHERE l.basicname = 'Q08' AND AES_DECRYPT( l.answertext, "aaa" ) =16
AND z.psu = n.psu
[AND n.state IN ( {state} )]
[AND n.district IN ( {district} )]
) female16, (
SELECT count(AES_DECRYPT(household_data.answertext , "aaa")) * 100 / (
SELECT count(AES_DECRYPT(household_data.answertext , "aaa"))
FROM household_data
INNER JOIN sms_household
INNER JOIN sms_psu
ON sms_psu.psu = sms_household.psu
AND sms_psu.state = sms_household.state
AND sms_psu.district = sms_household.district
WHERE basicname = 'Q07_year' AND z.psu= sms_psu.psu
[AND sms_psu.state IN ( {state} )]
[AND sms_psu.district IN ( {district} )]
)
FROM household_data
INNER JOIN sms_household
INNER JOIN sms_psu
ON sms_psu.psu = sms_household.psu
AND sms_psu.state = sms_household.state
AND sms_psu.district = sms_household.district
WHERE AES_DECRYPT(household_data.answertext , "aaa") = 9998
AND basicname = 'Q07_year'
AND z.psu = sms_psu.psu
[AND sms_psu.state IN ( {state} )]
[AND sms_psu.district IN ( {district} )]
) PercYearDontKnow
FROM household_data x
INNER JOIN sms_household y
ON x.prim_key = y.hhid
INNER JOIN sms_psu z
ON y.psu = z.psu
AND z.state = y.state
AND z.district = y.district
WHERE 1=1
[AND y.state IN ( {state} )]
[AND y.district IN ( {district} )
GROUP BY z.psu