EXPLAIN SELECT COUNT(*)
FROM BILL_PATIENT_BILL AS fetchInfo
WHERE IF(fetchInfo.BILL_TYPE='OP'
AND fetchInfo.BILL_CATEGORY=0,fetchInfo.DUE_AMOUNT != 0,TRUE)
你如果是沒有SENCE。它所做的是:
- 如果BILL_TYPE = 'OP',
- 和BILL_CATEGORY = 0,
- 然後在WHERE子句時DUE_AMOUNT = 0
- 否則where子句是真實的永遠是真的。
但無論如何,你不需要它。您可以改寫這樣的查詢,使用De Morgan
EXPLAIN SELECT COUNT(*)
FROM BILL_PATIENT_BILL AS fetchInfo
WHERE (fetchInfo.BILL_TYPE='OP'
AND fetchInfo.BILL_CATEGORY=0
AND fetchInfo.DUE_AMOUNT != 0)
OR fetchInfo.BILL_TYPE<>'OP'
OR fetchInfo.BILL_CATEGORY=0
你應該有(BILL_TYPE,BILL_CATEGORY,DUE_AMOUNT)一個指標,但即使這樣,與OR-條款,但仍可能會決定不使用索引。如果是這種情況,請嘗試重寫爲聯合:
SELECT COUNT(*)
FROM BILL_PATIENT_BILL AS fetchInfo
WHERE (fetchInfo.BILL_TYPE='OP'
AND fetchInfo.BILL_CATEGORY=0
AND fetchInfo.DUE_AMOUNT != 0)
UNION ALL
SELECT COUNT(*)
FROM BILL_PATIENT_BILL AS fetchInfo
WHERE fetchInfo.BILL_TYPE<>'OP'
UNION ALL
SELECT COUNT(*)
FROM BILL_PATIENT_BILL AS fetchInfo
WHERE fetchInfo.BILL_CATEGORY=0
您可以執行SUM來添加這些值。
因爲實際上接受列的所有值,也許你甚至不需要where子句...
EXPLAIN SELECT COUNT(
IF(fetchInfo.BILL_TYPE='OP'
AND fetchInfo.BILL_CATEGORY=0
AND fetchInfo.DUE_AMOUNT != 0,
1,
0))
FROM BILL_PATIENT_BILL AS fetchInfo
顯示查詢請。 – hims056
EXPLAIN SELECT COUNT(*)FROM \t BILL_PATIENT_BILL AS fetchInfo WHERE IF(fetchInfo.BILL_TYPE = 'OP' AND fetchInfo.BILL_CATEGORY = 0,fetchInfo.DUE_AMOUNT!= 0,TRUE) –