Jump to content

Recommended Posts

I have been thinking about making a change to my blog, mainly i want to group my posts by "tag".

 

I was thinking about what would be the most efficient method to do this. The first method which i have in mind, is having two tables, one containing the Posts and Comments, and one for the Tags.

 

Each time i make a new post, i will add the relevant tags. Then a post is made in the "tags table" for each tag, with a number corresponding to the auto-increment value of the real post.

 

This however creates a "problem", where the Blog needs to query the second database, before it can show the posts tagged as "tagname".

 

 

I'm thinking that its fine to do it like this, but i would still like your ideas on this. Are there any methods that are more efficiant?

Link to comment
https://forums.phpfreaks.com/topic/155125-solved-blog-change-efficient-tags/
Share on other sites

That sounds like a pretty stupid way of doing it. Why not just have an extra column in your blog table for tags? Then just get/insert data from/to that?

 

but this would be inefficient if you have more than one tag per post. A one to many relationship with a Tags table would be much better and allow as many tags against a post as you wanted.

Hi

 

Having one field with all the tags sounds nasty, and would likely push you into matching up using php rather than just keeping it in the database.

 

Assuming 2 tables

 

TheBlogTable

BlogId

BlogPost

 

TheTagTable

Id

BlogId

TagText

 

then you could just do:-

 

SELECT BlogId, BlogPost FROM TheBlogTable WHERE BlogId IN (SELECT BlogId FROM TheTagTable WHERE TagText = "TagToCheckFor" )

 

All the best

 

Keith

.....SNIP.......

SELECT BlogId, BlogPost FROM TheBlogTable WHERE BlogId IN (SELECT BlogId FROM TheTagTable WHERE TagText = "TagToCheckFor" )

 

 

Sounds good but I think:

SELECT TheBlogTable.BlogId, TheBlogTable.BlogPost 
FROM TheBlogTable
INNER JOIN TheTagTable on TheBlogTable.BlogId = TheTagTable.BlogId
WHERE TheTagTable.TagText = "TagToCheckFor"

 

I am not a big fan of sub queries, you cannot easily predict the performance......

 

Hi

 

Tend to agree and thought of a JOIN, but brain fade struck thinking about multiple records being returned unncessarily (which is not even relevant).

 

Might be worth this slight change though

 

SELECT TheBlogTable.BlogId, TheBlogTable.BlogPost 
FROM TheBlogTable
INNER JOIN TheTagTable on TheBlogTable.BlogId = TheTagTable.BlogId
AND TheTagTable.TagText = "TagToCheckFor"

 

That might force the join to only go against records with a matching tag, rather than possibly do a massive join then only filter for matching tags.

 

All the best

 

Keith

Its also possible with FULLTEXT index, and an extra column, from what i was able to find out. But there are some issues with special characters in tagnames, which i don't want to account for atm.

 

Besides, it would also limit the number of characters, unless i didn't mind the perfoamance lost by using TEXT fields.

 

 

I'm going to use a two-table solution to begin with. This is still very new for me, so I'm going to use the solution which is easiest for me, and then gradually try other solutions, it was also what i first came up with.

 

And who knows, maybe its time for me to try and work with a few Joins. :-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.