MyISAM 性能:加入分解?
在 高性能 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
连接分解在某些情况下很有用,但在大多数情况下连接会更快。
在你的例子中,我会坚持使用连接,而不是在 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.