Is there a MySQL command drop all indexes except PRIMARY index?

为君一笑 提交于 2019-12-12 14:49:01

问题


I have a database table with one Index where the keyname is PRIMARY, Type is BTREE, Unique is YES, Packed is NO, Column is ID, Cardinality is 728, and Collation is A.

I have a script that runs on page load that adds entries to the MySQL database table and also removes duplicates from the Database Table.

Below is the script section that deletes the duplicates:

// Removes Duplicates from the MySQL Database Table

   // Removes Duplicates from the MySQL Database Table based on 'Entry_Date' field
      mysql_query("Alter IGNORE table $TableName add unique key (Entry_Date)");

   // Deletes the added index created by the Removes Duplicates function
      mysql_query("ALTER TABLE $TableName DROP INDEX Entry_Date");

Using the Remove Duplicates command above, an additional index is added to the table. The next line command is suppose to delete this added index.

The problem is that sometimes the added index created by the Removes Duplicates command does not get deleted by the following Delete added index command and therefore more indexes are added to the table. These additional indexes prevent the script from adding additional data to the database until I remove the added indexes by hand.

My Question: Is there a command or short function that I can add to the script that will delete all indexes except the original index mentioned in the beginning of this post?

I did read the following post, but I don't know if this is the correct script to use: How to drop all of the indexes except primary keys with single query


回答1:


I don't think so, what you can do is create copies but that wouldn't copy the index. for example if you make create table1 as (select * from table_2), he will make copy but without index or PK.




回答2:


After all the comments I think I realize what is happening. You actually allow duplicates in the database. You just want to clean them some times. The problem is that the method you have chosen to clean them is through creating a Unique key and using the IGNORE option which causes duplicate lines to get dropped instead of failing the unique key creation. then you drop the unique key so that duplicate rows can be added again. your problem is that sometimes the unique key is not being dropped.

I suggest you delete the duplicates in another way. supposing that your table name is "my_table" and your primary key is my_mey_column then:

delete from my_table where my_key_column not in (select min(my_key_column) from my_table group by Entry_Date)

Edit: the above won't work due to limitation in mysql as pointed by @a_horse_with_no_name

try the three following queries instead:

create temporary table if not exists tmp_posting_data select id from posting_data where 1=2 

insert into tmp_posting_data(id) select min(id) from posting_data group by Entry_Date

delete from Posting_Data where id not in (select id FROM tmp_posting_data)

As a final note, try to reconsider the need to allow the rows to be duplicated also as suggested by @a_horse_with_no_name. instead of allowing rows to be entered and then deleted, you can create the unique key once in the database like:

Alter table posting_data add unique key (Entry_Date)

and then, when you are inserting new data from the RSS use the following instead of "insert" use "replace" which will delete the old row if it is a duplicate on the primary key or any unique index

replace into posting_data (......) values(.....)


来源:https://stackoverflow.com/questions/10180653/is-there-a-mysql-command-drop-all-indexes-except-primary-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!