我在我的项目中使用了实体框架(在.Net中使用MVC)并使用Oracle作为数据库。
在一个表格中包含大量数据(数百万条记录)。 许多专栏都有索引。
当我使用带有多个条件的where子句使用上表从视图访问数据时,然后执行快速。但是我使用实体框架从代码中使用相同的where子句然后需要很长时间(可能是10-15分钟)。
是否存在与实体框架的索引关系?
实体框架中不支持索引?
LINQ生成的查询:
SELECT
CAST( "Extent1"."LOPNR1" AS number(9,0)) AS "C1",
CAST( "Extent1"."LOPNR2" AS number(9,0)) AS "C2",
CAST( "Extent1"."INTFTG" AS number(9,0)) AS "C3",
"Extent1"."LON" AS "LON",
"Extent1"."EZX" AS "EZX",
"Extent1"."FAKTNR" AS "FAKTNR",
...........................
CAST( "Extent1"."UTBETDAT" AS number(9,0)) AS "C11",
CAST( "Extent1"."INBETDAT" AS number(9,0)) AS "C12",
"Extent1"."TRANSTEXT" AS "TRANSTEXT",
"Extent1"."ANVFRITT" AS "ANVFRITT",
CAST( "Extent1"."FLAGGA3" AS number(4,0)) AS "C13",
CAST( "Extent1"."FLAGGA4" AS number(4,0)) AS "C14",
CAST( "Extent1"."FLAGGA5" AS number(4,0)) AS "C15",
CAST( "Extent1"."DATUPPDAT" AS number(9,0)) AS "C16",
..........
"Extent1"."TRI" AS "TRI",
CAST( "Extent1"."OBJIDNR" AS number(18,0)) AS "C17",
CAST( "Extent1"."DOKLOPNR" AS number(18,0)) AS "C18",
.............................
CAST( "Extent1"."FULLDATE" AS number(18,0)) AS "C30",
"Extent1"."RADID" AS "RADID",
"Extent1"."INTFTG_VIEW" AS "INTFTG_VIEW",
CAST( "Extent1"."ISOBJECT" AS number(18,0)) AS "C31"
FROM (SELECT
"ViewName"."LOPNR1" AS "LOPNR1",
"ViewName"."LOPNR2" AS "LOPNR2",
"ViewName"."INTFTG" AS "INTFTG",
.....................................
"ViewName"."BOKFDAG_VIEW" AS "BOKFDAG_VIEW",
"ViewName"."INTFTG_VIEW" AS "INTFTG_VIEW",
"ViewName"."ISOBJECT" AS "ISOBJECT"
FROM "SchemaName"."ViewName" ) "Extent1"
WHERE ((3 <> ( CAST( CAST( "Extent1"."FLAGGA3" AS number(4,0)) AS number(9,0)))) AND (9 <> ( CAST( CAST( "Extent1"."FLAGGA4" AS number(4,0)) AS number(9,0)))) AND ("Extent1"."BOKFBEL" > 0) AND (1 = 1) AND (1 = 1) AND (1 = 1) AND (1 = 1) AND (1 = 1) AND (1 = 1) AND (1 = 1) AND (1 = 1) AND (1 = 1) AND (3636002 = ( CAST( "Extent1"."INTFTG" AS number(9,0)))) AND (90 = ( CAST( CAST( "Extent1"."VTYP" AS number(4,0)) AS number(9,0)))) AND (1 = 1))
答案 0 :(得分:1)
您的代码无法使用任何索引。假设您在Extent1.FLAGGA3上有一个索引。 Oracle查看您的WHERE子句并查看
CAST(Extent1.FLAGGA3, ...) <> 3
如果是
Extent1.FLAGGA3 <> 3
然后可以使用index,但是当列上有函数时,为了使用索引,Oracle需要将该函数应用于索引中的每个键。最有可能的是相当昂贵,Oracle选择不使用该索引。
为什么你需要所有那些CAST?
看起来CAST问题的一般解决方案是使用contains而不是=, cntr-V来自其中一个答案:
short[] shortValue = new short[] { 6 };
var entityList = from r in rep.DataContext.FooTable
--where r.SmallIntColumn == shortValue
where shortValue.Contains(r.SmallIntColumn)
select r;