If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Delete last row on insertion of new row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-15-10, 15:24
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Delete last row on insertion of new row

Is there a built-in way such that when a new record gets inserted, the last record gets deleted ?

I don't want more than 100 rows in a table. So when a new row is being inserted, I would like to flush out the last one from the table.

Basically, this data is coming from an incoming feed, and I dont want the database filled with all those data constantly.
__________________
MySQL 5.1
Reply With Quote
  #2 (permalink)  
Old 04-15-10, 16:59
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
If you have an auto_increment field on the table you can remove entries that are equal to 100 less than the new id. This could be performed inside a trigger using the last_insert_id() function which gives you the latest auto increment identifier.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 04-16-10, 03:20
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
I don't want to have an auto increment field as there are like 1000 rows being inserted every hour, which may result in overflow of max length of the auto_increment ID.
Reply With Quote
  #4 (permalink)  
Old 04-16-10, 03:55
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Is there a date/time field to determine the order in which the records arrive? If there is a date time stamp then the records to remove can be identified with the following select:

SELECT * FROM tablename ORDER BY datefield DESC LIMIT 100,1;
Using the date field from this query you can then use this to remove all entries older than this date/time.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 04-16-10, 05:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by anjanesh View Post
I don't want to have an auto increment field as there are like 1000 rows being inserted every hour, which may result in overflow of max length of the auto_increment ID.
using an INTEGER auto_increment column, with 1000 rows arriving per hour, guess how long before the overflow?

if you do the math, you will discover that it will take 245 years

if the column is UNSIGNED it will take 490 years

so how realistic is your concern about overflow? i would say not realistic at all

there has never, in the history of computer systems on this planet, been an application that has lasted that long

yours will ~not~ be the first

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 04-16-10, 10:42
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
and of course, just in case, there's always UNSIGNED BIGINT
Reply With Quote
  #7 (permalink)  
Old 04-19-10, 05:10
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Im thinking of using SQLite for this instead.
The data is coming in via Twitter's Streaming API which is a never ending script.
Quote:
Is there a date/time field to determine the order in which the records arrive?
Yes, and the data coming in is date sorted anyway.

Is it better to write a cron to delete entries above 100 or create a trigger to delete last row on new inserts ?
Reply With Quote
  #8 (permalink)  
Old 04-19-10, 10:26
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
This is something you have to decide. One way you are basically guaranteeing to have only 100 rows in the table at any single time, the other your table may grow as entries arrive.

One that that has not been mentioned previously in the posts concerns any indexes that you may have on the table where you are going to store the information. Inserts and deletes causes the indexes to become inefficient over time. You should also consider rebuilding these indexes to keep them optimized for access.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On