I create a table which entried over 60milion rows.Everytime i query one row,it cost me 2minutes.how to speed up query these data.dudes.enter image description here
if you haven't done Indexing on Phone column then please do it, it make faster
like
CREATE INDEX info_phone_index ON Info (phone);
Syntax
CREATE INDEX index_name ON table_name (column1, column2, ...);
Then run your query,you will get faster results.
Related
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;
If I make the following index on InnoDB table:
CREATE INDEX index_name on table_name (col1, col2)
and perform this select query:
SELECT col2 FROM table_name WHERE col1=some_value
will MySQL retrieve col2 straight from index?
(What I'm trying to achieve is to drastically speed up the selection process)
It should. That's called index-only scan.
But to be sure, you should examine the query execution plan.
I don't think MySQL will use index automatically, though SQLite will do. Try:
SELECT col2 FROM table_name use index (col1,col2) WHERE col1=some_value
I have a query like
SELECT * FROM Table_name WHERE column1 = '1' AND column2 IN ('1','2','3');
And index exists on (column1, column2, column3). Is my above query used index I have created or not? Basically I am confused with the IN keyword, without this it is using, but with IN I am not sure. Please explain me.
MySQL can use indexes with IN conditions. If you only have an index on column2, it will most likely be used. If you have indexes on each of column1 and column2, only one of them can be used, and the query planner will have to decide which one seems better for a particular query. If you have a composite index on (column1, column2) then it should be able to use that index to match both columns in the WHERE clause.
MySQL is capable of using indices with IN .. now, is MySQL using indices in the particular query? Well, ask the query planner!
In this case an index over (column1, column2, ..) could be used - because all the leftward components have been satisfied. That is, an index seek could be done on column1 (for =), and then column2 (for IN). But again, ask the query planner as unexpected plans are not unheard of; just because the query planner could choose an index doesn't mean that it will.
See EXPLAIN, for how to ask:
When EXPLAIN is used with an explainable statement, MySQL displays information from the optimizer about the statement execution plan. That is, MySQL explains how it would process the statement, including information [like index usage] about how tables are joined and in which order ..
.. With the help of EXPLAIN, you can see where you should add indexes to tables so that the statement executes faster by using indexes to find rows.
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.
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.