简体   繁体   中英

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?

media_id and user_id are 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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