![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
SUBDATE(CURDATE(), INTERVAL 7 DAY) | +------------------------------------+ 2009-06-29 | +------------------------------------+ |
#2
| |||
| |||
|
|
I have a script that I use to create another script to drop temporary tables that the applications is not taking care of, but I noticed that when the update_time column is NULL the script is not working properly... mysql -p information_schema -s -e "select concat('drop table ', trim (table_schema),'.',rtrim(table_name),';'),left(cre ate_time,10) from information_schema.tables where create_time < SUBDATE(CURDATE(), INTERVAL 7 DAY) and table_name like 'ruletable%'or table_name like 'temptable%'; " > / tmp/t.sql when I get the /tmp/t.sql file I get the following: drop table uptilt_db.ruletable_pp_web1test_10765_1243383842_3 813282_prevline; 2009-05-26 drop table uptilt_db.ruletable_pp_web1test_10765_1243383842_3 813282_prevval; 2009-05-26 drop table uptilt_db.temptable2_mail1test_0_12464928754189216 ; 2009-07-01 drop table uptilt_db.temptable3_mail1test_0_12464928752861763 ; 2009-07-01 drop table uptilt_db.temptable_supression_12465660084700031; 2009-07-02 As you can see the last 3 records should not be there, since I am trying to get the tables with "<" (less than July 1st, not including July 1st) mysql> select SUBDATE(CURDATE(), INTERVAL 7 DAY); +------------------------------------+ | SUBDATE(CURDATE(), INTERVAL 7 DAY) | +------------------------------------+ | 2009-06-29 | +------------------------------------+ 1 row in set (0.00 sec) Note: the left(create_time) was added to show the create and update time only!! But for some reason, perhaps the where clause is not really taking care of the the dates. Please advice, thanks in advance. |
#3
| |||||||
| |||||||
|
|
I have a script that I use to create another script to drop temporary tables that the applications is not taking care of |
|
but I noticed that when the update_time column is NULL the script is not working properly... |
|
mysql -p information_schema -s -e "select concat('drop table ', trim (table_schema),'.',rtrim(table_name),';'),left(cre ate_time,10) from information_schema.tables where create_time < SUBDATE(CURDATE(), INTERVAL 7 DAY) and table_name like 'ruletable%'or table_name like 'temptable%'; " > / tmp/t.sql |
|
uptilt_db.temptable2_mail1test_0_12464928754189216 ; 2009-07-01 drop table uptilt_db.temptable3_mail1test_0_12464928752861763 ; 2009-07-01 drop table uptilt_db.temptable_supression_12465660084700031; 2009-07-02 As you can see the last 3 records should not be there, since I am trying to get the tables with "<" (less than July 1st, not including July 1st) mysql> select SUBDATE(CURDATE(), INTERVAL 7 DAY); +------------------------------------+ | SUBDATE(CURDATE(), INTERVAL 7 DAY) | +------------------------------------+ | 2009-06-29 | +------------------------------------+ |
|
table_schema | table_name | create_time | +--------------+------------+---------------------+ test | sequence | 2008-11-17 12:52:58 | test | t1 | 2009-06-15 15:13:35 | +--------------+------------+---------------------+ |
|
table_schema | table_name | create_time | +--------------+------------+---------------------+ test | sequence | 2008-11-17 12:52:58 | +--------------+------------+---------------------+ |
|
version() | +-----------+ 5.0.79 | +-----------+ |
#4
| |||
| |||
|
|
argentin <UnArgent... (AT) gmail (DOT) com> wrote: I have a script that I use to create another script to drop temporary tables that the applications is not taking care of This makes no sense. Temporary tables are session-scoped objects. If your application has temporary tables in open sessions, this cannot be seen from other sessions. Maybe you're not talking about "real" temporary tables, but about normal SQL tables? but I noticed that when the update_time column is NULL the script is not working properly... I don't see you using the `update_time` column below... mysql -p information_schema -s -e "select concat('drop table ', trim (table_schema),'.',rtrim(table_name),';'),left(cre ate_time,10) from information_schema.tables where create_time < SUBDATE(CURDATE(), INTERVAL 7 DAY) and table_name like 'ruletable%'or table_name like 'temptable%'; " > / tmp/t.sql Further more: - there is no need to trim the table_schema or table_name columns - you should not use LEFT() on the create_time column; create_time * is a DATETIME, better use DATE_FORMAT() or DATE() to extract the * DATE part - the comparison create_time < SUBDATE(CURDATE(), INTERVAL 7 DAY) * has different types left/right from the < operator. MySQL thus * extends the right side to DATETIME (using time 00:00:00) and * compares as DATETIME. This might give an unexpected result * (though not in this case). ... uptilt_db.temptable2_mail1test_0_12464928754189216 ; 2009-07-01 drop table uptilt_db.temptable3_mail1test_0_12464928752861763 ; 2009-07-01 drop table uptilt_db.temptable_supression_12465660084700031; 2009-07-02 As you can see the last 3 records should not be there, since I am trying to get the tables with "<" (less than July 1st, not including July 1st) mysql> select SUBDATE(CURDATE(), INTERVAL 7 DAY); +------------------------------------+ | SUBDATE(CURDATE(), INTERVAL 7 DAY) | +------------------------------------+ | 2009-06-29 * * * * * * * * * * * * | +------------------------------------+ This is strange. Works perfectly for me: mysql>select table_schema, table_name, create_time * * -> from information_schema.tables * * -> where table_schema='test' * * -> and create_time<SUBDATE(CURDATE(), INTERVAL 21 DAY); +--------------+------------+---------------------+ | table_schema | table_name | create_time * * * * | +--------------+------------+---------------------+ | test * * * * | sequence * | 2008-11-17 12:52:58 | | test * * * * | t1 * * * * | 2009-06-15 15:13:35 | +--------------+------------+---------------------+ mysql>select table_schema, table_name, create_time * * -> from information_schema.tables * * -> where table_schema='test' * * -> and create_time<SUBDATE(CURDATE(), INTERVAL 22 DAY); +--------------+------------+---------------------+ | table_schema | table_name | create_time * * * * | +--------------+------------+---------------------+ | test * * * * | sequence * | 2008-11-17 12:52:58 | +--------------+------------+---------------------+ mysql>select version(); +-----------+ | version() | +-----------+ | 5.0.79 * *| +-----------+ XL |
![]() |
| Thread Tools | |
| Display Modes | |
| |