gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:23 25 4
gpt4 key购买 nike

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

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

最佳答案

你可以创建字典表:一个表按字母分区(以'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)

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

关于c# - 提高网站数据库的搜索性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12130638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com