MyISAM 性能:加入分解?

发布于 2024-10-25 05:00:00 字数 897 浏览 3 评论 0原文

高性能 MySQL 第 159 页,他们讨论了将复杂查询分解为简单查询:

转换

SELECT * FROM tag
JOIN tag_post ON tag_post.tag_id=tag.id
JOIN post ON tag_post.post_id=post.id
WHERE tag.tag='mysql';

SELECT * FROM tag WHERE tag='mysql';
SELECT * FROM tag_post WHERE tag_id=1234;
SELECT * FROM post WHERE post.id in (123,456,567,9098,8904);

并在应用程序中自行进行实际连接。

我的问题是,当最终查询有一个 where 子句需要匹配几千个 ID(实际表本身有大约 500k 条目)时,这是否仍然是一个好主意。

我的意思是,使用类似的查询

SELECT * FROM post WHERE post.id in (123,456,567, ... <a few thousand IDs here> ... ,9098,8904);

而不是上面的连接语句会受到很大的惩罚吗?将此逻辑移至数据库内的存储过程是否有帮助(同时考虑 MySQL 中存储过程的实现有多差)?

in High Performance MySQL on page 159 they talk about breaking up complex queries into simple ones:

Converting

SELECT * FROM tag
JOIN tag_post ON tag_post.tag_id=tag.id
JOIN post ON tag_post.post_id=post.id
WHERE tag.tag='mysql';

To

SELECT * FROM tag WHERE tag='mysql';
SELECT * FROM tag_post WHERE tag_id=1234;
SELECT * FROM post WHERE post.id in (123,456,567,9098,8904);

And sort of doing the actual join yourself in your application.

My Question is wether this is stil such a good idea when the final query has a where-clause with a few thousand IDs it needs to match (the actual table itself has about 500k entries).

What I mean is, will there be a big penalty for having a query like

SELECT * FROM post WHERE post.id in (123,456,567, ... <a few thousand IDs here> ... ,9098,8904);

instead of the join-statement above? Would it help to move this logic to Stored Procedures inside the Database (while considering how poorly stored procedures are implemented in MySQL)?

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

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

发布评论

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

评论(1

朮生 2024-11-01 05:00:00

连接分解在某些情况下很有用,但在大多数情况下连接会更快。

在你的例子中,我会坚持使用连接,而不是在 IN 子句中传递几千个 ID。

Join decomposition is useful in certain situations, but in most situations the joins are going to be faster.

In your case, I would stick with the joins instead of passing in a few thousand IDs in an IN clause.

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