ワードプレスの投稿テーブルでいくつかの投稿を照会し、postmeta テーブルに複数回参加して複数のメタ値を取得しています。また、投稿列とメタ値の両方にいくつかの条件があります。クエリは機能しているようですが、条件を正しい場所に配置したかどうかに興味があります。次のクエリを比較すると、投稿 ID とそれに関連するメタを最も効果的/効率的に取得できるのはどれですか?
これです:
SELECT a.ID as `post_id`, b.meta_value as `metaval1`, c.meta_value as `metaval2`, d.meta_value as `metaval3`
FROM posts AS a
JOIN postmeta AS b ON (b.post_id = a.ID AND b.meta_key = 'metakey1')
LEFT JOIN postmeta as c ON (c.post_id = a.ID AND c.meta_key = 'metakey2')
LEFT JOIN postmeta as d ON (d.post_id = a.ID AND d.meta_key = 'metakey3')
WHERE
b.meta_value != 10
AND a.post_status = 'pending'
AND a.post_date < '2013-03-07 00:00:00'
AND a.post_type = "post"
ORDER BY a.post_date DESC
LIMIT 100;
またはこれ:
SELECT a.ID as `post_id`, b.meta_value as `metaval1`, c.meta_value as `metaval2`, d.meta_value as `metaval3`
FROM posts AS a
JOIN postmeta AS b ON (b.post_id = a.ID)
LEFT JOIN postmeta as c ON (c.post_id = a.ID)
LEFT JOIN postmeta as d ON (d.post_id = a.ID)
WHERE
b.meta_key = 'metakey1'
AND b.meta_value != 10
AND c.meta_key = 'metakey2'
AND d.meta_key = 'metakey3'
AND a.post_status = 'pending'
AND a.post_date < '2013-03-07 00:00:00'
AND a.post_type = "post"
ORDER BY a.post_date DESC
LIMIT 100;
アドバイスをいただければ幸いです:)