[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 04-30-2007, 09:42 AM   #1 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


Interesting mySQL backup problem . . .

I am backing up several tables via a PHP script. The script lists the tables, you click on one, then click "backup table" and the table is backed up (the new table name is the old table name appended with the date of the backup, so table 'orders' would become 'orders_043007').

The problem is the size of the backed up table. They always end up being 6x larger than the original table.

I backed up one table last night that had a size of 31.8 MB and the backup was 197.4 MB, according to the data in phpMyAdmin.

The SQL I am using to do the backup is:

PHP Code:
$dateString = date('mdy');
$newTable = $_POST[buTable] . "_$dateString";
$sql = "CREATE TABLE $newTable AS SELECT * FROM $_POST[buTable]";
The other thing I am doing after the copy / creation is I am setting the primary keys:

PHP Code:
        # add primary keys
        
$pk = array (
            
'AUTO_ORDER' => 'ID',
            
'CB_ITEMS' => 'id',
            
'CB_ITEMS_CLOSED' => 'id',
            
'CB_ORDERS' => 'number',
            
'CB_ORDERS_CLOSED' => 'number',
            
'CREDITS' => 'id',
            
'CUSTOMER' => 'custID',
            
'product' => 'id',
            
'RESUBMITS' => 'id',
            );
            
        
$pk_type = array (
            
'AUTO_ORDER' => 'int',
            
'CB_ITEMS' => 'bigint',
            
'CB_ITEMS_CLOSED' => 'bigint',
            
'CB_ORDERS' => 'bigint',
            
'CB_ORDERS_CLOSED' => 'bigint',
            
'CREDITS' => 'bigint',
            
'CUSTOMER' => 'bigint',
            
'product' => 'int',
            
'RESUBMITS' => 'bigint',
        );
        
        
$sql = "ALTER TABLE $newTable ADD PRIMARY KEY({$pk[$_POST[buTable]]})";
        
$result = mysql_db_query($db_name,$sql,$db_connect);
        if(
mysql_error($db_connect)) {die("Error:<br>SQL: $sql<br>Error: " . mysql_error($db_connect) . "<br>");}
        
        
        
$sql = "ALTER TABLE $newTable CHANGE {$pk[$_POST[buTable]]} {$pk[$_POST[buTable]]} {$pk_type[$_POST[buTable]]} NOT NULL AUTO_INCREMENT";
        
$result = mysql_db_query($db_name,$sql,$db_connect);
        if(
mysql_error($db_connect)) {die("Error:<br>SQL: $sql<br>Error: " . mysql_error($db_connect) . "<br>");}
        
main("Backed up Table: $_POST[buTable]");
Anybody have any ideas or thoughts as to why this large increase in size might be happening?
__________________
The mass purge has begun.
moondog is offline  
Old 04-30-2007, 10:53 AM   #2 (permalink)
NamePros Member
 
Join Date: Apr 2005
Posts: 116
134.00 NP$ (Donate)

mikesherov will become famous soon enoughmikesherov will become famous soon enough


A few questions that may get you thinking about the solution:

1. Was the original data created using a different version of MySQL than you are using now? The way certain things are stored changed over the past few MySQL updates. For example, namepros member DanR can tell you that DECIMAL changed the way it works as of MySQL 5.0.3! http://www.namepros.com/programming/...-pay-25-a.html

2. Why are you altering the new table's primary key type? If you look at http://dev.mysql.com/doc/refman/5.0/...ric-types.html , you'll see that BIGINT requires 8 bytes, where INT only requires 4. MEDIUMINT requires 3, SMALLINT requires 2, and TINYINT only 1!

3. Have you tried using ANALYZE TABLE? This is a dead giveaway if PHPMyAdmin is saying that the cardinality of your indexes is "none". See if that helps.

Other than that, it may be helpful if you most the structures of your old and new tables here. Use PHPMyAdmin to dump the structure of both tables and post it here...
mikesherov is offline  
Old 04-30-2007, 11:30 AM   #3 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


The backup is on the same server, the same mysql version. Basically, once per week, I just go in and backup the existing table to the same database, with the date appended. Same server, same version, same everything.

The reason I alter the table's primary keys is because I noticed that when I did the SQL:

Code:
$sql = "CREATE TABLE $newTable AS SELECT * FROM $_POST[buTable]";
the primary key information did not follow through. When I had to restore the table to where it was previously, I would rename the table back to its original form, but then I would have to manually set up the primary keys again. So, I just did it via code.

Lastly, you suggest that I run an "analyze" on the table. I did that and the message I receive back was that the status was "OK". And, the cardinality is set (i.e. it does not say "none").

Ugh . . .

-Bob


Quote:
Originally Posted by mikesherov
A few questions that may get you thinking about the solution:

1. Was the original data created using a different version of MySQL than you are using now? The way certain things are stored changed over the past few MySQL updates. For example, namepros member DanR can tell you that DECIMAL changed the way it works as of MySQL 5.0.3! http://www.namepros.com/programming/...-pay-25-a.html

2. Why are you altering the new table's primary key type? If you look at http://dev.mysql.com/doc/refman/5.0/...ric-types.html , you'll see that BIGINT requires 8 bytes, where INT only requires 4. MEDIUMINT requires 3, SMALLINT requires 2, and TINYINT only 1!

3. Have you tried using ANALYZE TABLE? This is a dead giveaway if PHPMyAdmin is saying that the cardinality of your indexes is "none". See if that helps.

Other than that, it may be helpful if you most the structures of your old and new tables here. Use PHPMyAdmin to dump the structure of both tables and post it here...
__________________
The mass purge has begun.
moondog is offline  
Old 04-30-2007, 11:34 AM   #4 (permalink)
NamePros Member
 
Join Date: Apr 2005
Posts: 116
134.00 NP$ (Donate)

mikesherov will become famous soon enoughmikesherov will become famous soon enough


If you have data that was created before MySQL 5.0.3 but was then upgraded, the previous data behaves like normal until it's reused or inserted into a new table.

The data can be on the same server and exist in two states!
mikesherov is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 12:35 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85