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 8524985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:50:39+00:00 2026-06-11T07:50:39+00:00

I am having the following problem processing big data in the database: Basically all

  • 0

I am having the following problem processing big data in the database:

Basically all the metering from digital sensors are stored in the database for each second.
What the reports should show from all that data is only the occurred changes, for example at time X the register #1 changed value from 0 to 1.

I have created a procedure that is able to return only the data i need (the changes), and that is saving me a lot of processing in php BUT the big problem is that for a current data of 4 days the query takes 6 * N seconds to complete where N is the number of the selected registers.

Now i was wondering what is the best solution to overcome this problem.

Another thought is to make a trigger on each new insert of the data metering but the problem is that this will be more complicated since i will need to look into the previous metering that were submitted at another time.

So I thought to create views that will be automatically updated when new data arrives in some way. That means when the request is made for the reports the data will be ready and fetched from the view.

Will this be a good solution?

  • 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-06-11T07:50:41+00:00Added an answer on June 11, 2026 at 7:50 am

    Identifying status changes from your existing data is possible with a single query, but (as you have discovered) quite expensive. I would urge you to store each status change in a cache.

    As @Fluffeh explained, looking up the latest status from your existing table won’t be very expensive if you use a suitable index; so the trigger approach ought to be quite reasonable.

    Therefore:

    1. Define a suitable index (if it does not already exist):

      ALTER TABLE existing_table ADD INDEX (register_id, timestamp);
      
    2. Create a table for the cache (and optionally set user permissions so that it cannot be directly modified by your application):

      CREATE TABLE status_changes VALUES (
        register_id ...,
        timestamp   TIMESTAMP,
        old_status  ...,
        new_status  ...,
      
        PRIMARY KEY                (register_id, timestamp),
      
        FOREIGN KEY                (register_id, timestamp, old_status)
         REFERENCES existing_table (register_id, timestamp, status),
      
        FOREIGN KEY                (register_id, timestamp, new_status)
         REFERENCES existing_table (register_id, timestamp, status)
      );
      
    3. Define a trigger from a user that has permission to modify the new table:

      DELIMITER ;;
      
      CREATE TRIGGER record_change AFTER INSERT ON existing_table FOR EACH ROW
      BEGIN
        DECLARE  _last_status ... ;
      
        SELECT   last.status
        INTO     _last_status
        FROM     existing_table AS last
        WHERE    last.register_id <=> NEW.register_id
             AND last.timestamp    <  NEW.timestamp
        ORDER BY last.timestamp DESC
        LIMIT    1;
      
        IF NOT NEW.status <=> _last_status THEN
          INSERT INTO status_changes (
            register_id,
            timestamp,
            old_status,
            new_status
          ) VALUES (
            NEW.register_id,
            NEW.timestamp,
            _last_status,
            NEW.status
          );
        END IF;
      END;;
      
      DELIMITER ;
      
    4. Populate the new table from the historical data:

      INSERT IGNORE INTO status_changes (
        register_id,
        timestamp,
        old_status,
        new_status
      )
      SELECT NEW.register_id,
             NEW.timestamp,
             (
               SELECT   last.status
               FROM     existing_table AS last
               WHERE    last.register_id <=> NEW.register_id
                    AND last.timestamp    <  NEW.timestamp
               ORDER BY last.timestamp DESC
               LIMIT    1
             ) AS _last_status,
             NEW.status
      FROM   existing_table AS NEW
      WHERE  NOT NEW.status <=> (
               SELECT   last.status
               FROM     existing_table AS last
               WHERE    last.register_id <=> NEW.register_id
                    AND last.timestamp    <  NEW.timestamp
               ORDER BY last.timestamp DESC
               LIMIT    1
             )
      ;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the following problem: When I got two labels into each other: <Label
I am running xcode 4.3.2 and just started having the following problem. All projects
I'm having problems doing the Database example from PhoneGap. http://docs.phonegap.com/phonegap_storage_storage.md.html#openDatabase The Problem is, when
I'm having a problem with submitting a form and Javascript confirmation. Basically, the following
I'm having following problem. I should convert a control to a certain type, this
I am having the following problem: a) I have a UNIX build environment set
I'm having the following problem and wondered whether anyone could see why this is
I have been having the following problem, i think it's probably due to the
I'm having the following problem using the Visual Studio 2010 Team System Beta 1:
I've been having the following problem with my GoDaddy's server. I am using JSPs

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.