Oracle SQL奇怪的问题与我的下面的查询

时间:2013-12-11 06:32:45

标签: sql oracle

我有一个名为RULE_READOUT_VIEW的视图名称,其varchar列名为'READOUTTYPE'和'CUSTOMERNUMBER'。

当我运行以下计数查询时,我只得到一行作为输出。

SELECT count(1) FROM RULE_READOUT_VIEW
WHERE READOUTTYPE = '5' AND CUSTOMERNUMBER = 'NSMPF';

但是如果运行这个其他查询,我会得到一个不同的结果,这个时间计数是11行,这就是预期的结果。

SELECT count(1) FROM RULE_READOUT_VIEW
WHERE to_char(READOUTTYPE) = '5' AND CUSTOMERNUMBER = 'NSMPF';

我不确定为什么第一个查询无法正常工作,因为只有在列上强制使用to_char才能获得正确的答案。我搜索了答案,但找不到任何令人满意的东西。有人可以解释一下这种行为的原因吗?

我运行以下查询来检查每一行的长度

SELECT READOUTTYPE, TO_CHAR(READOUTTYPE), LENGTH(READOUTTYPE), LENGTHB(READOUTTYPE) FROM teoss.RULE_READOUT_VIEW WHERE to_char(READOUTTYPE) = '5' and CUSTOMERNUMBER = 'NSMPF';

我输出了11行,所有列的长度都为1。

5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1
5 5 1 1

因此列也没有任何附加空格。

查看DDL:

SELECT --ro.creation_date,
ro.POINT_OF_TIME_DAY                      AS pointOfReadoutDay,
ro.POINT_OF_TIME_HOUR                     AS pointOfReadoutHour,
ro.POINT_OF_TIME_MINUTE                   AS pointOfReadoutMinute,
rot.NAME                                  AS readoutType,
cp.IDENTIFICATION                         AS customerNumber,
mp.METER_POINT_NUMBER                     AS meterPointNumber,
ro.ORDER_REFERENCE                        AS readoutOrderNumber,
mtrefstat.NAME                            AS meterStatus,
mt.ADDRESS                                AS address,
mt.SERIAL_NUMBER                          AS serialNumber,
mt.UPPER_SERIAL_NUMBER                    AS upperSerialNumber,
servrefstat.NAME                          AS serverStatus,
mus.MAC                                   AS hostname,
mserv.IP_ADDRESS                          AS ipAddress ,
mt.device_id                              AS deviceID,
GET_ACCESSCONNECTION_TYPE(mus.device_id)  AS ACCESSCONNECTIONTYPE,
FUNC_LIST_OBIS_NAME(ro.order_read_out_id) AS refObisList,
mt.METER_NUMBER                           AS meterNumber
FROM ORDER_READ_OUT ro,
REF_METER_READ_OUT_TYPE rot,
COMPANY cp,
METER_POINT mp,
METER mt,
MAP_DEVICE_STATUS mtstat,
REF_STATUS mtrefstat,
DEVICE servdev,
MUS mus,
METERING_SERVER mserv,
MAP_DEVICE_STATUS servstat,
REF_STATUS servrefstat,
MAP_ORD_RD_OUT_CNTRCT_MT_PT mocm,
MAP_CONTRACT_METER_POINT mcm,
CONTRACT co
WHERE -- order readout relation has to be valid
mocm .ORDER_READ_OUT_ID         = ro.ORDER_READ_OUT_ID
AND ro.REF_METER_READ_OUT_TYPE_ID = rot.REF_METER_READ_OUT_TYPE_ID
AND ro.ACTIVATION_DATE           <= SYSTIMESTAMP
AND (ro.DEACTIVATION_DATE        IS NULL
OR ro.DEACTIVATION_DATE           > SYSTIMESTAMP)
-- contract relation has to be valid ...
AND mocm.MAP_CONTRACT_METER_POINT_ID = mcm.MAP_CONTRACT_METER_POINT_ID
AND mcm.CONTRACT_ID                  = co.CONTRACT_ID
AND co.COMPANY_ID                    = cp.COMPANY_ID
AND ((co.BEGIN                      IS NULL
AND mcm.BEGIN                       <= SYSTIMESTAMP)
OR (co.BEGIN                        <= SYSTIMESTAMP
AND mcm.BEGIN                       <= SYSTIMESTAMP
AND mcm.begin                       >= co.begin))
AND (mcm.end                        IS NULL
OR mcm.end                          >= SYSTIMESTAMP)
AND ((co.END                        IS NULL)
OR (co.END                          >= SYSTIMESTAMP
AND ((mcm.end                       IS NULL)
OR (mcm.end                         <= co.end))))
-- meter point relation has to be valid ...
AND mcm.METER_POINT_ID   = mp.METER_POINT_ID
AND mp.METER_POINT_ID    = mt.METER_POINT_ID
AND mt.DEVICE_ID         = mtstat.DEVICE_ID
AND mtstat.REF_STATUS_ID = mtrefstat.REF_STATUS_ID
AND (mtrefstat.name      = 'active'
OR mtrefstat.name        = 'toBeRemoved')
AND mtstat.STATUS_DATE   =
(SELECT MAX(mds.status_date)
FROM Map_device_status mds
WHERE mds.device_id = mtstat.DEVICE_ID
)
-- mus/metering-server relation has to be valid
AND servdev.DEVICE_ID =
(SELECT FUNC_FIND_SERVER_DEV_ID(mt.DEVICE_ID) FROM DUAL
)
AND servdev.DEVICE_ID      = mus.DEVICE_ID(+)
AND servdev.DEVICE_ID      = mserv.DEVICE_ID(+)
AND servdev.DEVICE_ID      = servstat.DEVICE_ID
AND servstat.REF_STATUS_ID = servrefstat.REF_STATUS_ID
AND (servrefstat.name      = 'active'
OR servrefstat.name        = 'toBeRemoved')
AND servstat.STATUS_DATE   =
(SELECT MAX(mds.status_date)
FROM Map_device_status mds
WHERE mds.device_id = servstat.DEVICE_ID
)
AND ro.CREATION_DATE =
(SELECT MAX(t9.CREATION_DATE)
FROM ORDER_READ_OUT t9
WHERE t9.point_of_time_day        = ro.point_of_time_day
AND t9.point_of_time_hour         = ro.point_of_time_hour
AND t9.point_of_time_minute       = ro.point_of_time_minute
AND t9.ref_meter_read_out_type_id = ro.ref_meter_read_out_type_id
AND t9.ORDER_READ_OUT_ID         IN
(SELECT t5.ORDER_READ_OUT_ID
FROM map_ord_rd_out_cntrct_mt_pt t5
WHERE t5.MAP_CONTRACT_METER_POINT_ID = mcm.MAP_CONTRACT_METER_POINT_ID
));

