我所拥有的:(解释)
我使用FreeRadius半径服务器,我想计算今天用户使用的总流量。
我有一个名为users的表,第二个表是users_graph

users_graph表每15分钟收集一次带宽数据(每个用户每天96行)。

我尝试进行查询以收集具有指定组名和今天使用的总流量的SUM的用户。

我做的事 :
我在下面的SQL查询中进行测试并测试结果,测试每个用户使用20个用户,并在users_graph上测试80行。

结果:我的查询的执行时间约为3秒。

我想要的是 :
我尝试改善查询以提供更高的性能和最快的执行时间。
有没有办法改善下面的查询?

MyQuery:

SELECT
  u.`UserName`,
  SUM(g.`Total`) totaltime
FROM
  users u
  INNER  JOIN `users_graph` g
    ON g.`UserName` = u.`UserName`
WHERE u.`GroupName` = 'Festival'  AND g.`Time` > '1408995000'
GROUP BY u.`UserName`;


Samle Resut:

UserName   GroupName    totaltime
---------  -----------  ----------
test123    Festival      81
test332    Festival      19
test523    Festival      29


编辑:

表信息:

/*Table: users_graph*/
----------------------

/*Column Information*/
----------------------

Field     Type          Collation          Null    Key     Default  Extra
--------  ------------  -----------------  ------  ------  -------  --------------
Guid      bigint(22)    (NULL)             NO      MUL     (NULL)   auto_increment
Time      int(11)       (NULL)             YES             0
UserName  varchar(255)  latin1_swedish_ci  YES             (NULL)
Down      int(11)       (NULL)             YES             0
Up        int(11)       (NULL)             YES             0
Total     int(11)       (NULL)             YES             0

/*Index Information*/
---------------------

Table        Non_unique  Key_name  Seq_in_index  Column_name  Collation  Cardinality  Sub_part  Packed  Null    Index_type
-----------  ----------  --------  ------------  -----------  ---------  -----------  --------  ------  ------  ----------
users_graph           1  Guid                 1  Guid         A              5584276    (NULL)  (NULL)          BTREE

/*DDL Information*/
-------------------

CREATE TABLE `users_graph` (
  `Guid` bigint(22) NOT NULL AUTO_INCREMENT,
  `Time` int(11) DEFAULT '0',
  `UserName` varchar(255) DEFAULT NULL,
  `Down` int(11) DEFAULT '0',
  `Up` int(11) DEFAULT '0',
  `Total` int(11) DEFAULT '0',
  KEY `Guid` (`Guid`)
) ENGINE=MyISAM AUTO_INCREMENT=5584277 DEFAULT CHARSET=latin1

/*Table: users*/
----------------

/*Index Information*/
---------------------

Table   Non_unique  Key_name    Seq_in_index  Column_name  Collation  Cardinality  Sub_part  Packed  Null    Index_type
------  ----------  ----------  ------------  -----------  ---------  -----------  --------  ------  ------  ----------
users            0  PRIMARY                1  id           A                21250    (NULL)  (NULL)          BTREE
users            0  UserName               1  UserName     A                21250    (NULL)  (NULL)          BTREE

/*DDL Information*/
-------------------

CREATE TABLE `users` (
`id` bigint(21) NOT NULL AUTO_INCREMENT,
`UserName` varchar(32) NOT NULL DEFAULT '',
`Pass` varchar(32) NOT NULL DEFAULT '',
`GroupName` varchar(32) NOT NULL DEFAULT '',
) ENGINE=MyISAM AUTO_INCREMENT=27456 DEFAULT CHARSET=latin1

最佳答案

必须索引搜索列,例如,如果我要搜索time大于等于的地方,则必须索引time列,然后,对Mysql排序time行。

索引列可以检索更快的结果。

10-04 12:41
查看更多