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

  • SEARCH
  • Home
  • 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 384533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:23:23+00:00 2026-05-12T15:23:23+00:00

I use a 1 column memory table to keep track of views on various

  • 0

I use a 1 column memory table to keep track of views on various items in my DB. Each view = INSERT query into the memory table. Every 10 mins, I wanna count() the rows for each item, and commit changes to DB.

The question is…. if I run the query that will get the list of all items, such as

SELECT COUNT(*) AS period_views, `item_id` FROM `-views` GROUP BY `item_id` ORDER BY `item_id`

and then run an update query for each row to add the amount of views in that period, and then truncate the table. This operation might take a few seconds…. and in those few seconds, there is going to be other INSERTS into that table, that didnt make it into the original count. Will they be truncated too once that command executes? or will the table be locked until the entire operation completes, and the the new INSERTs added?

  • 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-12T15:23:23+00:00Added an answer on May 12, 2026 at 3:23 pm

    MySQL does not lock the table automatically, and it is possible that you will lose some records in between getting the count and performing the truncate. So two solutions jump out at me:

    1) Use table locks to prevent the memory table being updated – depending on the nature of your application, this means that all of your clients might freeze for a few seconds while you are updating, this might be OK.

    2) Add a second column to keep track of which records you are currently updating …

    ALTER TABLE `-views` ADD work_in_progress TINYINT NOT NULL DEFAULT 0;
    

    And then when you want to work on the those records

    UPDATE `-views` SET work_in_progress = 1;
    SELECT COUNT(*) AS period_views, `item_id` FROM `-views` WHERE work_in_progress GROUP BY `item_id` ORDER BY `item_id`;
    # [ perform updates as necessary ]
    DELETE FROM `-views` WHERE work_in_progress;
    

    This implementation will guarantee that you don’t delete any -views which were added while you were updating.

    And FWIW, -views is an awful name for a table!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In MS Access, I have a query where I want to use a column
I’m not sure how to use Java/JDBC to insert a very long string into
I want to insert some data into a table and I want the multiple
This table only exists in memory, so can't do a quick SQL query. I
I need to load one column of strings from table on SqlServer into Array
I have temporary memory tables A and B. each contain 1 column of integer
When retrieving values from a DataRow is it better to use the column name
The definition I found for the blueprint css class called .column says: .column Use
How to use a calculated column in the where condition in Oracle 9i? I
I know you cannot use a alias column in the where clause for T-SQL;

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.