开发者

SQL Column Search Question (SQL Server 2005)

I have a column varchar(70) in one of my table where I store space separated tags:

Id  Tags 
1   Baby Kids Learning Alphabets 
2   Kids Baby 
3   Comedy Movie Fun
100 Kids Learning Alphabets
500 Kids Baby

I perform search on the column: Get all ids where we have Baby Kids and Alphabets in the tags

I can do where Tags like '%Baby%' or Tags like '%Kids%' or Tags like '%Alphabets %' Select query isslow when there are large # of rows. But add\delete\edit is always very fast.

So I added another table Called Tags where I store the tags alphabetically like:

  Tag        Id
  Alphabets  1
  Alphabets  100
  Baby       1
  Baby       2
  Baby       500
  Comedy     3
  Kids       1

This make the searching faster, buy update\delete\insert painful.

Is my design right for future growth?

How should you design this tags column?

Thanks for reading

EDIT: *All I am trying to pull is the list of related ids. Basically find all ids having the given tags or tag. Like on this question, you see "Related Questions" on the right. That is what I am trying t开发者_JAVA技巧o get.*


You will really have to analyze which aspects of your site will grow and what your goals are. Will there be tons of inserts? Will there be tons of tags? Do you need to answer the question "How many questions have the tag 'Alphabet'?" I hate "it depends" answers, but it really does depend on your goals and expectations.


Here is what I recommend:

Create 2 new tables. One simply stores the name of the tag

  Tag        Id
  Alphabets  1
  Baby       2
  Comedy     3
  Kids       4
  Learning   5
  Alphabets  6

The second one will link the tag to the entry in the first table

  main_id    tag_id
  1          2
  1          4
  1          5
  1          6
  100        4
  100        5
  100        6

Then you can simply search with a join between the tables. It will be a LOT faster. Be sure to include the appropriate indexes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