Problems creating an index - mysql

The question is:
Write the SQL statement to create an index on the city table for the attribute CountryCode. Name the index CountryCode_idx.
So my statement is:
CREATE INDEX city_name_countrycode_idx ON city (name, countrycode);
but the problem is, when I try to drop the index in the next question using statement
DROP INDEX city_name_countrycode_idx;
I get a syntax error expecting ON
and then I try making a statement called:
DROP INDEX city_name_countrycode_idx ON city;
but my statement affected 0 rows apparently. Can someone tell me if my create index statement is correct and why my drop index statement isn't working?

DROP INDEX city_name_countrycode_idx;
I get a syntax error expecting ON ...
As per documentation on DROP INDEX Syntax:
Name: 'DROP INDEX'
Description:
Syntax:
DROP INDEX index_name ON tbl_name
[algorithm_option | lock_option] ...
algorithm_option:
ALGORITHM [=] {DEFAULT|INPLACE|COPY}
lock_option:
LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE}
DROP INDEX drops the index named index_name from the table tbl_name.
Conclusion:
Hence to drop an index, you must use table name on which the index was created.
And for your index city_name_countrycode_idx to be dropped you have to use the table name city on which the said index is created.
DROP INDEX city_name_countrycode_idx ON city;

0 rows affected means no change in data . Although the index is dropped.
you can cross check by desc the table before and after dropping the index
Thanks

Related

Adding an Index to AP database

I am a CS student and I cannot figure out why my statement for adding an index will not work. To be specific, I am supposed to write a script that adds an index to the AP database for the zip code field in the Vendors table. I have attempted to do so using this statement:
CREATE INDEX (index_name) ON (table_name) ((column_name))
Am I missing something and I’m just not seeing it?
Try:
CREATE INDEX index_name ON table_name (column_name)
For example, to create index "i" on table "t" with column "c", you would do:
create index i on t (c)

How to create index for existing table in mysql?

Our server gets slow, "To get data from mysql database". So I search for it on google. They told me, "Use INDEX for the select query to get data from the database it becomes more fastest execution".
Index is a small copy of a database table sorted by key values.
U need to create index first.
CREATE INDEX index_name ON table_name(column_name)
Then:
SELECT * FROM table_name
USE INDEX (index_name)
WHERE condition;

How to/ Where to delete/edit an index from MySql which prevents duplicates row

I'm using SugarCRM and a few weeks ago I executed a a query on MySQL which created an index to prevent duplicate rows. Where can I see that or find it and edit or delete this ? I'm not able to remember the exact query but it's needed to add more columns. Using MySQL only just a few weeks.
MySQL error 1062: Duplicate entry 'example-dyplicate' for key
'idx_name'
To see the structure of a table, including all the indexes, use:
SHOW CREATE TABLE tablename;
You can delete an index with:
DROP INDEX indexname ON tablename;
There's no way to edit an index. If you want to change an index, you drop it and then add a new index with the new columns you want. However, you can do both in a single query using ALTER TABLE:
ALTER TABLE tablename DROP INDEX indexname ADD INDEX indexname (col1, col2, ...);

CREATE INDEX with condition?

I am using MySQL v5.1.
I would like to create index on a table by executing the following SQL statement:
CREATE INDEX index_name
ON table_name (column_name)
But, I wan to firstly check if the index on that column has already been created or not, if not, create it (otherwise do not create). What is the SQL syntax for this?
Mysql doesn't have IF NOT EXISTS for CREATE INDEX. You can work it out by querying information_schema.statistics table. Take a look here, there is an example of stored procedure that does what you are looking for (search for "CREATE INDEX IF NOT EXISTS" on the page)
You want SHOW INDEX.
To get all the indexes on a table:
SHOW INDEX FROM table_name
MySQL allows you to add a WHERE clause to limit the results as well.
Lock the table while you're checking to see if the index exists (and if it doesn't exist, creating the index) so that another process doesn't create the index right after you've checked for it but before you've created it yourself.

Optimize mySql for faster alter table add column

I have a table that has 170,002,225 rows with about 35 columns and two indexes. I want to add a column. The alter table command took about 10 hours. Neither the processor seemed busy during that time nor were there excessive IO waits. This is on a 4 way high performance box with tons of memory.
Is this the best I can do? Is there something I can look at to optimize the add column in tuning of the db?
I faced a very similar situation in the past and i improve the performance of the operation in this way :
Create a new table (using the structure of the current table) with the new column(s) included.
execute a INSERT INTO new_table (column1,..columnN) SELECT (column1,..columnN) FROM current_table;
rename the current table
rename the new table using the name of the current table.
ALTER TABLE in MySQL is actually going to create a new table with new schema, then re-INSERT all the data and delete the old table. You might save some time by creating the new table, loading the data and then renaming the table.
From "High Performance MySQL book" (the percona guys):
The usual trick for loading MyISAM table efficiently is to disable keys, load the data and renalbe the keys:
mysql> ALTER TABLE test.load_data DISABLE KEYS;
-- load data
mysql> ALTER TABLE test.load_data ENABLE KEYS;
Well, I would recommend using latest Percona MySQL builds plus since there is the following note in MySQL manual
In other cases, MySQL creates a
temporary table, even if the data
wouldn't strictly need to be copied.
For MyISAM tables, you can speed up
the index re-creation operation (which
is the slowest part of the alteration
process) by setting the
myisam_sort_buffer_size system
variable to a high value.
You can do ALTER TABLE DISABLE KEYS first, then add column and then ALTER TABLE ENABLE KEYS. I don't see anything can be done here.
BTW, can't you go MongoDB? It doesn't rebuild anything when you add column.
Maybe you can remove the index before alter the table because what is take most of the time to build is the index?
Combining some of the comments on the other answers, this was the solution that worked for me (MySQL 5.6):
create table mytablenew like mytable;
alter table mytablenew add column col4a varchar(12) not null after col4;
alter table mytablenew drop index index1, drop index index2,...drop index indexN;
insert into mytablenew (col1,col2,...colN) select col1,col2,...colN from mytable;
alter table mytablenew add index index1 (col1), add index index2 (col2),...add index indexN (colN);
rename table mytable to mytableold, mytablenew to mytable
On a 75M row table, dropping the indexes before the insert caused the query to complete in 24 minutes rather than 43 minutes.
Other answers/comments have insert into mytablenew (col1) select (col1) from mytable, but this results in ERROR 1241 (21000): Operand should contain 1 column(s) if you have the parenthesis in the select query.
Other answers/comments have insert into mytablenew select * from mytable;, but this results in ERROR 1136 (21S01): Column count doesn't match value count at row 1 if you've already added a column.