토드에서 실행 계획을 실행해 보면 인덱스를 타고 있습니다.
EXPLAIN
SELECT ,ACCOUNT_ID,UNIT_PRICE, LINEITEM_USAGETYPE, SUM(USAGE_AMOUNT) AS USAGE_AMOUNT, SUM(COST) AS COST
FROM table_test
WHERE SDATE_MM = '201803'
AND SERVICE_CODE = 'AWSLambda'
GROUP BY UNIT_PRICE, LINEITEM_USAGETYPE
ORDER BY UNIT_PRICE ASC
-----------------------------------------------------------------------------------------------
1 | SIMPLE | aws_billing_dd2 | ref | aws_billing_dd_idx2,aws_billing_dd_idx3 | aws_billing_dd_idx3 | 324 | const,const | 2580 | 100 | Using index condition; Using temporary; Using filesort |
그런데 프로시저에서 in 파라미터를 함께 사용하면 인덱스를 타지 않네요
CREATE PROCEDURE billing.`invoice_create_freeTire_pr2`(in _sdate varchar(6), OUT _result TEXT, OUT _msg TEXT)
BEGIN
DECLARE _no_more_rows1 INTEGER DEFAULT 0;
DECLARE _useDay INTEGER DEFAULT 0;
DECLARE _err INTEGER DEFAULT 0;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
@p2 = MESSAGE_TEXT;
ROLLBACK;
INSERT INTO error_log(reg_date, error_code, error_msg)
VALUES (now(), 'invoice_create_freeTire_pr', @p2);
COMMIT;
SET _result = 'E';
SET _msg = @p2;
END;
EXPLAIN
SELECT 1,ACCOUNT_ID,UNIT_PRICE, LINEITEM_USAGETYPE, SUM(USAGE_AMOUNT) AS USAGE_AMOUNT, SUM(COST) AS COST
FROM aws_billing_dd2
WHERE SDATE_MM = _sdate
AND SERVICE_CODE = 'AWSLambda'
;
SET _result = 'S';
SET _msg = CONCAT(_msg, TIMEDIFF(now(), @dd));
END;
---------------------------------------------------------------
1 | SIMPLE | aws_billing_dd2 | ALL | 741815 | 10 | Using where |
그런데 WHERE SDATE_MM = _sdate 부분에 값을 직접 입력하면 인덱스를 탑니다.
WHERE SDATE_MM = '201803'
설정 문제인지 인덱스 생성의 문제인지...
부탁드립니다.