I am working with MySQL 5.6 . I created a table with 366 partitions to store daily funds data. After a year, we had a maximum of 366 days, so I created 366 sections on this table. The hash sections were controlled by an integer column that stores from 1 to 366 for each record.
Report_Summary Table:
CREATE TABLE `Report_Summary` ( `PartitionsID` int(4) unsigned NOT NULL, `ReportTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `Amount` int(10) NOT NULL, UNIQUE KEY `UNIQUE` (`PartitionsID`,`ReportTime`), KEY `PartitionsID` (`PartitionsID`), KEY `ReportTime` (`ReportTime`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED
My current request:
SELECT DATE(RS.ReportTime) AS ReportDate, SUM(RS.Amount) AS Total FROM Report_Summary RS WHERE RS.ReportTime >= '2014-12-26 00:00:00' AND RS.ReportTime <= '2014-12-30 23:59:59' AND RS.PartitionsID BETWEEN DAYOFYEAR('2014-12-26 00:00:00') AND DAYOFYEAR('2014-12-30 23:59:59') GROUP BY ReportDate;
The above query works fine and uses p360 partitions for p364 to get data. Now the problem is that I pass fromDate to '2014-12-26' and toDate to '2015-01-01' . Then the above request will not work. Because Day of the year 2015-01-01 is Year 1 , so my conditions have failed.
Now I tried to pass the value to IN , then it works fine in checking the database below the query:
SELECT DATE(RS.ReportTime) AS ReportDate, SUM(RS.Amount) AS Total FROM Report_Summary RS WHERE RS.ReportTime >= '2014-12-26 00:00:00' AND RS.ReportTime <= '2015-01-01 23:59:59' AND RS.PartitionsID IN (360,361,362,363,364,365,1) GROUP BY ReportDate;
To generate the above script, I created a function and passed two dates and generated a comma-separated string of identifiers
SELECT GenerateRange('2014-12-26 00:00:00', '2015-01-01 23:59:59');
Which returns my data as:
'360,361,362,363,364,365,366,1'
And I tried to use this function in my request, so I changed my request as shown below:
SELECT DATE(RS.ReportTime) AS ReportDate, SUM(RS.Amount) AS Total FROM Report_Summary RS WHERE RS.ReportTime >= '2014-12-26 00:00:00' AND RS.ReportTime <= '2015-01-01 23:59:59' AND FIND_IN_SET(RS.PartitionsID, GenerateRange('2014-12-26 00:00:00', '2015-01-01 00:00:00')) GROUP BY ReportDate;
Then I checked the execution plan of the above query using EXPLAIN PARTITION SELECT .... And I found that my condition would not work. It uses all partitions to retrieve data. I want to use only certain sections of these dates. You only need to check these sections 360,361,362,363,364,365,366,1 : p360 to p366 and p1 .
Why is my request not working? And this is the wrong way to implement this, then I want a solution. How can i achieve this?
I know that from coding I can implement this, but I need to write a request to implement it.
Thanks...