I got to a point were I can not understand why the following MySQL query gets slower when I use an index in my where clause. The column that makes me crazy is called deleted. The table contains 4.8M rows.
The Query:
SELECT SQL_NO_CACHE SUM(amount)/100 FROM transactions WHERE (type="Payment" or type="Refund") and deleted is NULL
That query takes slightly above 11 seconds when the column is an Index and 3 seconds when its not indexed or when I use USE INDEX()
which tell the optimizer not to use any index.
MySQL version 5.6, tested in AWS Aurora db.r5.xlarge (4CPU/32GB)
Table Structure:
id int(11) NOT NULL,
type enum('Charge','Payment','Refund','Credit Adjustment','Debit Adjustment','Transfer') NOT NULL,
amount int(11) NOT NULL,
deleted datetime DEFAULT NULL,
deleted_by int(11) DEFAULT NULL
ENGINE=InnoDB DEFAULT CHARSET=utf8;
ADD KEY type (type),
ADD KEY deleted (deleted)
I would appreciate any clues here!