在 MySQL 性能中通过硬编码列表过滤掉行

发布于 2024-08-08 06:50:43 字数 358 浏览 0 评论 0原文

我有一个硬编码的值列表,例如:1、5、7、8 等。

我必须从表中过滤掉上面列表中具有 ID 的行,因此我执行如下操作:

Select
* 
from myTable m
   left join othertable t
   on t.REF_ID = m.ID
where m.ID not in (1,5,7,8...)

但是,当 othertable 和 myTable 中有更多值(如 1000)和更多行(100)时,此查询开始变慢。我有一个关于 REF_ID 和 ID 的索引。看来“where m.ID in (1,5,7,8)”部分是问题所在。

是否有更快的方法通过硬编码值列表过滤行?

I have a hardcoded list of values like: 1,5,7,8 and so on.

And I must filter out rows from table that have ID in list above, so I do something like this:

Select
* 
from myTable m
   left join othertable t
   on t.REF_ID = m.ID
where m.ID not in (1,5,7,8...)

But when I have more values (like 1000) and more rows (100) in othertable and myTable this query starts to be slow. I have an index on REF_ID and ID. It seems that the part "where m.ID in (1,5,7,8) is the problem.

Is there faster way to filter out rows by hardcoded list of values?

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

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

发布评论

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

评论(1

深陷 2024-08-15 06:50:43

尝试将您的列表作为 temptable.ID 放入临时表中并执行

SELECT * 
FROM myTable m
LEFT JOIN othertable t ON t.REF_ID = m.ID
LEFT JOIN temptable ON m.ID = temptable.ID
WHERE temptable.ID IS NULL

Try putting your list in a temporary table as temptable.ID and doing

SELECT * 
FROM myTable m
LEFT JOIN othertable t ON t.REF_ID = m.ID
LEFT JOIN temptable ON m.ID = temptable.ID
WHERE temptable.ID IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文