2

有什么办法可以改进这个查询,我对返回的结果很满意,但性能真的很差,在这种情况下联合会导致往返吗?通过使用存储过程来获得性能,这样的事情是否可能实现?是否可以在结果上做一些类似自我加入的事情,它会提高性能吗?

        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);
        }
4

1 回答 1

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)

只需查询一个表并使用分区来提高性能,您将获得良好的性能,因为工作将在计划任务中为您完成

于 2012-08-26T14:19:10.550 回答