I have a table with 5 million records. Table columns are id, customer_id, status, and amount. When I run a query without Index(status),
SELECT SUM(amount) FROM `game_results` WHERE `status` = 1
the query takes 4 seconds to complete, but when I add an index to status column, the query takes 45 seconds to complete. I need Index for the status column, because I also need to make search queries on this table.
How can I solve this issue?
Slow query with index - EXPLAIN
Slow query with index - ANALYZE
Query without index - EXPLAIN
Query without index - ANALYZE
customer_id, game_id, date, prize_type filterable.
amount, point columns for summary stats.
Show Create Table
CREATE TABLE `game_results`(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`customer_id` bigint(20) NOT NULL,
`game_id` bigint(20) NOT NULL,
`amount` decimal(10,2) NOT NULL DEFAULT '0.00',
`prize_id` smallint(6) NOT NULL DEFAULT '0',
`prize_type` smallint(6) NOT NULL DEFAULT '1',
`point` decimal(10,2) NOT NULL DEFAULT '0.00',
`ext_id` bigint(20) DEFAULT NULL,
`ext_value` bigint(20) DEFAULT NULL,
`status` smallint(6) NOT NULL DEFAULT '0',
`site_user` bigint(20) NOT NULL DEFAULT '0',
`date` bigint(20) NOT NULL DEFAULT '0',
`update_date` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10009524
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
EXPLAIN ANALYZE
for both the fast and slow query doesn't hurt too.status
field is 1. If I'm interpreting that correctly, does every row in your table have the same value forstatus
currently?