Mysql / Django - Begin auto increment to 1 - mysql

I have a django script which loads data, the beginning of the script deletes all datas in database.
So when I execute 1st time this script, the auto increment primary keys begin to 1 to 15 (if 15 objects) and if I want to reload data, I reexecute the script.
My issue is when I execute it again, pks numbers begin to 16 (for 2nd launch), I would like each time auto_increment begins to 1, is it possible whitout regenerating tables structure each time ?
Thanks

You could use ALTER TABLE, but I'm not sure that is that much better than just regenerating the schema.
ALTER TABLE table AUTO_INCREMENT = 1;

When you delete rows from the database, you're not really:
Freeing up the disk space
Resetting the auto_increment values as you've noticed.
As such, it might actually be a better idea to drop the table and re-create it as required. Failing that you can use just either:
TRUNCATE <table name>; (Depending on your storage engine, this will actually drop/re-create as mentioned above for you.)
ALTER TABLE <table name> SET AUTO_INCREMENT = X;
Of these, I'd recommend using the truncate approach.

Related

What is the best and safest method to update/change the data type of a column in a MySQL table that has ~5.5 million rows (TINYINT to SMALLINT)?

Similar questions have been asked, but I have had issues in the past by using
ALTER TABLE tablename MODIFY columnname SMALLINT
I had a server crash and had to recover my table when I ran this the last time. Is it safe to use this command when there is that much data in the table? What if there are other queries that may be running on the table in parallel? Should I copy the table and run the query on the new table? Should I copy the column and move the data to the new column?
Please let me know if there are any best or "safest" practices when doing this.
Also, I know this depends on a lot of factors, but does anyone know how long the query should take on an InnoDB table with ~5.5 million rows (rough estimate)? The column in question is a TINYINT and has data in it. I want to upgrade to a SMALLINT to handle larger values.
Thanks!
On a slow disk, and with lots of columns in the table, it could take hours to finish.
The ALTER is "safe" because it used to do the following:
Lock the table
Create a similar table, but with SMALLINT instead of TINYINT.
Copy all the rows over to the new table.
Rename the tables and drop the old one.
Unlock
Step 3 is the slow part. The only vulnerability is in step 4, which is very fast.
A server crash during steps 1-3 should have left the old table intact, but possibly left behind a partially created tmp table named something like #sql....
Percona's pt-online-schema-change has the advantage of being virtually lockless.
This cannot be easily answered.
It depends on things like
Has the table its own file, or is it shared with others?
How big is the table in terms of bytes?
etc.
It can last from some minutes to, indeed, some hours and can involve copying over the whole content of the table, so you have quite big needs of disk space.
You can add a new SMALLINT column to the table:
ALTER TABLE tablename ADD columnname_new SMALLINT AFTER columnname;
then copy the data from old column to new one:
UPDATE tablename SET columnname_new = columnname WHERE columnname_new IS NULL LIMIT 100000
repeat above until all records done
then you can drop old column:
ALTER TABLE tablename DROP COLUMN columnname
and finally rename new column:
ALTER TABLE tablename CHANGE columnname_new columnname SMALLINT
you could do the copy of values from old column to new column in batch of 100000 rows, just to be sure not to have any issue
I would add a new column, change the code to check if a value exists in the new column and to read/write it if it does. Also change the code to read from the old column and write to the new column. At this point you can migrate the data at will, copying over values from the old column into the new column where a value does not exist in the new column.
Once all of the data has been migrated you can drop the old column.

Resetting data in MySql database

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

How do I remove redundant Primary Keys from a MYSqL Table?

Ihave been developing an app for some time. This involves entering and deleteing alot of useless data in the tables. Now that I want to go to production I want to get rid of all the data but also restore all the 'IDs' ( primary keys ) to 0 so that the live system can start fresh with sensible ID's like 1,2,3 etc.
Using MySQL and PHP / Codeigniter
Many Many Thanks for yoru help !
I would normally use TRUNCATE - this both removes the data and resets the AUTO_INCREMENT.
Note that MySQL will perform a row by row deletion if there is a foreign key relationship, which is quite convenient (compared to SQL Server).
If your pk is autoincrement, you can do
ALTER TABLE tbl AUTO_INCREMENT =1
Make sure table is empty before executing the query.

create an index without locking the DB

I have a table with 10+ million rows. I need to create an index on a single column, however, the index takes so long to create that I get locks against the table.
It may be important to note that the index is being created as part of a 'rake db:migrate' step... I'm not adverse to creating the index manually if that will work.
UPDATE: I suppose I should have mentioned that this a write often table.
MySQL NDBCLUSTER engine can create index online without locking the writes to the table. However, the most widely used InnoDB engine does not support this feature. Another free and open source DB Postgres supports 'create index concurrently'.
you can prevent the blockage with something like this (pseudo-code):
create table temp like my_table;
update logger to log in temp;
alter table my_table add index new_index;
insert into my_table select * from temp;
update logger to log in my_table;
drop table temp
Where logger would be whatever adds rows/updates to your table in regular use(ex.: php script). This will set up a temporary table to use while the other one updates.
Try to make sure that the index is created before the records are inserted. That way, the index will also be filled during the population of the table. Although that will take longer, at least it will be ready to go when the rake task is done.

Change the step auto_increment fields increment by

How do I change the amount auto_increment fields in MySQL increment by from the default (1) to n?
If you want to change autoincrement step from 1 to N then there is a solution.
It could be done on MySQL server side:
look for '--auto-increment-increment' startup option or use following command SET ##auto_increment_increment=2;, but be warned that this is a server wide change (all tables will increment by 2).
Unortodox solutions could that could be considered:
Launch two MySQL servers on same machine, with different ports (one with auto_increment_increment=1 other with auto_increment_increment=2)
Use some serverside magic (PHP, ASP ,???) combined with turning off tables auto_increment to manually calculate (simple peek at last id and +=2 would be ok) and provide id in INSERT query.
Some official MySQL FAQ
You can change it using ALTER TABLE:
ALTER TABLE table AUTO_INCREMENT = n;
Or if you want to do set it from start:
CREATE TABLE table (...) AUTO_INCREMENT = n;
You can also use
ALTER SEQUENCE sequence_name INCREMENT BY N where N is the new incremnent value.
alter table <table name> auto_increment=n
where n is the number you want to start