使用 Left Join 的 Mysql 查询太慢了

发布于 2024-09-10 05:29:22 字数 298 浏览 2 评论 0原文

查询:

   select `r`.`id` as `id` 
     from `tbl_rls` as `r` 
left join `tblc_comment_manager` as `cm` on `cm`.`rlsc_id` != `r`.`id`

两个表都有8k条记录,但是为什么很慢,有时需要2-3分钟甚至更长时间?

天啊,这个查询让 mysql 服务器宕机了。将立即回复大家:(

所有建议对列进行索引的人都是正确的。 是的,我写的查询很愚蠢而且有问题。谢谢纠正我。

Query:

   select `r`.`id` as `id` 
     from `tbl_rls` as `r` 
left join `tblc_comment_manager` as `cm` on `cm`.`rlsc_id` != `r`.`id`

Both tables have 8k records but why is it very slow, taking 2-3 minutes and more sometimes?

OMG , this query makes mysql server down. Will get back to you peoples in a second :(

All peoples those suggested Indexing the columns are all Correct.
Yeh the query i wrote was silly and buggy. Thanks correcting me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拧巴小姐 2024-09-17 05:29:22

还可以考虑为表建立索引。我们正在一个超过 100 万条记录的表上运行多个左连接,返回结果的时间不会超过一两秒。

Consider also indexing your tables. We're running multiple left joins on a 1million+ record table that doesn't take more than a second or two to return results.

独闯女儿国 2024-09-17 05:29:22

您真的需要 != 还是本来就是 ​​=

 select `r`.`id` as `id` from `tbl_rls` as `r` 
  left join `tblc_comment_manager` as `cm` 
on  `cm`.`rlsc_id`!=`r`.`id

这将几乎选择两个表的笛卡尔积。 (我猜大约有 6000 万行)

编辑:来自评论

是的,它是“!=”来匹配tbl_rls.id
这些不在 tblc_comment_manager 中

我认为如果您想使用 outer join 方法,这就是您所需要的。

 select DISTINCT `r`.`id` as `id` from `tbl_rls` as `r` 
  left join `tblc_comment_manager` as `cm` 
on  `cm`.`rlsc_id`=`r`.`id
WHERE `cm`.`rlsc_id` IS NULL

虽然我的偏好通常是

 select `r`.`id` as `id` 
 from `tbl_rls`
 as `r` 
 WHERE NOT EXISTS(
          SELECT * FROM `tblc_comment_manager` as `cm` 
          WHERE  `cm`.`rlsc_id`=`r`.`id)

Do you really need the != or is it meant to be =?

 select `r`.`id` as `id` from `tbl_rls` as `r` 
  left join `tblc_comment_manager` as `cm` 
on  `cm`.`rlsc_id`!=`r`.`id

This will select nearly the cartesian product of the 2 tables. (I guess around 60 million rows)

Edit: From the comment

yes it is " != " to match tbl_rls.id
those are not in tblc_comment_manager

I think this is what you need if you want to use the outer join approach.

 select DISTINCT `r`.`id` as `id` from `tbl_rls` as `r` 
  left join `tblc_comment_manager` as `cm` 
on  `cm`.`rlsc_id`=`r`.`id
WHERE `cm`.`rlsc_id` IS NULL

Although my preference is usually

 select `r`.`id` as `id` 
 from `tbl_rls`
 as `r` 
 WHERE NOT EXISTS(
          SELECT * FROM `tblc_comment_manager` as `cm` 
          WHERE  `cm`.`rlsc_id`=`r`.`id)
丘比特射中我 2024-09-17 05:29:22

您想选择什么?

如果您想查找在其他表中没有匹配记录的 tbl_rls 记录,请使用此查询

select `r`.`id`
from `tbl_rls` as `r` 
left join `tblc_comment_manager` as `cm` 
    on  `cm`.`rlsc_id`=`r`.`id
where `cm`.`rlsc_id` IS NULL

What do you want to select?

Use this query if you want to find tbl_rls records that haven't matching records in other table

select `r`.`id`
from `tbl_rls` as `r` 
left join `tblc_comment_manager` as `cm` 
    on  `cm`.`rlsc_id`=`r`.`id
where `cm`.`rlsc_id` IS NULL
╭ゆ眷念 2024-09-17 05:29:22

MySQL 的 EXPLAIN 可能会帮助您找出发生了什么。

MySQL's EXPLAIN might help you finding out what is going on.

放血 2024-09-17 05:29:22

您可能需要提供更多信息。但我会尝试的一件事是反转 ON 子句的顺序(因为它很简单):

ON r.id != cm.rlsc_id

编辑:你应该在 PK 上放置索引(id)列。

但我认为这篇文章可能会对您有所帮助

基本上它表示 NOT INLEFT JOIN 占用更少的资源。该文章中的评论者提到使用 NOT EXISTS 是最好的。

另外,我不确定这是否准确,但是这篇文章说< code>NOT IN 进行全表扫描,NOT EXISTS 可以使用索引

You may need to provide more info. But one thing I would try is reversing the order of your ON clause (because it's so easy):

ON r.id != cm.rlsc_id

Edit: and you should put indexes on your PK (id) columns.

But I think this article might help you out.

Basically it says that NOT IN takes less resources than LEFT JOIN. A commenter in that article mentions using NOT EXISTS is best.

Also, I'm not sure this is accurate or not, but this article says that NOT IN does a full table scan, and NOT EXISTS can use an index.

小猫一只 2024-09-17 05:29:22

看起来您想要的 r.id 值不在 tblc_comment_manager 表中。

使用 Not In

select r.id 作为 id
来自 tbl_rlsr
其中 r.id 不在(从 tblc_comment_manager 中选择不同的 cm.rlsc_id 作为厘米

Looks like you are wanting the r.id values that are not in the tblc_comment_manager table.

Use a Not In

select r.id as id
from tbl_rls as r
where r.id not in (select distinct cm.rlsc_id from tblc_comment_manager as cm)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文