我目前使用的是大盟数据库8版本。在SQL查询语句中,我正在处理一个非常大的表。我在开发环境中对该表创建了相同的唯一索引,但是当我在开发环境中测试SQL查询语句时,它们可以正常使用该索引。但是,当我使用EXPLAIN关键字查看SQL语句的执行计划时,我发现,当我在生产环境中运行相同的SQL查询语句时,它们无法使用索引。
开发环境和生产环境的数据库版本都是V8,现在不知道该尝试什么。希望同一条SQL语句在生产环境和开发环境能够得到相同的输出。
WITH FilteredData AS (
SELECT /*+ INDEX(T0403_DAY_DATA,T0403_DAY_DATA_IND) */
c0007_point_id,
c0403_flag,
c0403_date,
c0403_value
FROM T0403_DAY_DATA
WHERE c0403_date BETWEEN 1722441600 AND 1723046399
AND c0403_flag IN (200, 201, 202)
),
BaseData AS (
SELECT
c0007_point_id,
c0403_date,
MAX(CASE WHEN c0403_flag = 200 THEN c0403_value END) AS maxV,
TO_CHAR(DATE '1970-01-01' + (MAX(CASE WHEN c0403_flag = 200 THEN c0403_date END) / 86400), 'YYYY-MM-DD HH24:MI:SS') AS maxTime,
MAX(CASE WHEN c0403_flag = 201 THEN c0403_value END) AS minV,
TO_CHAR(DATE '1970-01-01' + (MAX(CASE WHEN c0403_flag = 201 THEN c0403_date END) / 86400), 'YYYY-MM-DD HH24:MI:SS') AS minTime,
MAX(CASE WHEN c0403_flag = 202 THEN c0403_value END) AS avgV
FROM FilteredData
GROUP BY c0007_point_id, c0403_date
),
FailRateData AS (
SELECT
c0007_point_id,
ROUND(
CAST(
COUNT(CASE WHEN c0403_flag = 202 AND (c0403_value > 10.7 OR c0403_value < 10) THEN 1 END) * 100.0 /
NULLIF(COUNT(CASE WHEN c0403_flag = 202 THEN 1 END), 0) AS NUMERIC
), 4
) || '%' AS failRate
FROM FilteredData
WHERE c0403_flag = 202
GROUP BY c0007_point_id
),
BaseInfo AS (
SELECT
b.c0007_point_id,
c.c1211_devid AS busId,
c.c1211_devdesc AS busDesc,
e.description AS stationDesc
FROM T0007_POINT b
JOIN T1211_DEVICE c ON b.c1211_devid = c.c1211_devid
JOIN T0015_VOLTAGELEVEL d ON c.c0015_voltagelevelid = d.c0015_voltagelevelid
JOIN (
SELECT DISTINCT
t.substation AS suri,
z.subctrlarea AS area,
z.uri AS zone,
a.description AS description
FROM XOPENS.CIM_FEEDER t
JOIN XOPENS.CIM_SUBSTATION a ON t.substation = a.uri
JOIN XOPENS.CIM_ZONE z ON t.zone = z.uri
) e ON d.c0003_station_no = TO_NUMBER(SUBSTR(e.suri, INSTR(e.suri, '-') + 1))
WHERE b.c1220_meastypeid = 206
AND c.c1216_devtype = 'BS'
AND d.c0015_voltagelevel = '10kV'
)
SELECT
n.stationDesc,
n.busDesc,
n.busId,
b.maxV AS maxHis,
b.maxTime AS maxTimeHis,
b.minV AS minHis,
b.minTime AS minTimeHis,
b.avgV AS avgHis,
f.failRate AS volFailRateHis
FROM BaseInfo n
JOIN BaseData b ON n.c0007_point_id = b.c0007_point_id
JOIN FailRateData f ON b.c0007_point_id = f.c0007_point_id
ORDER BY n.stationDesc, n.busDesc;
生产环境执行计划如下
1 #NSET2: [3520786, 22668, 882]
2 #PRJT2: [3520786, 22668, 882]; exp_num(9), is_atom(FALSE)
3 #SORT3: [3520786, 22668, 882]; key_num(2), partition_key_num(0), is_distinct(FALSE), top_flag(0), is_adaptive(0)
4 #HASH2 INNER JOIN: [3520779, 22668, 882]; KEY_NUM(2); KEY(F.C0007_POINT_ID=B.C0007_POINT_ID AND N.C0007_POINT_ID=B.C0007_POINT_ID) KEY_NULL_EQU(0, 0)
5 #SLCT2: [1255372, 15112, 762]; F.C0007_POINT_ID = N.C0007_POINT_ID
6 #NEST LOOP INNER JOIN2: [1255372, 15112, 762]; [with var]
7 #PRJT2: [43, 17778, 642]; exp_num(4), is_atom(FALSE)
8 #HASH2 INNER JOIN: [43, 17778, 642]; KEY_NUM(1); KEY(D.C0003_STATION_NO=exp11) KEY_NULL_EQU(0)
9 #HASH2 INNER JOIN: [22, 1670, 354]; LKEY_UNIQUE KEY_NUM(1); KEY(C.C1211_DEVID=B.C1211_DEVID) KEY_NULL_EQU(0)
10 #HASH2 INNER JOIN: [19, 1848, 264]; LKEY_UNIQUE KEY_NUM(1); KEY(D.C0015_VOLTAGELEVELID=C.C0015_VOLTAGELEVELID) KEY_NULL_EQU(0)
11 #SLCT2: [1, 253, 108]; D.C0015_VOLTAGELEVEL = '10kV'
12 #CSCN2: [1, 1689, 108]; INDEX34393935(T0015_VOLTAGELEVEL as D); btr_scan(1)
13 #BLKUP2: [15, 13381, 156]; T1211_DEVICE_I3(C)
14 #SSEK2: [15, 13381, 156]; scan_type(ASC), T1211_DEVICE_I3(T1211_DEVICE as C), scan_range['BS','BS']
15 #SSEK2: [1, 9640, 90]; scan_type(ASC), IDX_T0007_MEASTYPEID_ID(T0007_POINT as B), scan_range[(exp_cast(206),min,min),(exp_cast(206),max,max))
16 #PRJT2: [19, 3054, 288]; exp_num(2), is_atom(FALSE)
17 #DISTINCT: [19, 3054, 288]
18 #HASH2 INNER JOIN: [18, 3054, 288]; KEY_NUM(1); KEY(T.SUBSTATION=A.URI) KEY_NULL_EQU(0)
19 #HASH2 INNER JOIN: [1, 2946, 192]; KEY_NUM(1); KEY(Z.URI=T.ZONE) KEY_NULL_EQU(0)
20 #PARALLEL: [1, 164, 96]; scan_type(FULL), key_num(0, 0, 0), simple(0)
21 #CSCN2: [1, 164, 96]; INDEX33714966(CIM_ZONE as Z); btr_scan(1)
22 #PARALLEL: [1, 2870, 96]; scan_type(FULL), key_num(0, 0, 0), simple(0)
23 #CSCN2: [1, 2870, 96]; INDEX33714976(CIM_FEEDER as T); btr_scan(1)
24 #PARALLEL: [9, 76610, 96]; scan_type(FULL), key_num(0, 0, 0), simple(0)
25 #CSCN2: [9, 76610, 96]; INDEX33714764(CIM_SUBSTATION as A); btr_scan(1)
26 #PRJT2: [64, 1419, 120]; exp_num(2), is_atom(FALSE)
27 #SAGR2: [64, 1419, 120]; grp_num(1), sfun_num(2), distinct_flag[0,0]; slave_empty(0) keys(DMTEMPVIEW_901191724.TMPCOL0)
28 #PRJT2: [53, 141990, 120]; exp_num(3), is_atom(FALSE)
29 #PRJT2: [53, 141990, 120]; exp_num(3), is_atom(FALSE)
30 #PARALLEL: [53, 141990, 120]; scan_type(GE_LE), key_num(0, 1, 1), simple(0)
31 #BLKUP2: [53, 47330, 120]; T0403_DAY_DATA_IND(T0403_DAY_DATA)
32 #SSEK2: [53, 47330, 120]; scan_type(ASC), T0403_DAY_DATA_IND(T0403_DAY_DATA), scan_range[(var3,exp_cast(202),exp_cast(1722441600)),(var3,exp_cast(202),exp_cast(1723046399))]
33 #PRJT2: [2265388, 170388, 120]; exp_num(6), is_atom(FALSE)
34 #HAGR2: [2265388, 170388, 120]; grp_num(2), sfun_num(5), distinct_flag[0,0,0,0,0]; slave_empty(0) keys(DMTEMPVIEW_901191723.TMPCOL0, DMTEMPVIEW_901191723.TMPCOL1)
35 #PRJT2: [2261787, 17038818, 120]; exp_num(7), is_atom(FALSE)
36 #PRJT2: [2261787, 17038818, 120]; exp_num(4), is_atom(FALSE)
37 #PARALLEL: [2261787, 17038818, 120]; scan_type(GE_LE), key_num(0, 1, 1), simple(0)
38 #HASH RIGHT SEMI JOIN2: [2261787, 17038818, 120]; n_keys(1) KEY(DMTEMPVIEW_901191759.colname=T0403_DAY_DATA.C0403_FLAG) KEY_NULL_EQU(0)
39 #CONST VALUE LIST: [1, 3, 30]; row_num(3), col_num(1)
40 #SLCT2: [2261787, 2019415547, 120]; [(T0403_DAY_DATA.C0403_DATE >= var15 AND T0403_DAY_DATA.C0403_DATE <= var16)]
41 #BLKUP2: [6785363, 6058246641, 120]; T0403_DAY_DATA_IND(T0403_DAY_DATA)
42 #SSCN: [6785363, 6058246641, 120]; T0403_DAY_DATA_IND(T0403_DAY_DATA); btr_scan(1)
开发环境执行计划如下
1 #NSET2: [25, 3, 882]
2 #PRJT2: [25, 3, 882]; exp_num(9), is_atom(FALSE)
3 #SORT3: [25, 3, 882]; key_num(2), is_distinct(FALSE), top_flag(0), is_adaptive(0)
4 #SLCT2: [25, 3, 882]; (B.C0007_POINT_ID = F.C0007_POINT_ID AND N.C0007_POINT_ID = B.C0007_POINT_ID)
5 #NEST LOOP INNER JOIN2: [25, 3, 882]; [with var]
6 #SLCT2: [17, 1, 762]; F.C0007_POINT_ID = N.C0007_POINT_ID
7 #NEST LOOP INNER JOIN2: [17, 1, 762]; [with var]
8 #PRJT2: [12, 1, 642]; exp_num(4), is_atom(FALSE)
9 #SLCT2: [12, 1, 642]; D.C0003_STATION_NO = exp11
10 #NEST LOOP INNER JOIN2: [12, 1, 642]; [with var]
11 #HASH2 INNER JOIN: [4, 1, 354]; KEY_NUM(1); KEY((C.C1211_DEVID=B.C1211_DEVID))
12 #SLCT2: [0, 14, 264]; C.C1216_DEVTYPE = 'BS'
13 #HASH2 INNER JOIN: [0, 14, 264]; LKEY_UNIQUE KEY_NUM(1); KEY((D.C0015_VOLTAGELEVELID=C.C0015_VOLTAGELEVELID))
14 #SLCT2: [0, 14, 264]; C.C1216_DEVTYPE = 'BS'
15 #NEST LOOP INDEX JOIN2: [0, 14, 264]
16 #ACTRL: [0, 14, 264];
17 #SLCT2: [0, 7, 108]; D.C0015_VOLTAGELEVEL = '10kV'
18 #CSCN2: [0, 299, 108]; INDEX33556139(T0015_VOLTAGELEVEL as D)
19 #BLKUP2: [0, 2, 0]; C1211_DEVTYPE(C)
20 #SSEK2: [0, 2, 0]; scan_type(ASC), C1211_DEVTYPE(T1211_DEVICE as C), scan_range[D.C0015_VOLTAGELEVELID,D.C0015_VOLTAGELEVELID]
21 #CSCN2: [35, 256500, 156]; INDEX33556635(T1211_DEVICE as C)
22 #SSEK2: [1, 10721, 90]; scan_type(ASC), INDEX_T0007_MEASTYPEID_ID(T0007_POINT as B), scan_range[(exp_cast(206),min,min),(exp_cast(206),max,max))
23 #PRJT2: [4, 1, 288]; exp_num(2), is_atom(FALSE)
24 #DISTINCT: [4, 1, 288]
25 #HASH2 INNER JOIN: [3, 59, 288]; KEY_NUM(1); KEY((T.ZONE=Z.URI))
26 #HASH2 INNER JOIN: [2, 54, 192]; KEY_NUM(1); KEY((T.SUBSTATION=A.URI))
27 #NEST LOOP INDEX JOIN2: [2, 54, 192]
28 #ACTRL: [2, 54, 192];
29 #PARALLEL: [0, 27, 96]; scan_type(FULL), key_num(0, 0, 0)
30 #SLCT2: [0, 27, 96]; var9 = exp11
31 #CSCN2: [0, 1094, 96]; INDEX33558033(CIM_FEEDER as T)
32 #PARALLEL: [1, 2, 0]; scan_type(FULL), key_num(0, 0, 0)
33 #CSEK2: [1, 2, 0]; scan_type(ASC), INDEX33557604(CIM_SUBSTATION as A), scan_range[(T.SUBSTATION,min),(T.SUBSTATION,max))
34 #PARALLEL: [5, 43432, 96]; scan_type(FULL), key_num(0, 0, 0)
35 #CSCN2: [5, 43432, 96]; INDEX33557604(CIM_SUBSTATION as A)
36 #PARALLEL: [0, 110, 96]; scan_type(FULL), key_num(0, 0, 0)
37 #CSCN2: [0, 110, 96]; INDEX33558019(CIM_ZONE as Z)
38 #PRJT2: [1, 1, 120]; exp_num(2), is_atom(FALSE)
39 #SAGR2: [1, 1, 120]; grp_num(1), sfun_num(2) slave_empty(0) keys(DMTEMPVIEW_20860601.TMPCOL0)
40 #SORT3: [1, 1, 120]; key_num(1), is_distinct(FALSE), top_flag(0), is_adaptive(1)
41 #PRJT2: [0, 10, 120]; exp_num(3), is_atom(FALSE)
42 #PRJT2: [0, 10, 120]; exp_num(2), is_atom(FALSE)
43 #PARALLEL: [0, 10, 120]; scan_type(GE_LE), key_num(0, 1, 1)
44 #HASH RIGHT SEMI JOIN2: [0, 10, 120]; n_keys(1) KEY((DMTEMPVIEW_20860635.colname=exp_cast(202)))
45 #CONST VALUE LIST: [0, 3, 30]; row_num(3), col_num(1),
46 #BLKUP2: [2, 133, 120]; T0403_DAY_DATA_IND(T0403_DAY_DATA)
47 #SSEK2: [2, 133, 120]; scan_type(ASC), T0403_DAY_DATA_IND(T0403_DAY_DATA), scan_range[(var8,exp_cast(202),exp_cast(1722441600)),(var8,exp_cast(202),exp_cast(1723046399))]
48 #PRJT2: [3, 1, 120]; exp_num(6), is_atom(FALSE)
49 #HAGR2: [3, 1, 120]; grp_num(2), sfun_num(5); slave_empty(0) keys(DMTEMPVIEW_20860600.TMPCOL0, DMTEMPVIEW_20860600.TMPCOL1)
50 #PRJT2: [2, 10, 120]; exp_num(7), is_atom(FALSE)
51 #PRJT2: [2, 10, 120]; exp_num(4), is_atom(FALSE)
52 #SLCT2: [2, 10, 120]; T0403_DAY_DATA.C0007_POINT_ID = var7
53 #NEST LOOP INDEX JOIN2: [2, 10, 120]
54 #CONST VALUE LIST: [0, 3, 0]; row_num(3), col_num(1),
55 #PARALLEL: [2, 44, 120]; scan_type(GE_LE), key_num(0, 1, 1)
56 #BLKUP2: [8, 133, 120]; T0403_DAY_DATA_IND(T0403_DAY_DATA)
57 #SSEK2: [8, 133, 120]; scan_type(ASC), T0403_DAY_DATA_IND(T0403_DAY_DATA), scan_range[(var6,DMTEMPVIEW_20860617.colname,exp_cast(1722441600)),(var6,DMTEMPVIEW_20860617.colname,exp_cast(1723046399))]
您面临的问题是,即使数据库版本和索引相同,达梦数据库优化器也会在您的开发和生产环境中为同一查询选择不同的执行计划。以下是具体情况和一些故障排除步骤:
问题:开发环境和生产环境执行计划不同
可能原因:
统计信息:查询中涉及的表的过时或不准确的统计信息可能会导致优化器在使用哪些索引方面做出错误的决定。 数据分布:如果开发环境和生产环境之间的数据分布明显不同,优化器可能会选择不同的访问方法。 其他配置设置:影响优化器行为的环境之间的配置设置可能存在细微差异。
您面临的问题是,即使数据库版本和索引相同,达盟数据库优化器也会在您的开发和生产环境中为同一查询选择不同的执行计划。以下是具体情况和一些故障排除步骤:
问题:开发环境和生产环境执行计划不同
**
**
统计信息:查询中涉及的表的过时或不准确的统计信息可能会导致优化器在使用哪些索引方面做出错误的决定。 数据分布:如果开发环境和生产环境之间的数据分布存在显着差异,优化器可能会选择不同的访问方法。 其他配置设置: 环境之间的配置设置可能存在细微差异,从而影响优化器的行为。
**
**
1。查看统计数据:
使用带有 FOR STATISTICS 选项的 EXPLAIN 命令来查看优化器使用的统计信息。
如果统计数据看起来已经过时(例如,不反映当前的数据分布),请使用 ANALYZE 或类似命令收集新的统计数据。
2。分析数据分布:
检查两个环境中的数据量和价值分布是否相似。显着差异可能会影响优化器的选择。
3.查看配置差异:
比较两种环境中与优化器和索引相关的配置设置。寻找可能影响行为的细微差异。
4.强制索引使用(可选):
作为最后的手段,您可以尝试通过 SQL 语句中的提示强制使用所需的索引。这并不理想,因为它绕过了优化器,但它对于测试目的很有帮助。