I know that using index could optimize SQL with GROUP BY or WHERE clause. But how to optimize SQL with GROUP BY and WHERE? Please see my case.
I have a table to store traffic data and use it to draw a network traffic topology. Below is the table structure :
DROP TABLE IF EXISTS `data`;
CREATE TABLE `data`
(
`sip` varbinary(16) DEFAULT NULL,
`dip` varbinary(16) DEFAULT NULL,
`app` char(96) DEFAULT NULL,
`up` bigint(20) DEFAULT NULL,
`down` bigint(20) DEFAULT NULL,
`dtime` datetime DEFAULT CURRENT_TIMESTAMP,
KEY `dtime` (`dtime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And create index on column dtime.
The simplified SQL is
SELECT
INET6_NTOA(sip),
INET6_NTOA(dip),
app,
sum(up) AS up,
sum(down) AS down
FROM
`data`
WHERE
`data`.dtime > FROM_UNIXTIME(1583031879)
AND `data`.dtime < FROM_UNIXTIME(1585537477)
GROUP BY
sip,
dip,
app
The table can store about 10,000,000 records for one month and our requirement is to draw a network traffic topology with granularity for Last 30 days, Last 24 hours, Last 1 hour.
Obviously the index dtime is helpful to query data for last 1 hour or last 24 hours. But when query for last 30 days, it's a full table scan.
In extreme case, querying for 24 hours costs 5s, which is acceptable, while querying for 30 days costs 60s+ which is hard to accept.
Create index for sip,dip,app ? Seems not helpful, for I have to filter data by dtime at first. I googled the various indexing solutions, which may not suit for me.
Any ideas to speed up my SQL ? Or Any ideas to improve table design ? Thank you very much.