简体   繁体   English

您是否应该将多个简单查询合并为一个?

[英]Should you combine multiple simple queries into one?

Performance wise, is there any reason to combine (using JOIN for example) simple queries like the ones below into one? 在性能方面,是否有任何理由将下面的简单查询(例如,使用JOIN)组合在一起?

media_id and user_id are INDEXES media_id和user_id为INDEXES

I can't seem to find an answer and have combined similar queries in the past and ended up with longer overall runtimes. 我似乎找不到答案,过去合并了类似的查询,最终导致更长的总体运行时间。

Edit: I was always told that the fewer queries the better, does that apply here? 编辑:我总是被告知,查询越少越好,这适用于这里吗?

0.0028      SELECT *
FROM `z_ratings`
WHERE `media_id` = 18610
AND `user_id` = '1' 

0.0013      SELECT *
FROM `z_watchlist`
WHERE `media_id` = 18610
AND `user_id` = '1' 

0.0016      SELECT *
FROM `z_favourite`
WHERE `user_id` = '1'
AND `media_id` = 18610 

0.0021      SELECT *
FROM `z_watched`
WHERE `user_id` = '1'
AND `media_id` = 18610 

It depends how exactly you are going to combine them. 这取决于您要如何精确地组合它们。

If you combine them into one statement using JOIN or UNION, than you need to take indexes into consideration, and check how combined query will fetch the data compare to simple ones. 如果使用JOIN或UNION将它们组合成一个语句,则需要考虑索引,并检查组合查询与简单查询相比如何获取数据。

You may also just concatenate statements into statement block using ";", or to pack them into Stored Procedure. 您也可以使用“;”将语句连接到语句块中,或将其打包到存储过程中。 In that case you may actually save time by reducing number of calls to server, but then you may need more complex logic to analyze the result. 在这种情况下,您实际上可以通过减少对服务器的调用次数来节省时间,但是随后可能需要更复杂的逻辑来分析结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM