简体   繁体   English

如果在 Oracle 中传递参数,实体框架查询会变慢

[英]Entity Framework query slow if parameters are passed in Oracle

Here is my query which take about ~15 seconds to execute.这是我的查询,执行大约需要 15 秒。 The table has an index on REQID该表在REQID上有一个索引

var tls = (from tradelines in _context.TRADELINEs
    where tradelines.REQID == requestId 
    select tradelines).ToList();

Model:模型:

[StringLength(36)]
[Column(TypeName = "char")]
public string REQID { get; set; }

Oracle Db Type for REQID is CHAR(36 BYTE) REQID Oracle 数据库类型为CHAR(36 BYTE)

If I execute the same query with hard coded string it return the data in ~1 sec.如果我使用硬编码字符串执行相同的查询,它会在 ~1 秒内返回数据。

var tls = (from tradelines in _context.TRADELINEs
    where tradelines.REQID == "2435d403-0beb-416f-a536-0df4e2fcc03f"
    select tradelines).ToList();

I have already tried this approach:我已经尝试过这种方法:

EntityFunctions.AsNonUnicode(requestId)

Still no luck.仍然没有运气。 Any idea what I am missing here?知道我在这里缺少什么吗?

Doing this :这样做:

[StringLength(36)]
[Column(TypeName = "varchar")]
public string REQID { get; set; }

throws an error saying抛出一个错误说

The store type 'varchar' could not be found in the OracleEFProvider provider manifest在 OracleEFProvider 提供程序清单中找不到存储类型“varchar”

Also, there is no change in execution time if I add此外,如果我添加,执行时间没有变化

[Column(TypeName = "varchar2")]

EF Log Query: EF 日志查询:

SELECT
    // Column List
FROM "TRADELINE" "Extent1"
WHERE (("Extent1"."REQID" = :p__linq__0) OR (("Extent1"."REQID" IS NULL) AND (:p__linq__0 IS NULL)))

-- p__linq__0: '2435d403-0beb-416f-a536-0df4e2fcc03f' (Type = Object)

I am wondering why it is generating (Type = Object) for string parameter?我想知道为什么它为string参数生成(Type = Object)

Now, this is interesting:现在,这很有趣:

There is another column in same table with following structure,同一个表中还有另一列具有以下结构,

[StringLength(36)]
[Column(TypeName = "char")]
public string ID { get; set; }

This column is having primary key with the datatype CHAR(36 BYTE) .此列具有数据类型为CHAR(36 BYTE)主键。 If I apply the where condition on this column and specify parameter value, it take only few milliseconds to execute.如果我在此列上应用 where 条件并指定参数值,则只需几毫秒即可执行。 The only difference between ID and REQID is REQID is allow null. IDREQID之间的唯一区别是REQID允许为空。

Taken from this answer ...取自这个答案......

public class MyContext : DbContext
{
    public MyContext()
    {
        this.Configuration.UseDatabaseNullSemantics = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM