Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 623269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:04:29+00:00 2026-05-13T19:04:29+00:00

I’m trying to solve a problem with latency on a to a mysql-5.0 db.

  • 0

I’m trying to solve a problem with latency on a to a mysql-5.0 db.

  • The query itself is extremely simple: SELECT SUM(items) FROM tbl WHERE col = 'val'
  • There’s an index on col and there are not more than 10000 values to sum in the worst case (mean of count(items) for all values of col would be around 10).
  • The table has up to 2M rows.
  • The query is run frequently enough that sometimes the execution time goes up to 10s, although 99% of them take << 1s
  • The query is not really cachable – in almost every case, each query like this one will be followed by an insert to that table in the next minute and showing old values is out of question (billing information).
  • keys are good enough – ~100% hits

The result I’m looking for is every single query < 1s. Are there any ways to improve the select time without changes to the table? Alternatively, are there any interesting changes that would help to resolve the problem? I thought about simply having a table where the current sum is updated for every col right after every insert – but maybe there are better ways to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:04:29+00:00Added an answer on May 13, 2026 at 7:04 pm

    Another approach is to add a summary table:

    create table summary ( col varchar(10) primary key, items int not null );
    

    and add some triggers to tbl so that:

    on insert:

    insert into summary values( new.col, new.items ) 
    on duplicate key update set summary.items = summary.items + new.items;
    

    on delete:

    update summary set summary.items = summary.items - old.items where summary.col = old.col
    

    on update:

    update summary set summary.items = summary.items - old.items where summary.col = old.col;
    update summary set summary.items = summary.items + new.items where summary.col = new.col;
    

    This will slow down your inserts, but allow you to hit a single row in the summary table for

    select items from summary where col = 'val';
    

    The biggest problem with this is bootstrapping the values of the summary table. If you can take the application offline, you can easily initialise summary with values from tbl.

    insert into summary select col, sum(items) from tbl group by col;
    

    However, if you need to keep the service running, it is a lot more difficult. If you have a replica, you can stop replication, build the summary table, install the triggers, restart replication, then failover the service to using the replica, and then repeat the process on the retired primary.

    If you cannot do that, then you could update the summary table one value of col at a time to reduce the impact:

    lock table write tbl, summary; 
    delete from summary where col = 'val';
    insert into summary select col, sum(items) from tbl where col = 'val';
    unlock tables;
    

    Or if you can tolerate a prolonged outage:

    lock table write tbl, summary;
    delete from summary;
    insert into summary select col, sum(items) from tbl group by col;
    unlock tables;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.