Bug fix and Optimize profile items query with batching and ordering

Prevent SQLite variables limit issue by implementing chunked/batched queries.
This commit is contained in:
2dust
2026-07-02 10:35:17 +08:00
parent ca186e2636
commit f8e1186095
3 changed files with 25 additions and 14 deletions

View File

@@ -92,6 +92,7 @@ public class Global
public const string LinuxBash = "/bin/bash";
public const string StringTrue = "true";
public const string StringFalse = "false";
public const int SqliteMaxBatchSize = 10000;
public const string SingboxDirectDNSTag = "direct_dns";
public const string SingboxRemoteDNSTag = "remote_dns";

View File

@@ -244,9 +244,26 @@ public sealed class AppManager
{
return [];
}
return await SQLiteHelper.Instance.TableAsync<ProfileItem>()
.Where(it => ids.Contains(it.IndexId))
.ToListAsync();
if (ids.Count <= Global.SqliteMaxBatchSize)
{
return await SQLiteHelper.Instance.TableAsync<ProfileItem>()
.Where(it => ids.Contains(it.IndexId))
.ToListAsync();
}
var items = new List<ProfileItem>();
for (var size = 0; size < ids.Count; size += Global.SqliteMaxBatchSize)
{
var chunk = ids.Skip(size).Take(Global.SqliteMaxBatchSize).ToList();
var chunkItems = await SQLiteHelper.Instance.TableAsync<ProfileItem>()
.Where(it => chunk.Contains(it.IndexId))
.ToListAsync();
items.AddRange(chunkItems);
}
return items;
}
public async Task<Dictionary<string, ProfileItem>> GetProfileItemsByIndexIdsAsMap(IEnumerable<string> indexIds)
@@ -257,18 +274,11 @@ public sealed class AppManager
public async Task<List<ProfileItem>> GetProfileItemsOrderedByIndexIds(IEnumerable<string> indexIds)
{
var idList = indexIds.Where(id => !id.IsNullOrEmpty()).Distinct().ToList();
if (idList.Count == 0)
{
return [];
}
var items = await SQLiteHelper.Instance.TableAsync<ProfileItem>()
.Where(it => idList.Contains(it.IndexId))
.ToListAsync();
var ids = indexIds.Where(id => !id.IsNullOrEmpty()).Distinct().ToList();
var items = await GetProfileItemsByIndexIds(ids);
var itemMap = items.ToDictionary(it => it.IndexId);
return idList.Select(itemMap.GetValueOrDefault)
return ids.Select(itemMap.GetValueOrDefault)
.Where(item => item != null)
.ToList();
}

View File

@@ -3862,7 +3862,7 @@ namespace ServiceLib.Resx {
}
/// <summary>
/// 查找类似 For multi-interface environments, enter the name of the interface to bind. Only effective on Windows systems or TUN mode 的本地化字符串。
/// 查找类似 For multi-interface environments, enter the interface name for outbound connections. On Linux/macOS, it only works when TUN mode is enabled. 的本地化字符串。
/// </summary>
public static string TbSettingsBindInterfaceTip {
get {