optimization and indexing problems for sum in MySQL

Source: Internet
Author: User
Table structure CREATE TABLE IF not EXISTS ' Radacct ' (' Radacctid ' bigint (+) not NULL auto_increment, ' UserName ' varchar ($) Not N ULL default ' ', ' acctsessiontime ' int (+) default NULL, ' Acctinputoctets ' bigint () default null, ' Acctoutputoctets ' B  Igint () DEFAULT NULL, ..... PRIMARY key (' Radacctid '), key ' UserName ' (' UserName '), key ' Acctsessiontime ' (' acctsessiontime '), key ' Acctinputoctets ' (' acctinputoctets '), KEY ' acctoutputoctets ' (' acctoutputoctets ')) Engine=myisam DEFAULT Charset=utf8 collate=utf8_ Unicode_ci auto_increment=456017; $sql = ' SELECT UserName, COUNT (*) as numofsession, sum (acctsessiontime) as time, sum (acctinputoctets) as Upload, sum (accto  Utputoctets) as Download, sum (acctinputoctets+acctoutputoctets) as Bandwidth from Radacct GROUP by UserName '; Mysql> explain SELECT UserName, COUNT (*) as numofsession, sum (acctsessiontime) as time, sum (acctinputoctets) as UPL Oad, SUM (acctoutputoctets) as Download, sum (acctinputoctets + acctoutputoctets) as BaNdwidth from Radacct GROUP by UserName; +----+-------------+---------+------+---------------+------+---------+------+--------+------------------------- --------+| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |+----+-------------+---------+------+---------------+------+---------+------+--------+---  ------------------------------+| 1 | Simple | Radacct | All | NULL | NULL | NULL | NULL | 456010 | Using temporary; Using filesort |+----+-------------+---------+------+---------------+------+---------+------+--------+---------- -----------------------+

The amount of data is about 45w. Adding an index to the sum field does not improve query efficiency

Plus paging or something is even slower.

How to optimize the comparison good AH first humbly


Reply to discussion (solution)

First of all, if you use the calculation, the index will be invalidated, so you do not add index no effect.

My idea is this, if the table becomes extraordinary, you can consider caching a copy of the data to replace your query every time.

First of all, if you use the calculation, the index will be invalidated, so you do not add index no effect.

My idea is this, if the table becomes extraordinary, you can consider caching a copy of the data to replace your query every time.
Okay, no one's going to give you all the points for the year.

Plus paging or something is even slower. My idea is this, if the table becomes extraordinary, you can consider caching a copy of the data in lieu of your query every time.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.