Fastest way to do a weighted tag search in SQL Server

Posted by Hasan Khan on Stack Overflow See other posts from Stack Overflow or by Hasan Khan
Published on 2010-04-25T12:24:26Z Indexed on 2010/04/25 12:33 UTC
Read the original article Hit count: 222

My table is as follows

ObjectID bigint
Tag nvarchar(50)
Weight float
Type tinyint

I want to get search for all objects that has tags 'big' or 'large' I want the objectid in order of sum of weights (so objects having both the tags will be on top)

select objectid, row_number() over (order by sum(weight) desc) as rowid
from tags 
where tag in ('big', 'large')
      and type=0
group by objectid

the reason for row_number() is that i want paging over results.

The query in its current form is very slow, takes a minute to execute over 16 million tags. What should I do to make it faster?

I have a non clustered index (objectid, tag, type)

Any suggestions?

© Stack Overflow or respective owner

Related posts about sql

Related posts about query-optimization