简体   繁体   English

加入标签表 - 我应该加入PHP还是加入DB服务器?

[英]Joining with a tags table — should I join in PHP or on the DB server?

Using the Toxi solution , how should I select the tags for a certain "Bookmark" (to keep the Delicious theme): 使用Toxi解决方案我应该如何为某个“Bookmark”选择标签(以保持Delicious主题):

I can either: 我可以:

1) Join in a single query: 1)加入一个查询:

select bookmark.title, bookmark.url, 
(
  SELECT group_concat( tags.name ) as tagNames
  FROM taggings INNER JOIN tags
  ON taggings.tagId_fk=tags.tagId
  WHERE taggings.bookmarkId_fk = bookmarks.bookmarkId_fk
)
from bookmarks
where bookmarks.id=1 ;

^^ That gives
title    url               tagNames
A bkmrk  http://url.com    tag1,tag2,tag3

2) Use two queries : one to retrieve the bookmark id's to display, then another to retrieve the tags for those bookmarks. 2) 使用两个查询 :一个用于检索要显示的书签ID,另一个用于检索这些书签的标签。 The results can then be merged in PHP. 然后可以在PHP中合并结果。

So really this question is: In general efficiency/database load-wise is it better to do more joining in a single query or multiple queries? 所以真的这个问题是:在一般效率/数据库负载方面 ,更好地加入单个查询或多个查询?

How do you make that kind of decision? 你是如何做出这种决定的? Or do you simply not think about it until load causes a problem? 或者你只是在负载导致问题之前不考虑它?

Server side is more efficient. 服务器端更高效。

In both cases, the server must read all of the tags. 在这两种情况下,服务器都必须读取所有标记。

If you bring them to PHP, then they must all travel over the wire and PHP has to fiddle with them. 如果你把它们带到PHP,那么它们必须通过网络传输,PHP必须摆弄它们。

If you do them on the server, the finished answer (smaller) comes over the wire ready for PHP to pass it up to the UI. 如果你在服务器上执行它们,那么完成的答案(较小的)就可以通过线路准备好让PHP将其传递给UI。

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

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