Create dynamic add indexes script in MySQL - mysql

I have generated a drop indexes script that pulls from information_schema.statistics to drop the indexes dynamically. It works properly.
However I have been unable to think of a way to recreate the indexes (based on a temp table created by drop indexes script).
For example:
Table Food exists with 3 columns, id is indexed, and there exists a multi-column index of name and category.
My drop script drops the indices [after storing them in a temp table food_temp_indexes]. I then would like to re-generate the indexes, including the multi-column index as well as the id index dynamically. Whether there is one index or 5, varying from a simple index to a primary key to a multi-column, I wish to dynamically re-add these indexes.
The idea is to drop indexes before inserting millions of records, insert them, then recreate the indexes.

Related

Does it make sense to optimize the table after alter table drop column?

I dropped the column name in table employees.
If I run OPTIMIZE TABLE employees, will it reduce space usage?
My thoughts:
The documentation says that optimize table is equal to alter table for InnoDB (if I read this https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html#optimize-table-innodb-details correctly).
Also, alter table drop column changes rows structure in the table, so it should rewrite all rows. This is where, I assume, optimization happens.
It's not necessary to OPTIMIZE TABLE on an InnoDB table after running any ALTER TABLE that changes the row size.
The ALTER TABLE copies rows into a new tablespace, and rebuilds indexes. This will accomplish the same defragmentation you hoped to do with OPTIMIZE TABLE.

Does an index create a new table?

I’m having trouble understanding exactly what happens when I create an index. So if I create an index on (Col1, Col2). Will there be a new table created containing only Col1 and Col2 which my query will run on?
Or do I specify that I’m using an index in my query and then for that query and that query only my table is now reduced to (Col1, Col2)?
Thanks
When you create an index on a table, internally it doesn't create any new table but just index. This index is physically separate and has data organized like a tree (usually B+ tree) which helps in faster lookups and speeds up the queries that have where clause having the column name on which index are created. If you don't create indexes on table then the queries on that table may have to do full table scan in order to find records. Indexes can be unique or non unique.

How to load a huge data set into newly created table?

I'm trying to FULLTEXT index into my table. That table content 3 million records.It was very difficult to insert that index using Alter table statement or Create index statement. Therefor easiest way to create new table and 1st add index and load the data. How can I load existing table data into newly created table? I'm using Xammp MySql database.
I don't know why creating a full text index on an existing table would be difficult. You just do:
create fulltext index idx_table_col on table(col)
Usually, it is faster to add indexes to already loaded tables than to load data into an empty table that has indexes pre-defined.
EDIT:
You can do the load by using insert. The following will insert the first 100,000 rows:
insert into newtable
select *
from oldtable
order by id
limit 0, 100000;
You can put this in a loop (via a stored procedure in MySQL or at the application level). Perhaps this will return faster. Each time you run it, you would change the offset value in limit.
I would expect that the overall time for creating an index would be less than using insert, but for your purposes, you might find this more convenient.
INSERT INTO newTable SELECT * FROM oldTable;
After your new table and index on it is created.
This is given you want to copy all columns. You can select specific columns as well.

MySQL Create table with two indexes on same column(s)

Is it possible to create two indexes with names different on the same column?
Yes, you can, but why would you do that?
Unless the indexes are different in some way, for example if there are additional columns, or differences in the order of the columns in the indexes, a second duplicated index would be redundant.
Each additional index on a table requires more disk storage (slight cost increase), and also means more data needs to be written when inserting, updating or deleting data (slightly slower writes).
But yes, it is possible, and the syntax is one would expect, e.g. given the table:
CREATE TABLE T1
(
col1 INT,
col2 INT
);
CREATE INDEX IX1 on T1(col1);
CREATE INDEX IX2 on T1(col1);
SQL Fiddle here

phpMyAdmin wants to add multiple indices as one multi-column index

I'm creating tables using phpMyAdmin and want to define two different columns as indices. I'm not trying to create a multi-column index but phpMyAdmin creates them as such. Are there any possible issues with that? The fields don't relate to each other directly and both fields will not be used in WHERE clauses simultaneously.
Consider:
ALTER TABLE `documents` ADD INDEX (`offer_number`, `contract_number`);
And:
ALTER TABLE `documents` ADD INDEX (`offer_number`);
ALTER TABLE `documents` ADD INDEX (`contract_number`);
What's the difference?
MySQL can only make use of an index if the first column(s) of the index match the columns used in the query. In other words, if you perform a query where an index on contract_number could be used, the composite index won't be used since contract_number is not the first column in that key. The composite index could be used for a query where offer_number is used, however.
http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
Given what you say about these fields, they should not be a part of one multi column index.
If you want to create single column indexes on PhpMyAdmin, you need to create them one at a time.