我正在查询 wordpress 帖子表中的一些帖子,并通过多次加入 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;
任何意见,将不胜感激 :)