What is the best approach to store hashtags in a database?

Yogesh Dahake
5 min readNov 12, 2021

--

What is the best approach to store hashtags in a database? if you dont know about hashtags then you would not using any social sites in a great extent. So, I am going to explain you about the Importance of Hashtags, Where,Why, and How to Use them benifits of hashtag and how you can implement it with database schema or tables.

Tags is not an new trend anymore if you are newbie to instagram or twitter tags or hashtags are something you need to get familiar with.

The History of the Hashtag in two lines

According to socialmediatoday.com The hashtag symbol itself came into existence sometime around the 14th century.

Before it was used on telephones and Twitter, the hashtag came from “lb,” the Latin abbreviation for “pound”. Thus, we (in the U.S.) called it the pound sign for a while.

It wasn’t called hashtag until its usage became popular on social media.

Why hashtag?

Hashtag help increase your social media presence as they make your content viewable by anyone who has an interest in your hashtag as it goes beyond just your followers. Hashtags can help you build a brand for your business. If you participate in high-traffic conversation by discovering the biggest trending topics.

Hashtags on LinkedIn

LinkedIn hasn't always supported hashtags, but now they providing the hashtag facilities to their users. People can search for hashtags on LinkedIn in the same way they can on other platforms, and LinkedIn uses hashtags to monitor what's important to users so it can show them more relevant content.

How to use them

There's a couple different solutions.

So let's step through three potential structures that we could use and remember the way that this works is that one photo can have 20 or 50 hash tags.

But” there is a limit on Instagram.

I mean you can keep adding them as many as you want but only 30 actually will show up or it's not that they won't show up but it's that when you search for a hash tag your photo will only show up, if you use less than 30. The idea is you can add hash tags to a photo but then you can also go click on a hash tag and see all the associated photos with it.

There are three popular solution to tagging.

Solution #1

In first solution where we actually just have our photos table but we add a column called tags and it's a string varchar that just has our tags and we can separate them by the hash

You need to add a new tag into something you'd basically just take all the other tags and then concatenate in at the end a new tag or at the beginning and then you could have an order to you know figure out which ones were posted first I guess based off of that order that they're in.

You know if you want to find all photos that are tagged with “goldfish” you just basically have to use where tags like ‘%cute%’

Here are some Advantages for this solution,

Easy to implement

Disadvantages for this solution,

Limited number of tags can be stored.

Cannot store additional information(first time/person the hashtag is used or time).

Have to be careful with searching.

It is the easy solution but it's not going to be the best !!

Solution #2

Another solution is to use two tables

We have our photos as our photos table then we have a tag's table where we have our tag name. In this case cute and then a corresponding photo ID and then a photo with ID 3 which is curresponding to the lonavla. So as you can see we can have a single photo with multiple tags.

Here are some Advantages for this solution,

There's unlimited number of tags.

Disadvantages for this solution,

Slower than Previous solution.

disadvantage is that for one thing we are storing tagged names over and over maybe not ideal to store all these different strings and have duplicated data but it's not a big deal.

Duplication isn't the problem.

But the problem is that this is actually slower than the previous solution when it comes to things like inserting or updating or deleting.

Solution #3

Solution 3 is which involves three tables.

So we have our photos table unchanged. We have a different tag's table this tag table is just a tag name and curresponding ID then we have a table a join table called Photo Tags which is an instance of a hashtag being applied to a photo and all that it is a photo ID and tag ID and that's it, So in this case you know photo ID one is Goldfish is being tagged with cute and then the Nature photo is being tagged with Himalaya.

My Foodgasm photo is being tagged with chicken and Biryani or Chickenbiryani ;) and so on.

Here are some Advantages for this solution,

There's unlimited number of tags.

We can also add additional information(first time/person the hashtag is used).

We also have less duplication of the tags over here.

Disadvantages for this solution,

More work when inserting and deleting.

Tags is not an new trend anymore if you are newbie to instagram or twitter tags or hashtags are something you need to get familiar with.

So from Next Time

That’s it for this article! I hope you enjoyed it..

.

--

--