Problem Solution Discussion Moving Records Between Tables Safely

• I f you want t o m ake t he dest inat ion t able an exact copy of t he source t able, use t he cloning t echnique described in Recipe 3.26 .

3.24 Moving Records Between Tables Safely

3.24.1 Problem

Youre m oving records by copying t hem from one t able t o anot her and t hen delet ing t hem from t he original t able. But som e records seem t o be get t ing lost .

3.24.2 Solution

Be careful t o delet e exact ly t he sam e set of records from t he source t able t hat you copied t o t he dest inat ion t able.

3.24.3 Discussion

Applicat ions t hat copy rows from one t able t o anot her can do so wit h a single operat ion, such as INSERT ... SELECT t o ret rieve t he relevant rows from t he source t able and add t hem t o t he dest inat ion t able. I f an applicat ion needs t o m ove rat her t han copy rows, t he procedure is a lit t le m ore com plicat ed: Aft er copying t he rows t o t he dest inat ion t able, you m ust rem ove t hem from t he source t able. Concept ually, t his is not hing m ore t han INSERT ... SELECT follow ed by DELETE . I n pract ice, t he operat ion m ay require m ore care, because it s necessary t o select exact ly t he sam e set of rows in t he source t able for bot h t he INSERT and DELETE st at em ent s. I f ot her client s insert new rows int o t he source t able aft er you issue t he INSERT and before you issue t he DELETE , t his can be t ricky. To illust rat e, suppose you have an applicat ion t hat uses a working log t able worklog int o which records are ent ered on a cont inual basis, and a long- t erm reposit ory log t able repolog . Periodically, you m ove worklog records int o repolog t o keep t he size of t he working log sm all, and so t hat client s can issue possibly long- running log analysis queries on t he reposit ory wit hout blocking processes t hat creat e new records in t he working log. [ 3] [ 3] I f you use a MyI SAM log t able t hat you only insert int o and never delet e from or m odify, you can run queries on t he t able wit hout prevent ing ot her client s from insert ing new log records at t he end of t he t able. How do you properly m ove records from worklog t o repolog in t his sit uat ion, given t hat worklog is subj ect t o ongoing insert act ivit y? The obvious but incorrect way is t o issue an INSERT ... SELECT st at em ent t o copy all t he worklog records int o repolog , follow ed by a DELETE t o rem ove t hem from worklog : INSERT INTO repolog SELECT FROM worklog; DELETE FROM worklog; This is a perfect ly workable st rat egy when youre cert ain nobody else will insert any records int o worklog during t he t im e bet ween t he t wo st at em ent s. But if ot her client s insert new records in t hat period, t heyll be delet ed wit hout ever having been copied, and youll lose records. I f t he t ables hold logs of web page request s, t hat m ay not be such a big deal, but if t heyre logs of financial t ransact ions, you could have a serious problem . What can you do t o keep from losing records? Two possibilit ies are t o issue bot h st at em ent s wit hin a t ransact ion, or t o lock bot h t ables while youre using t hem . These t echniques are covered in Chapt er 15 . However, eit her one m ight block ot her client s longer t han youd prefer, because you t ie up t he t ables for t he durat ion of bot h queries. An alt ernat ive st rat egy is t o m ove only t hose records t hat are older t han som e cut off point . For exam ple, if t he log records have a colum n t cont aining a t im est am p, you can lim it t he scope of t he select ed records t o all t hose creat ed before t oday. Then it wont m at t er whet her new records are added t o worklog bet ween t he copy and delet e operat ions. Be sure t o specify t he cut off properly, t hough. Heres a m et hod t hat fails under som e circum st ances: INSERT INTO repolog SELECT FROM worklog WHERE t CURDATE ; DELETE FROM worklog WHERE t CURDATE ; This wont work if you happen t o issue t he INSERT st at em ent at one second before m idnight and t he SELECT st at em ent one second lat er. The value of CURDATE will differ for t he t wo st at em ent s, and t he DELETE operat ion m ay rem ove t oo m any records. I f youre going t o use a cut off, m ake sure it has a fixed value, not one t hat m ay change bet ween st at em ent s. For exam ple, a SQL variable can be used t o save t he value of CURDATE in a form t hat wont change as t im e passes: SET cutoff = CURDATE ; INSERT INTO repolog SELECT FROM worklog WHERE t cutoff; DELETE FROM worklog WHERE t cutoff; This ensures t hat bot h st at em ent s use t he sam e cut off value so t hat t he DELETE operat ion doesnt rem ove records t hat it shouldnt .

3.25 Creating Temporary Tables