• The MySQL SHOW INDEX command


     

    For a long time I couldn't remember how to show the indexes for a database table with MySQL, but I think it's finally sunk in. Just use the MySQL SHOW INDEX command, like this:

    show index from node;
    

      

    That example can be read as, "Show the indexes from the database table named node". It assumes that you have already selected a database table, so really, a full sequence of commands to show a MySQL database table index looks like this:

    # log into your mysql database
    $ mysql -u root -p
    
    # once in mysql, select your database
    mysql> use drupal;
    Database changed
    

      

    # now show the index on the node table in the drupal database mysql> show index from node;
    

      

    Or, if you prefer, you don't have to switch to your database, you can include the name of the database in your show index query, like this:

    show indexes from node from drupal;
    

      

    Syntax variations

    As you may have noticed from that last example, you don't have to type show index; either of the following three commands will give you the same results:

    show index from node;
    show indexes from node;
    show keys from node;
    

      

    I actually prefer the second version (show indexes) because it's easier to read, and closely corresponds to how I'd like to say this as a human being: "Show the indexes from (the database table named) node".

    Output from MySQL SHOW INDEX

    Of course the output from the MySQL show index command will vary depending on the indexes that have been created on your table, but here's what it looks like when I run it on my Drupal term_datatable on my test server:

    +-----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table     | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +-----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | term_data |          0 | PRIMARY       |            1 | tid         | A         |        3224 |     NULL | NULL   |      | BTREE      |         | 
    | term_data |          1 | taxonomy_tree |            1 | vid         | A         |           2 |     NULL | NULL   |      | BTREE      |         | 
    | term_data |          1 | taxonomy_tree |            2 | weight      | A         |           2 |     NULL | NULL   |      | BTREE      |         | 
    | term_data |          1 | taxonomy_tree |            3 | name        | A         |        3224 |     NULL | NULL   |      | BTREE      |         | 
    | term_data |          1 | vid_name      |            1 | vid         | A         |           2 |     NULL | NULL   |      | BTREE      |         | 
    | term_data |          1 | vid_name      |            2 | name        | A         |        3224 |     NULL | NULL   |      | BTREE      |         | 
    +-----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    6 rows in set (0.00 sec)
    

      

    The key_name column shows the names of the indexes on the table, so in this case, my term_data table has the following indexes:

    • PRIMARY
    • taxonomy_tree
    • vid_name

    As its name implies, the PRIMARY key is the key on the primary field of the table. The Seq_in_indexcolumn tells you the sequence of each field in the index, so the taxonomy_tree index is a compound index, with the following three fields in the key:

    • vid
    • weight
    • name
     

    Another way to see MySQL indexes

    You can confirm this with another MySQL SHOW command, a command which also lets you see the indexes on a MySQL database table. This time we use the show create table syntax, which looks like this when run on the term_data table:

    show create table term_data;
    

      

    Here's the output from that command:

    term_data | CREATE TABLE `term_data` (
      `tid` int(10) unsigned NOT NULL auto_increment,
      `vid` int(10) unsigned NOT NULL default '0',
      `name` varchar(255) NOT NULL default '',
      `description` longtext,
      `weight` tinyint(4) NOT NULL default '0',
      PRIMARY KEY  (`tid`),
      KEY `taxonomy_tree` (`vid`,`weight`,`name`),
      KEY `vid_name` (`vid`,`name`)
    ) ENGINE=MyISAM AUTO_INCREMENT=3228 DEFAULT CHARSET=utf8 
    

      

    As you can see from this line of that output:

    KEY `taxonomy_tree` (`vid`,`weight`,`name`),
    

      

    the taxonomy_tree index has the three fields in the sequence shown earlier.

     

    Help remembering these commands

    If (like me) you have trouble remembering these MySQL SHOW commands, it may be easier to just remember this MySQL help command:

    help show;
    

      

    In MySQL 5.x, this help command shows the following output, including the two commands I've covered in this article:

    mysql> help show;
    Name: 'SHOW'
    Description:
    SHOW has many forms that provide information about databases, tables,
    columns, or status information about the server. This section describes
    those following:
    
    SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [LIKE 'pattern']
    SHOW CREATE DATABASE db_name
    SHOW CREATE FUNCTION funcname
    SHOW CREATE PROCEDURE procname
    SHOW CREATE TABLE tbl_name
    SHOW DATABASES [LIKE 'pattern']
    SHOW ENGINE engine_name {LOGS | STATUS }
    SHOW [STORAGE] ENGINES
    SHOW ERRORS [LIMIT [offset,] row_count]
    SHOW FUNCTION CODE sp_name
    SHOW FUNCTION STATUS [LIKE 'pattern']
    SHOW GRANTS FOR user
    SHOW INDEX FROM tbl_name [FROM db_name]
    SHOW INNODB STATUS
    SHOW PROCEDURE CODE sp_name
    SHOW PROCEDURE STATUS [LIKE 'pattern']
    SHOW [BDB] LOGS
    SHOW MUTEX STATUS
    SHOW PRIVILEGES
    SHOW [FULL] PROCESSLIST
    SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n]
    SHOW PROFILES
    SHOW [GLOBAL | SESSION] STATUS [LIKE 'pattern']
    SHOW TABLE STATUS [FROM db_name] [LIKE 'pattern']
    SHOW [OPEN] TABLES [FROM db_name] [LIKE 'pattern']
    SHOW TRIGGERS
    SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern']
    SHOW WARNINGS [LIMIT [offset,] row_count]
    
    The SHOW statement also has forms that provide information about
    replication master and slave servers and are described in [HELP PURGE
    MASTER LOGS]:
    
    SHOW BINARY LOGS
    SHOW BINLOG EVENTS
    SHOW MASTER STATUS
    SHOW SLAVE HOSTS
    SHOW SLAVE STATUS
    

      

    If the syntax for a given SHOW statement includes a LIKE 'pattern' part, 'pattern' is a string that can contain the SQL `%' and `_' wildcard characters. The pattern is useful for restricting statement output to matching values. Several SHOW statements also accept a WHERE clause that provides more flexibility in specifying which rows to display. See http://dev.mysql.com/doc/refman/5.0/en/extended-show.html. URL: http://dev.mysql.com/doc/refman/5.0/en/show.html

    As a final point of reference, here's a link to the MySQL SHOW INDEX documentation.

  • 相关阅读:
    【转】Eclipse中查看jar包中的源码
    maven No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    Android问题-DelphiXE8新建AVD出现“no system images installed for this target”
    Android教程-DelphiXE Android自适应屏幕办法
    教程-在F9后提示内存错误,点击了乎略,之后怎么取消乎略?
    PC问题-该虚拟机似乎正在使用中
    PC问题-(仅供备用)取消磁盘的自动扫描
    Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)
    Android教程-DelphiXE Android的所有权限按照分类总结说明
    C++问题-UniqueAppObject.cpp(147): error C3861: “GUXClientInit”: 找不到标识符
  • 原文地址:https://www.cnblogs.com/hephec/p/4586889.html
Copyright © 2020-2023  润新知