How to effeciently create index in mysql - mysql

I want to create an Index on Id of my table. It's a bigint.
I would like to know what is the most good to have this index so that I can retrieve the data as fast as possible.
For example :
CREATE INDEX id_index ON lookup (id) USING BTREE;

Wit respect, you're overthinking this. It happens that InnoDB and MyISAM tables only support BTREE indexes. If you have MEMORY table you could create the index USING HASH but this is probably an ordinary table.
CREATE INDEX id_index ON lookup (id);
gets you what you want.

Related

What is the best practice to improve table performance?

In order to maintain tables and indexes efficiency it is not recommended to directly run "OPTIMYZE TABLE" (as stated in the link below), so is it best practice to do so in the following exact order?
DROP foreign keys (in order to drop related indexes)
DROP indexes (composite and non composites)
Perform OPTIMIZE TABLE
ADD INDEXES
ADD FOREIGN KEYS
ANALYZE TABLE
I believe this last step (analyze table) is also useful in order to update indexes cardinality within the information_schema.statistics table.
Any correction or further suggestions on this are welcome!
Thanks
link: https://www.percona.com/blog/2010/12/09/mysql-optimize-tables-innodb-stop/
example:
alter table T1 DROP index i1 ;
alter table T1 DROP index i2 ;
optimize table T1 ;
alter table T1 ADD index i1(c1) ;
alter table T1 ADD unique index i2(c1, c2) ;
analyze table T1 ;
Did you perceive any improvement? I doubt it. In most situations, InnoDB takes care of keeping its BTrees clean "enough". Running an OPTIMIZE TABLE provides only a minor improvement for a short period of time.
There are other things to look at. Examples:
index i1(c1) is unnecessary since you also have unique index i2(c1, c2).
If you have PRIMARY KEY(id) consider upgrading that unique index to PRIMARY KEY(c1, c2).
FOREIGN KEYs cost something on every insert; do you really need them?
Are you blindly using BIGINT when perhaps SMALLINT would suffice?

What does changing the primary key of a table incur in MySQL?

In MySQL,
whenever I change the primary key of a table, is it correct that the original index on the original primary key will be removed, and a new index on the new primary key will be created?
is an index based on a primary key always clustered? If yes. when changing the primary key of a table, are the records in the table to be moved to be stored in the order of the new primary key?
Thanks.
MySQL's default storage engine is InnoDB. InnoDB always stores a table as a clustered index, using the primary key as the clustered index. See https://dev.mysql.com/doc/refman/8.0/en/innodb-index-types.html for details on this.
If you change the columns defined for your table's primary key, like the following for example:
ALTER TABLE MyTable DROP PRIMARY KEY, ADD PRIMARY KEY (id2);
This will require all the pages of that table to be copied to a new layout, using the newly defined primary key as the clustered index.
This isn't the only operation that requires a table-copy. Any ALTER TABLE that changes the size of a row will perform a table-copy. E.g. adding/dropping a column, changing a data type (with some exceptions), changing nullability of a column, etc. See https://dev.mysql.com/doc/refman/8.0/en/innodb-create-index-overview.html for details
P.S.: I don't bother to answer about MyISAM storage engine anymore. It's on its way to being deprecated. The sooner people stop considering MyISAM as a viable option, the better.
To answer the questions you asked:
1) Yes, a new index will be created for the new primary key.
2) For tables using the InnoDB storage engine, yes, the table will be reorganized with the new primary key as the cluster key (rows will be stored in index order by primary key; the table itself is organized as an index.) For tables using MyISAM storage engine, no.

Index in partitioned table

How MySQL create index for a partition table, Example if I create 5 hash by ID partitions:
Create 1 global index for all data and 5 partitions will use this index
Create 5 partitioned index with subdata in 5 partitioned tables
Create 5 index with all data in 5 partitioned tables
Thanks
There is no "global" index for a partitioned table in MySQL.
The only indexes you can put on a partitioned table ends up being separate indexes on each partition. Each partition is effectively an independent table.
HASH partitioning is virtually useless; do you have a particular use for which you think it might be beneficial?
Addenda...
The size of the index is similar to that of a table.
Since there are no "global" indexes, you cannot have a UNIQUE key unless it includes the column(s) of the "partition key". Nor can you use FOREIGN KEYs.
There is no type of index that spans more than one table.
Partitioned table in Mysql has only local indexes support.
What does that mean? Every partition of the table stores its own B-Tree for the indexes. This can slow down the process of search if you don't have partition key as part of the index. Also for unique key constraint, you need to add partition key as part of the unique key.
Compared to Mysql, Oracle has concept of Global indexes as well. Global Indexes are very hard to manage.
I am not too sure How helpful Mysql partitions would be if it has ignored Global indexes.

does a primary key speed up an index?

Aside from the convenient auto-increment and UNIQUE features, does the PK actually speed up the index?
Will the speed be the same whether it's a non-PKed indexed INT or PKed (same column, two different tests)? If I had the same column on the same table on the same system, will it be faster if a UNIQUE INT column with an index also has PK enabled? Does PK make the index it coexists with faster?
Please, actual results only with system stats if you could be so kind.
The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values. With the InnoDB storage engine, the table data is physically organized to do ultra-fast lookups and sorts based on the primary key column or columns.
If your table is big and important, but does not have an obvious column or set of columns to use as a primary key, you might create a separate column with auto-increment values to use as the primary key. These unique IDs can serve as pointers to corresponding rows in other tables when you join tables using foreign keys.
Also refer the following locations : http://www.dbasquare.com/2012/04/04/how-important-a-primary-key-can-be-for-mysql-performance/ and http://www.w3schools.com/sql/sql_primarykey.asp
Rows in a base table are uniquely identified by the value of the primary key defined for the table. The primary key for a table is composed of the values of one or more columns.
Primary keys are automatically indexed to facilitate effective information retrieval.
The primary key index is the most effective access path for the table.
Other columns or combinations of columns may be defined as a secondary index to improve performance in data retrieval. Secondary indexes are defined on a table after it has been created (using the CREATE INDEX statement).
An example of when a secondary index may be useful is when a search is regularly performed on a non-keyed column in a table with many rows, defining an index on the column may speed up the search. The search result is not affected by the index but the speed of the search is optimized.
It should be noted, however, that indexes create an overhead for update, delete and insert operations because the index must also be updated.
Indexes are internal structures which cannot be explicitly accessed by the user once created. An index will be used if the internal query optimization process determines it will improve the efficiency of a search.
SQL queries are automatically optimized when they are internally prepared for execution. The optimization process determines the most effective way to execute each query, which may or may not involve using an applicable index.

Why does MySQL use a temporary table to drop a primary key?

When using the command:
ALTER TABLE my_table DROP PRIMARY KEY;
The state (when SHOW PROCESSLIST) appears as:
copy to tmp table
Why would it need to use a tmp table to "drop" a primary key constraint?
Consider the case of a composite primary key. In this case, the DB engine has to create a new clustered index from a synthetic key, which will require moving rows around. (Keep in mind that rows are physically ordered on disk by the primary key.) Given the rarity of this situation, it's not really worth handling the special case where your primary key is already an integer.