Mysql Indexing is not working on a particular table

不问归期 提交于 2019-12-24 18:44:48

问题


I have a large set of data in the MySQL version 5.6 And Suppose name columnA, columnB, & columnC.

columnA(bigint) columnB(bigint) Time(timestamp)

Where I have applied BTREE UNIQUE INDEXING.

But When I explaining my query, I am getting all rows traversed.

I have tried to Analyze, Repair table but no luck.

Note:

In different tables, I am getting expected result after applying to index.

The size of columnA(bigint 20) where I am having an issue, is more than 50,000.

I want one row traversed after indexing.

Mysql database storage shows 0B on Index.

CREATE TABLE TableA ( ColumnA bigint(20) NOT NULL DEFAULT '0', 
                      ColumnB bigint(20) NOT NULL DEFAULT '0', 
                      ColumnC bigint(20) NOT NULL DEFAULT '0', 
                      ColumnD bigint(20) NOT NULL DEFAULT '0', 
                      ColumnE timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `TableA`
  ADD UNIQUE KEY `idx_npp_pid` (`ColumnA`),
  ADD KEY `idx_npp_uid` (`ColumnD`);
COMMIT;

EXPLAIN SELECT `ColumnA` FROM `TableA` WHERE `ColumnA`=4444;

Explain Result:

id select_type    table    type    possible_keys   key    key_len    ref   rows   Extra 
1  SIMPLE         TableA   ALL     NULL            NULL   NULL       NULL  132244     Using where

回答1:


Mysql database storage shows 0B on Index.

InnoDB always needs a Primary Key, because it clusters the Primary Key with Data (and that is why it is also called as Clustered Index). That is why, there is no explicit disk storage for Index, when there is only Primary Key defined on an InnoDB table.

Now, if you don't explicitly define a Primary Key, InnoDB looks for the first Unique Key which is NOT NULL also, to be implicitly defined as Primary Key. If no such key is available, it creates a hidden Primary Key. So, it is always better to define a Primary Key. In your case, it has implicitly considered ColumnA as the primary key because it is UNIQUE and NOT NULL.

If no Natural Primary Key (combination) is available, then people generally advise to define a Surrogate Primary Key, using auto_increment.

Get more details here: https://dev.mysql.com/doc/refman/5.6/en/innodb-index-types.html

But When I explaining my query, I am getting all rows traversed.

You need to provide us your EXPLAIN.. plan result, to be able to come to some conclusion.



来源:https://stackoverflow.com/questions/58096726/mysql-indexing-is-not-working-on-a-particular-table

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