计划摘要: explain plan for SELECT count(1) FROM RULE_READOUT_VIEW WHERE READOUTTYPE = '5' AND CUSTOMERNUMBER = 'NSMPF';

| 19 |按INDEX ROWID表的访问权限| REF_METER_READ_OUT_TYPE | 1 | 7 | 1 |
| 20 | INDEX UNIQUE SCAN | UQ_REF_METER_READ_OUT_TY_NAME | 1 | | 0

    explain plan for SELECT count(1) FROM RULE_READOUT_VIEW WHERE to_char(READOUTTYPE) = '5' AND CUSTOMERNUMBER = 'NSMPF';

| 14 | INDEX FAST FULL SCAN | PK_METERREADTYPE | 1 | 7 | 1 |
| 15 | INDEX FAST FULL SCAN | UQ_REF_METER_READ_OUT_TY_NAME | 1 | 7 | 1 |

2 个答案:

答案 0 :(得分:1)

创意1:

当您运行此查询时,您应该返回有问题的行: -

SELECT READOUTTYPE, TO_CHAR(READOUTTYPE), LENGTH(READOUTTYPE) 
FROM RULE_READOUT_VIEW
WHERE to_char( READOUTTPYE ) = '5'
AND READOUTTYPE != '5'
AND CUSTOMERNUMBER = 'NSMPF';

或许可以使用此查询的结果更新您的问题,看是否能让所有人都清楚。

创意2:

试试这个:

CREATE TABLE TEMP_RULE_READOUT AS 
SELECT * FROM RULE_READOUT_VIEW
WHERE CUSTOMERNUMBER = 'NSMPF';

SELECT count(1) FROM TEMP_RULE_READOUT
WHERE READOUTTYPE = '5' AND CUSTOMERNUMBER = 'NSMPF';

SELECT count(1) FROM RULE_READOUT_VIEW
WHERE to_char(READOUTTYPE) = '5' AND CUSTOMERNUMBER = 'NSMPF';

当数据被实现到表格中时,看到这是否正常将会很有趣。

答案 1 :(得分:1)

我怀疑您在REF_METER_READ_OUT_TYPE.NAME上可能有一个损坏的索引,并且查询结果之间的差异可以通过是否使用索引来解释。

尝试删除并重新创建索引(不重建,因为这可能会保留任何损坏)。