Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 90EFB9FBDD2 for ; Wed, 18 Jul 2007 18:54:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 72851-04 for ; Wed, 18 Jul 2007 18:53:59 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.182]) by postgresql.org (Postfix) with ESMTP id CBB1F9FB689 for ; Wed, 18 Jul 2007 18:54:08 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id d32so755799pye for ; Wed, 18 Jul 2007 14:54:08 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=FkCV8s70UO8hMbBIN23FhuiUNUjhiR35PwCTEnzLQEgg5mmhfFFWhwCl5tcBdm06Z35tUHYFPnLTziWWntOyCjAqgLNh/rOX2ivaYu8nz2vWO2q2LXJAvjwU52BEdkL7Uy5uFIWXqmpt3b+aWAHppdwFfO0A+s0HbqDPY2TfXcU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=nqUrXjEl26PaGsxyC4W8Q4EexG2m9EfAoJy0hwP6pixPbNV4NrYnsioZBO/RirT9H/qx+c81AyP5Tv+KbSHAypGwRZo7IXOjM1IyIv4BYINj0OiPzQYbXjLgPIgrB4OfAsqqRJZ08T8yq1ofgtDnPeZwFLRdFP/CLfdc62laI4k= Received: by 10.65.239.14 with SMTP id q14mr1877747qbr.1184795647743; Wed, 18 Jul 2007 14:54:07 -0700 (PDT) Received: by 10.64.208.4 with HTTP; Wed, 18 Jul 2007 14:54:07 -0700 (PDT) Message-ID: Date: Wed, 18 Jul 2007 16:54:07 -0500 From: "Bryan Murphy" To: "lists@on-track.ca" Subject: Re: Optmal tags design? Cc: pgsql-performance@postgresql.org In-Reply-To: <1271.69.31.174.216.1184793960.squirrel@webmail.ctgameinfo.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_52339_8541576.1184795647693" References: <1271.69.31.174.216.1184793960.squirrel@webmail.ctgameinfo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/234 X-Sequence-Number: 25918 ------=_Part_52339_8541576.1184795647693 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline We store tags on our items like this like this: Tag.ID INT NOT NULL PRIMARY KEY Tag.Value TEXT LCASE NOT NULL UNIQUE Item.ID INT NOT NULL PRIMARY KEY ItemTagBinding.ItemID INT NOT NULL REFERENCES Item.ID ItemTagBinding.TagID INT NOT NULL REFERENCES Tag.ID ItemTagBinding.ItemID + ItemTagBinding.TagID UNIQUE with appropriate indexes on the columns we need to frequently query. We have about 3 million tag bindings right now, and have not run into any performance issues related to tagging other than generating tag clouds (which we pre-calculate anyway). I'll have to get back to you when we get up to 10's, or even 100's of millions and let you know how it scaled. Bryan On 7/18/07, lists@on-track.ca wrote: > > I am planning to add a tags (as in the "web 2.0" thing) feature to my web > based application. I would like some feedback from the experts here on > what the best database design for that would be. > > The possibilities I have come up with are: > * A tags table containing the tag and id number of what it links to. > select pid from tags where tag='bla' > select tag from tags where pid=xxx. > > * a tags table where each tag exists only once, and a table with the tag > ID and picture ID to link them together. > > select pid from tags inner join picture_tags using(tag_id) where tag='bla' > select tag from tags inner join picture_tags using(tag_id) where pid='xxx' > > * A full text index in the picture table containing the tags > > select pid from pictures where tags @@ to_tsquery('bla') > (or the non-fti version) > select pid from pictures where tags ~* '.*bla.*' > > select tags from pictures where pid=xxx; > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match > ------=_Part_52339_8541576.1184795647693 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline We store tags on our items like this like this:

Tag.ID INT NOT NULL PRIMARY KEY
Tag.Value TEXT LCASE NOT NULL UNIQUE

Item.ID INT NOT NULL PRIMARY KEY

ItemTagBinding.ItemID INT NOT NULL REFERENCES Item.ID
ItemTagBinding.TagID INT NOT NULL REFERENCES Tag.ID
ItemTagBinding.ItemID + ItemTagBinding.TagID UNIQUE

with appropriate indexes on the columns we need to frequently query.

We have about 3 million tag bindings right now, and have not run into any performance issues related to tagging other than generating tag clouds (which we pre-calculate anyway).

I'll have to get back to you when we get up to 10's, or even 100's of millions and let you know how it scaled.

Bryan

On 7/18/07, lists@on-track.ca <lists@on-track.ca> wrote:
I am planning to add a tags (as in the "web 2.0" thing) feature to my web
based application. I would like some feedback from the experts here on
what the best database design for that would be.

The possibilities I have come up with are:
* A tags table containing the tag and id number of what it links to.
select pid from tags where tag='bla'
select tag from tags where pid=xxx.

* a tags table where each tag exists only once, and a table with the tag
ID and picture ID to link them together.

select pid from tags inner join picture_tags using(tag_id) where tag='bla'
select tag from tags inner join picture_tags using(tag_id) where pid='xxx'

* A full text index in the picture table containing the tags

select pid from pictures where tags @@ to_tsquery('bla')
(or the non-fti version)
select pid from pictures where tags ~* '.*bla.*'

select tags from pictures where pid=xxx;

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match

------=_Part_52339_8541576.1184795647693--