[英]EF SQL query Performance on comma-separated string Ids
To store and query on associated IDs, which method works best in terms of query speed and performance?要存储和查询关联的 ID,哪种方法在查询速度和性能方面效果最好? Especially in a large number of records in primary table, such as 100,000 records.
尤其是在主表的大量记录中,比如10万条记录。
1- Use the comma-separated string field as Ids: 1- 使用逗号分隔的字符串字段作为 ID:
query.Where(x => (',' + x.Ids + ',').Contains(',' + value + ','));
2- Use a separate table and join operation: 2-使用单独的表和连接操作:
query.Where(x => x.SecondaryTable.Any(s => s.Id == value));
The first option performs a string comparison on a dynamically-generated string, so there is no possibility of an index improving query performance.第一个选项对动态生成的字符串执行字符串比较,因此索引不可能提高查询性能。 It will be exceptionally slow.
它会异常缓慢。
The second option is performing a numeric comparison on an indexed primary key (assuming that Id is some numeric type and that it is the primary key).第二个选项是对索引主键执行数字比较(假设 Id 是某种数字类型并且它是主键)。 This is a much faster comparison for your processor to evaluate, and it is a simple comparison without generating dynamic data for said comparison.
对于您的处理器来说,这是一个更快的比较来评估,而且它是一个简单的比较,无需为所述比较生成动态数据。
If you store Ids as comma separated string - you always have TABLE/INDEX scan.如果您将 ID 存储为逗号分隔的字符串 - 您总是有 TABLE/INDEX 扫描。 If your table is small it can be enough.
如果你的桌子很小,那就足够了。
With SecondaryTable
table which stores Ids associated with main table there a lot of other plans:使用存储与主表关联的 Id 的
SecondaryTable
表,还有许多其他计划:
query = query.Where(x => x.SecondaryTable.Any(s => s.Id == value));
var query =
from m in query
from s in m.SecondaryTable.Where(s => s.Id == value)
select s;
var secondary = db.SecondaryTable.Where(s => s.Id == value);
var mainIds = secondary.Select(s => new { s.MainId }).Distinct();
query =
from m in query
from s in mainIds.Where(s => s.MainId == m.Id)
select m;
Anyway, better to test and check execution plan.无论如何,最好测试和检查执行计划。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.