Author Topic: Determining if a table has been updated  (Read 1540 times)

0 Members and 1 Guest are viewing this topic.

Offline kickstartTopic starter

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Determining if a table has been updated
« on: September 09, 2010, 06:09:13 AM »
Hi

I have googled around but can't find anything beyond using the table checksum.

What I want to do is store an object based on fairly static table data. This way it can just be serialized to store it as 99% of the time the table data will not have changed, and when brought back it can just check if it needs to update itself.

Is there a way to determine if a table has changed (ie, any records added, updated, deleted or inserted) since it was last read?

Table checksum might be useable, although it is theoretically possible that an update could result in no change to the checksum.

Checking all the rows would defeat the object of the exercise.
There are 10 types of people in the world. Those who understand binary and those who don't

Offline mikosiko

  • Devotee
  • Posts: 946
    • View Profile
Re: Determining if a table has been updated
« Reply #1 on: September 09, 2010, 09:56:26 AM »
column Update_time in "show table status" maybe?

error_reporting(E_ALL); 
ini_set("display_errors"1);

Offline kickstartTopic starter

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Re: Determining if a table has been updated
« Reply #2 on: September 09, 2010, 10:36:39 AM »
Hi

Thanks. Unfortunately I am on a Windows system and the update time is given as NULL :'( .

All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline mikosiko

  • Devotee
  • Posts: 946
    • View Profile
Re: Determining if a table has been updated
« Reply #3 on: September 09, 2010, 10:45:27 AM »
if you are using MyIsam storage engine and you have access to INFORMATION_SCHEMA.tables table you can get the last update from there... doesn't work for Innodb storage.

error_reporting(E_ALL); 
ini_set("display_errors"1);

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Determining if a table has been updated
« Reply #4 on: September 09, 2010, 10:53:00 AM »
I would use triggers (one each for INSERT, DELETE, and UPDATE queries) to write the datetime to a Semaphore table that you could query to find out if the table in question has been changed.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline kickstartTopic starter

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Re: Determining if a table has been updated
« Reply #5 on: September 09, 2010, 12:11:11 PM »
Hi

mikosiko - nice idea but probably best to avoid anything that is db engine specific.

I would use triggers (one each for INSERT, DELETE, and UPDATE queries) to write the datetime to a Semaphore table that you could query to find out if the table in question has been changed.

That idea appeals to me the best so far. Minor hit on update queries, but for the situation I envisage with fairly static data this shouldn't be a major problem.

Thank you

All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Determining if a table has been updated
« Reply #6 on: September 10, 2010, 11:43:44 AM »
Yup -- I stumbled upon this issue a while ago -- SHOW TABLE STATUS only works for MyISAM, and even then, only on Linux.  Crazy.

Right now, since I'm using a DB wrapper anyway, I just automatically update a system table (with one record per table) whenever my app issues any update/insert/delete statements.

Triggers is a technically a better way to handle this, but because you have to have 3 on each table, I just find it really annoying to maintain.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline kickstartTopic starter

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Re: Determining if a table has been updated
« Reply #7 on: September 10, 2010, 07:19:27 PM »
Hi

What concerns me about using some kind of manual way to record the updates (such as a wrapper) is it won't take account of manual changes such as someone sorting out a problem through phpmyadmin.

I am still a bit tempted to use the check sum.

Thankyou.

All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Determining if a table has been updated
« Reply #8 on: September 10, 2010, 10:36:59 PM »
Table checksum might be useable, although it is theoretically possible that an update could result in no change to the checksum.
Yes, but incredibly unlikely.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline RussellReal

  • Addict
  • Posts: 1,730
  • Gender: Male
    • View Profile
Re: Determining if a table has been updated
« Reply #9 on: September 10, 2010, 11:49:30 PM »
I was also gonna suggest triggers, I think thats 2 thumbs up for triggers :)
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com

Offline kickstartTopic starter

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Re: Determining if a table has been updated
« Reply #10 on: September 13, 2010, 04:52:55 AM »
Yes, but incredibly unlikely.

Very true and not critical if it does happen hence thinking of it. Just that it seems million to one chances occur 9 times out of 10.

All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Determining if a table has been updated
« Reply #11 on: September 13, 2010, 10:51:20 AM »
I mean, the CHECKSUM algorithm is really just millions of CRC32() calls, and with a large enough table, it's unlikely to collide in hash generation.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline kickstartTopic starter

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Re: Determining if a table has been updated
« Reply #12 on: September 13, 2010, 11:13:14 AM »
Hi

I know, just that it seems wrong. The chances of an issue are small but an easily returned date of last update would mean effectively zero chance. But a trigger means a load of extra hidden updates.

All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Determining if a table has been updated
« Reply #13 on: September 13, 2010, 11:48:22 AM »
It's really annoying that the update_time isn't maintained on windows.
Quote
MyISAM uses file's timestamp  for Update_time that you see in SHOW TABLE STATUS. Windows
does not update timestamp of the open file (according to my experiments) even if you do
fflush().

So really, they're just using the same code on both platforms, instead of doing it properly.  Seems like an easy patch to commit.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline shlumph

  • Enthusiast
  • Posts: 451
  • Gender: Male
    • View Profile
    • My Site
Re: Determining if a table has been updated
« Reply #14 on: September 13, 2010, 12:12:30 PM »
Have you considered caching the result set, and then purging the cache on inserts/deletes/updates?
whether you think you can, or can't, you're right -HF
Visit my website or github