你好,我正在寻找优化mysql查询的方法,基本上,我是为属于category_id = 25和source_id的用户获取文章,而不是存储用户未订阅的源id的表中。
select
a.article_id,
a.article_title,
a.source_id,
a.article_publish_date,
a.article_details,
n.source_name
from sources n
INNER JOIN articles a
ON (a.source_id = n.source_id)
WHERE n.category_id = 25
AND n.source_id NOT IN(select
source_id
from news_sources_deselected
WHERE user_id = 5)
ORDER BY a.article_publish_date DESC用于项目表的模式
CREATE TABLE IF NOT EXISTS `articles` (<br>
`article_id` int(255) NOT NULL auto_increment,<br>
`article_title` varchar(255) NOT NULL,<br>
`source_id` int(255) NOT NULL,<br>
`article_publish_date` bigint(255) NOT NULL,<br>
`article_details` text NOT NULL,<br>
PRIMARY KEY (`article_id`),<br>
KEY `source_id` (`source_id`),<br>
KEY `article_publish_date` (`article_publish_date`)<br>
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Contains articles.';源表结构
CREATE TABLE IF NOT EXISTS `sources` (<br>
`source_id` int(255) NOT NULL auto_increment,<br>
`category_id` int(255) NOT NULL,<br>
`source_name` varchar(255) character set latin1 NOT NULL,<br>
`user_id` int(255) NOT NULL,<br>
PRIMARY KEY (`source_id`),<br>
KEY `category_id` (`category_id`),<br>
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='News Sources.'项目表有大约30万条记录,源表包含大约1000条记录,查询大约需要180秒才能执行。
任何帮助都将不胜感激。

发布于 2013-04-17 05:37:43
我通过划分表来解决这个问题,但我仍然愿意听取建议。
发布于 2013-04-10 09:16:46
尝试使用带IS条件的派生查询。你解释说有一个依赖的子查询。忽略使用它,并使用嘲笑查询您的问题。这将提高性能。
select
a.article_id,
a.article_title,
a.source_id,
a.article_publish_date,
a.article_details,
n.source_name
from sources n
INNER JOIN articles a
ON (a.source_id = n.source_id)
LEFT JOIN (SELECT *
FROM news_sources_deselected
WHERE user_id = 5) AS nsd
ON nsd.source_id = n.source_id
WHERE n.category_id = 25
AND nsd.source_id IS NULL
ORDER BY a.article_publish_date DESC发布于 2013-04-10 08:51:56
在查询和分析结果之前使用EXPLAIN。
这里,您可以找到如何开始优化工作。
https://stackoverflow.com/questions/15921603
复制相似问题