[英]how to get better performance, with not in query
here is my custom sql query ; 这是我的自定义sql查询;
SELECT id,
post_title,
post_content,
comment_count,
post_date,
post_status,
post_name
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'
AND id NOT IN (SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'visible_headlane'
AND meta_value = 'On')
AND id NOT IN (SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'visible_homepage'
AND meta_value = 'On')
ORDER BY post_date DESC
LIMIT 11, 32
i can not use, LIMIT in NOT IN Query so, there is any way to use, or limit SELECT DISTINCT
sql query, or any idea ? 我不能使用LIMIT在NOT IN Query中,所以,有什么方法可以使用,或者限制SELECT DISTINCT
sql查询,或者有什么想法?
Note : I need to optimise this query because, it takes so long , like a slow query. 注意:我需要优化此查询,因为它要花很长时间,就像慢速查询一样。
Thanks. 谢谢。
Switch to NOT EXISTS( SELECT 1 FROM ... )
-- this variation does not need to compute the entire list of things; 切换到NOT EXISTS( SELECT 1 FROM ... )
-这种变化不需要计算整个事物列表; it only needs to check for the absence. 它只需要检查是否缺少。 LEFT JOIN .. ON .. IS NULL
is also more efficient. LEFT JOIN .. ON .. IS NULL
也更有效。
Furthermore, the standard schema for wp_postmeta is quite inefficient; 此外,wp_postmeta的标准架构效率很低。 see my suggestions . 看我的建议 。
Meanwhile, keep it as two tests, unlike the suggestion by 'holder'. 同时,与“持有人”的建议不同,将其保留为两个测试。
The EXISTS
would look something like 该EXISTS
看起来是这样的
AND NOT EXISTS ( SELECT 1 FROM wp_postmeta
WHERE post_id = wp_posts.id
AND meta_key = 'visible_headlane'
AND meta_value = 'On' )
which would make good use of the PRIMARY KEY(post_id, meta_key)
that I recommend. 这将充分利用我推荐的PRIMARY KEY(post_id, meta_key)
。
(Is it really 'headlane', not 'headline'?) (真的是“标题”而不是“标题”吗?)
Maybe something like this as jarlh suggested 也许像贾尔建议的那样
SELECT
ID,post_title,post_content,comment_count,post_date,post_status,post_name
FROM wp_posts t1
left join (
SELECT DISTINCT post_id from wp_postmeta
WHERE meta_key IN ('visible_headlane','visible_homepage') AND meta_value = 'On' ) t2 on t1.ID = t2.post_id
WHERE post_type = 'post'
AND post_status = 'publish'
AND t2.post_id is null
ORDER BY post_date DESC
LIMIT 11, 32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.