添加distinct()会导致巨大的性能问题(有时仅限于此)

au9on6nz  于 2021-08-13  发布在  Java
关注(0)|答案(0)|浏览(226)

我使用.NETMVC作为服务器端,实体框架作为orm,ORACLE11G作为数据库
问题是我们的查询需要很长时间才能运行,这种情况并不总是发生,有时只会发生这种情况。如果是正常情况,那么它将在毫秒内完成。但有时需要4-5分钟才能完成。

var alphaList= _dBontext.SampleVIEW
                    .Where(it => it.CODE == code)
                    .Select(it => it.NAME.Substring(0, 1)).Distinct()
                    .ToArray();

由于这个性能问题,我将上面的查询分为两部分,如下所示

var discList = _dBontext.SampleVIEW
                .Where(it => it.CODE == code)
                .Select(it => it.NAME).ToList();

  var alphaList = discList.Select(s => s.Substring(0, 1)).Distinct().ToArray();

这一次我没有看到任何巨大的性能问题。
出于测试目的,我在上面的查询中添加了一个distinct(),如下所示

var discLists =  _dBontext.SampleVIEW
                    .Where(it => it.CODE == code)
                    .Select(it => it.NAME).Distinct().ToList();

这次性能问题又来了。问题是,我无法始终在此查询中找到性能问题。很少只有这种情况发生。然后执行这个查询需要4-5分钟。
我知道使用distinct()将尝试对项目进行排序,与普通查询相比,这种排序可能代价高昂。但我的疑问是,如果这个问题是有关独特的,那么为什么它不总是来??如果问题与distinct()无关,那么还有哪些可能性?
注意:我在这个视图中没有太多数据。现在最多只有5公里的数据。
我已经问过这个问题了。但我的上下文不同,查询性能问题并不总是出现。
更新:
下面是查询的执行计划

"PLAN_TABLE_OUTPUT"
"Plan hash value: 1907157887"
" "
"------------------------------------------------------------------------------------------------------------------"
"| Id  | Operation                 | Name                         | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |"
"------------------------------------------------------------------------------------------------------------------"
"|   0 | SELECT STATEMENT          |                              | 39901 |  3312K|       |  1664   (6)| 00:00:01 |"
"|   1 |  HASH UNIQUE              |                              | 39901 |  3312K|  3640K|  1664   (6)| 00:00:01 |"
"|*  2 |   HASH JOIN RIGHT OUTER   |                              | 39901 |  3312K|       |   857   (8)| 00:00:01 |"
"|   3 |    TABLE ACCESS FULL      | BAL_TBL       |   980 |  9800 |       |    14   (0)| 00:00:01 |"
"|*  4 |    HASH JOIN              |                              |  4072 |   298K|       |   840   (8)| 00:00:01 |"
"|*  5 |     INDEX FAST FULL SCAN  | CVB_USERS_PRI               |  3432 | 58344 |       |     7   (0)| 00:00:01 |"
"|*  6 |     HASH JOIN OUTER       |                              | 11865 |   672K|       |   832   (8)| 00:00:01 |"
"|   7 |      NESTED LOOPS         |                              | 11681 |   501K|       |   272   (9)| 00:00:01 |"
"|   8 |       MERGE JOIN CARTESIAN|                              | 11681 |   387K|       |   262   (6)| 00:00:01 |"
"|   9 |        INDEX FULL SCAN    | TESTV_PROFILE_PRI             |     1 |     9 |       |     1   (0)| 00:00:01 |"
"|  10 |        BUFFER SORT        |                              | 11681 |   285K|       |   261   (6)| 00:00:01 |"
"|  11 |         TABLE ACCESS FULL | PDETAILS_TBL_OTHERS_TAX    | 11681 |   285K|       |   261   (6)| 00:00:01 |"
"|* 12 |       INDEX UNIQUE SCAN   | XYZ_CTL_LIST_OTH_TAX_PR2_IDX |     1 |    10 |       |     0   (0)| 00:00:01 |"
"|* 13 |      TABLE ACCESS FULL    | P_INFO            | 26432 |   361K|       |   557   (7)| 00:00:01 |"
"------------------------------------------------------------------------------------------------------------------"
" "
"Query Block Name / Object Alias (identified by operation id):"
"-------------------------------------------------------------"
" "
"   1 - SEL$3258542E"
"   3 - SEL$3258542E / ACCTBAL@SEL$2"
"   5 - SEL$3258542E / VUSERS@SEL$4"
"   9 - SEL$3258542E / VPRO@SEL$4"
"  11 - SEL$3258542E / A@SEL$3"
"  12 - SEL$3258542E / B@SEL$3"
"  13 - SEL$3258542E / C@SEL$3"
" "
"Predicate Information (identified by operation id):"
"---------------------------------------------------"
" "
"   2 - access(TRIM(""ACCTBAL"".""ID""(+))=TRIM(""A"".""ID""))"
"   4 - access(TRIM(TO_CHAR(""C"".""ID1""))=TRIM(""VUSERS"".""ID1"") AND "
"              TRIM(""VPRO"".""SAMPLECODE"")=TRIM(""VUSERS"".""SAMPLECODE""))"
"   5 - filter(""VUSERS"".""SAMPLECODE""='ABCTESTV1')"
"   6 - access(""A"".""ID""=""C"".""ID""(+))"
"  12 - access(""A"".""ID""=""B"".""ID"")"
"  13 - filter(""C"".""ID""(+) IS NOT NULL)"
" "
"Column Projection Information (identified by operation id):"
"-----------------------------------------------------------"
" "
"   1 - (#keys=1) ""LNAME""||', '||""FNAME""[62]"
"   2 - (#keys=1) ""LNAME""[VARCHAR2,30], ""FNAME""[VARCHAR2,30]"
"   3 - ""ACCTBAL"".""ID""[CHARACTER,9]"
"   4 - (#keys=2) ""A"".""ID""[CHARACTER,9], ""LNAME""[VARCHAR2,30], ""FNAME""[VARCHAR2,30]"
"   5 - ""VUSERS"".""SAMPLECODE""[VARCHAR2,20], ""VUSERS"".""ID1""[VARCHAR2,10]"
"   6 - (#keys=1) ""A"".""ID""[CHARACTER,9], ""VPRO"".""SAMPLECODE""[VARCHAR2,20], ""LNAME""[VARCHAR2,30], "
"       ""FNAME""[VARCHAR2,30], ""C"".""ID1""[NUMBER,22]"
"   7 - (#keys=0) ""VPRO"".""SAMPLECODE""[VARCHAR2,20], ""A"".""ID""[CHARACTER,9], ""LNAME""[VARCHAR2,30], "
"       ""FNAME""[VARCHAR2,30]"
"   8 - (#keys=0) ""VPRO"".""SAMPLECODE""[VARCHAR2,20], ""A"".""ID""[CHARACTER,9], ""LNAME""[VARCHAR2,30], "
"       ""FNAME""[VARCHAR2,30]"
"   9 - ""VPRO"".""SAMPLECODE""[VARCHAR2,20]"
"  10 - (#keys=0) ""A"".""ID""[CHARACTER,9], ""LNAME""[VARCHAR2,30], ""FNAME""[VARCHAR2,30]"
"  11 - (rowset=200) ""A"".""ID""[CHARACTER,9], ""LNAME""[VARCHAR2,30], ""FNAME""[VARCHAR2,30]"
"  13 - ""C"".""ID""[CHARACTER,9], ""C"".""ID1""[NUMBER,22]"
" "
"Note"
"-----"
"   - dynamic statistics used: dynamic sampling (level=2)"
"   - 3 Sql Plan Directives used for this statement"

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题