提高网站数据库的搜索性能

时间:2012-08-26 13:46:45

标签: c# sql asp.net-mvc linq-to-sql

有没有办法改进这个查询,我对返回的结果非常满意,但性能非常差,在这种情况下联合会导致往返吗?通过使用存储过程获得性能,这样的事情是否可能?是否有可能在结果上进行自我加入,这会提高性能吗?

        List<string> words = Util.Search.SplitByWhiteSpace(q);

        using (WebsitesDataContext context = new WebsitesDataContext())
        {
            IQueryable<WebsitesRanked> query = Enumerable.Empty<WebsitesRanked>().AsQueryable();
            query = query.Union(context.Websites.Where(x => x.Title.Contains(q)).Select(x => new WebsitesRanked { Webiste = x, Rank = 100 }));
            query = query.Union(context.Websites.Where(x => x.Description.Contains(q)).Select(x => new WebsitesRanked { Webiste = x, Rank = 100 }));
            query = query.Union(context.Websites.Where(x => x.Keywords.Contains(q)).Select(x => new WebsitesRanked { Webiste = x, Rank = 100 }));
            query = query.Union(context.Websites.Where(x => x.Url.Contains(q)).Select(x => new WebsitesRanked { Webiste = x, Rank = 100 }));

            foreach (var word in words)
            {
                string keyword = word;
                query = query.Union(context.Websites.Where(x => x.Title.Contains(word)).Select(x => new WebsitesRanked { Webiste = x, Rank = 20 }));
                query = query.Union(context.Websites.Where(x => x.Description.Contains(word)).Select(x => new WebsitesRanked { Webiste = x, Rank = 20 }));
                query = query.Union(context.Websites.Where(x => x.Keywords.Contains(word)).Select(x => new WebsitesRanked { Webiste = x, Rank = 20 }));
                query = query.Union(context.Websites.Where(x => x.Url.Contains(word)).Select(x => new WebsitesRanked { Webiste = x, Rank = 20 }));
            }

           var results = query.GroupBy(x => x.Webiste).Select(x => new WebsitesRanked { Webiste = x.First().Webiste, Rank =  x.Sum(s => s.Rank) }).OrderByDescending(x => x.Rank).Select(x => x.Webiste);
        }

1 个答案:

答案 0 :(得分:0)

你可以创建字典表: 一个表分区的字母(表格分区为单词,以'a'开头,另一个'b'......) 此表将包含以下列:

column 1:Word (each word in your table will be setted here using a nightly schedule task or Background schedule task)
column 2:Website 
column 3:Rank (each time a word is found, increment this rank)

只有一个表可以查询并使用分区执行,您将获得良好的性能,因为工作将在计划任务中完成