· 8 years ago · May 02, 2018, 03:04 AM
1/* I want to run this query: */
2
3SELECT SUM( price ) AS total_price, date_month
4FROM daily
5WHERE processed =0
6AND date_month = '200910'
7AND customerid =1
8GROUP BY date_month, customerid
9
10/* On this table */
11
12CREATE TABLE IF NOT EXISTS `daily` (
13`id` int(10) NOT NULL auto_increment,
14`error` varchar(255) NOT NULL,
15`customerid` varchar(10) NOT NULL default '',
16`dateDay` date NOT NULL default '0000-00-00',
17`price` int(10) NOT NULL default '0',
18`ourcalculatedprice` int(10) NOT NULL default '0',
19`processed` int(1) NOT NULL default '0',
20`date_month` int(6) NOT NULL,
21PRIMARY KEY (`id`),
22KEY `customerid` (`customerid`,`date_month`)
23) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
24
25/*
26How can I make it use an index. Right now it is really slow. I added date_month just
27to make the query use this index.
28
29What I need is the SUM(price) for a customer on one month where the amount has
30not already been processed. As soon as the amount is processed it will be set to 1.
31
32How can I make the query use indexes?
33*/