I have an application with a MySQL Database. The application is currently under testing. After testing I want to delete all testing data in the database(but keep all tables as it is). I want to refresh all tables in the DB. If i just delete values from the DB, the AUTO_INCREMENT columns do not start from 1.
How do I refresh the DB so all values are gone?
You just need to truncate all your tables.
after truncating all your Auto Increment starts from 1 instead of where you left.
here is the link for more info
TRUNCATE TABLE table_name;
Related
Hi I have mysql rds database server and one of my table is having 3 million+ records when I am trying to add new column it's always failing and giving following error
Query ALTER TABLE user_notifications ADD program_id int(11);
Error Temporary file write failure.
My RDS DB instance is db.t2.medium
ALTER TABLE is a table re-creation, so two copies of the table will exist on the system at some stage during the process.
So, you will need free space more than data length size for this operation.
You can check data length in table info.
There is this alternate solution, in which you create a new table with new desired schema, and move data from old table to new table in chunks.
MySQL will re-create the table when using an alter so you need enough free space on the disk for another copy of the table.
I suspect the table is large as it has 3million records and your disk does not have sufficient space
This article shows how to determine how much disk space a tables are using
I'm trying to run a query into a table in our MySQL database, table is called 'ac_userdata'. Whenever i run a query or update or anything, it just constantly loads and eventually (after ~5 minutes) it times out. I tried dropping the table and making a new one, but i can't even drop it. I checked to see if the table was locked, and even unlocked all tables in the database, no luck. No other table in the database and on the server has this problem, and this is one of the smallest tables in the database...
Any ideas?
Thanks!
Erouax
If there is only one table in database or you have tried to drop the table then why you don't drop the database delete database and create a new one if your current database have no data.
I am making bunch of INSERT ON DUPLICATE KEY UPDATE to a table filled with data.
I need to fill table with that data AND remove data that I haven't filled (I mean remove rows that was not mentioned in my INSERTs).
What I tried and what was working:
create new timestamp column in table
During INSERTs insert or update this column with CURRENT_TIMESTAMP, so that all rows I touched have newest timestamps
Run delete query that deletes all rows that are older than the starting time of my script.
This idea works perfectly, but there is one problem: my replication binary log get filled with unnececary data on both modes (ROW and STATEMENT). I don't need that timestamps at all to be replicated...
I don't want to do TRUNCATE TABLE before inserts because my app should deliever a non-stop access to data (old or new). If I do TRUNCATE TABLE tables can be without data for some time.
I can also save all primary key values that I insert in scripts memory or temporary table, and then delete the rows that are not in that table, but I hope there is a more optimized and clever way to do that.
Do you have any idea how can I achieve that goal so I can update data, delete only untouched rows and replicate only changes (I guess in ROW mode)?
I'm not very familiar with replication binary logs, sorry in advance if won't work. I assumed that logging can be set differently for tables.
I would do the following:
create a table for the new data with the same primary key column with
the old table
delete all rows from old table where not found in the new table
update rows in the old table according to the new table
This way wouldn't be unnecessary log inserts.
This assumes that you have the required space in the server, but can work.
I am faced with a scenario where I would like to remove all existing data from one of my tables and then reuse the table again and fill it with new data.
My Question: Should I destroy/recreate the exact same table to do this? Or is there an easier way? -Side Note: I am storing user id's and would like for the id's to be set back again as opposed to continuing at the number in which last data was stored.
I'm using PhpMyAdmin.
If you use TRUNCATE TABLE.
TRUNCATE TABLE empties a table completely. Logically, this is equivalent to a DELETE statement that deletes all rows, but there are practical differences under some circumstances.
...
it resets any AUTO_INCREMENT counter to zero
So if you want to keep the counter use DELETE FROM
If you delete all rows in the table with DELETE FROM tbl_name (without a WHERE clause) in autocommit mode, the sequence starts over for all storage engines except InnoDB and MyISAM.
You can also look here: How to prevent mySQL from resetting auto increment value?
You have to truncate your table, see documentation here: https://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
In phpmyadmin there's a button in the table list called: Empty
I have a MySQL database that is up to about 17 GB in size and has 38 million entries. At the moment, I need to both increase the size of one column (varchar 40 to varchar 80) and add more columns.
Many of the fields are indexed including the one that I need to change. It is part of a unique pair that is necessary for the applications to work. In attempting to just make the change yesterday, the query ran for almost four hours without finishing, when I decided to cut our outage and just bring the service back up.
What is the most efficient way to make changes to something of this size?
Many of these entries are also old and if there is a good way to sort of shard off entries but still have them available that might help with this problem by making the table a much more manageable size.
You have some choices.
In any case you should take a backup before you do this stuff.
One possibility is to take your service offline and do it in place, as you have tried. If you do that you should disable key checks and constraints.
ALTER TABLE bigtable DISABLE KEYS;
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE (whatever);
ALTER TABLE (whatever else);
...
SET FOREIGN_KEY_CHECKS=1;
ALTER TABLE bigtable ENABLE KEYS;
This will allow the ALTER TABLE operation to go faster. It will regenerate the indexes all at once when you do ENABLE KEYS.
Another possibility is to create a new table with the new schema you want, then disable the keys on the new table, then do as #Bader suggested and insert the contents of the old table.
After your new table is built you will re-enable the keys on it, then rename the old table to some name like "old_bigtable" then rename the new table to "bigtable".
It's possible that you can keep your service online while you're populating the new table. But that might work poorly.
A third possibility is to dump your giant table (to a flat file) and then load it to a new table with the new layout. That is pretty much like the second possibility except that you get a table backup for free. You can make this go pretty fast with SELECT DATA INTO OUTFILE and LOAD DATA INFILE. You'll need to have access to your server machine's file system to do this.
In all cases, disable, then re-enable, the constraints and keys to get things to go fast.
Create a new table with the new structure you want with a different name for example NewTable.
Then insert data into this new table from the old table using the following query:
INSERT INTO NewTable (field1, field2, etc...) SELECT field1, field2, ... FROM OldTable
After this is done, you can drop the old table and rename the new table to the original name
DROP TABLE `OldTable`;
RENAME TABLE `NewTable` TO `OldTable` ;
I have tried this approach on a very large table and it's much much faster than altering the table.
With MySQL 5.1 and again with 5.5 certain alter statements were enhanced to just modify the structure without rewriting the entire table ( http://dev.mysql.com/doc/refman/5.5/en/alter-table.html - search for in-place). The availability of this though varies by the type of change you are making and the engine in use, the most value comes from InnoDB Plugin. In the case of your specific changes though the entire table would be rewritten.
When we encounter these issues, we typically try to leverage replica databases. As long as you are adding and not removing you can run your DDL against the replica first and then schedule a brief outage for promoting the replica to the master role. If you happen to be on RDS this is even one of their suggested uses for their replica instances http://aws.amazon.com/about-aws/whats-new/2012/10/11/amazon-rds-mysql-rr-promotion/.
Some other alternatives include:
Selecting out a subset of records into a new table with the desired structure (use INTO OUTFILE to avoid a table lock). Once complete you can schedule a maintenance window and REPLACE INTO or UPDATE any records that have changed in the origin table since the initial data copy. Once the update is complete a RENAME TABLE... of both tables wraps the changes up.
Using a tool like Percona's pt-online-schema-change: http://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html. This tool works with triggers so if you already have triggers on the tables you want to change this may not fit your needs.