Home  >  Q&A  >  body text

mysql优化 - mysql KEY是什么键?有什么作用?

1.表

CREATE TABLE jforum_posts (
post_id int(11) NOT NULL AUTO_INCREMENT,
topic_id int(11) NOT NULL DEFAULT '0',
forum_id int(11) NOT NULL DEFAULT '0',
user_id int(11) NOT NULL DEFAULT '0',
post_time datetime DEFAULT NULL,
poster_ip varchar(15) DEFAULT NULL,
enable_bbcode tinyint(1) NOT NULL DEFAULT '1',
enable_html tinyint(1) NOT NULL DEFAULT '1',
enable_smilies tinyint(1) NOT NULL DEFAULT '1',
enable_sig tinyint(1) NOT NULL DEFAULT '1',
post_edit_time datetime DEFAULT NULL,
post_edit_count int(11) NOT NULL DEFAULT '0',
status tinyint(1) DEFAULT '1',
attach tinyint(1) DEFAULT '0',
need_moderate tinyint(1) DEFAULT '0',
PRIMARY KEY (post_id),
KEY user_id (user_id),
KEY topic_id (topic_id),
KEY forum_id (forum_id),
KEY post_time (post_time),
KEY need_moderate (need_moderate)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8

2.问题

这里的KEY是什么键?是外键吗?如果不是外键,它有什么用?
KEY user_id (user_id),
KEY topic_id (topic_id),
KEY forum_id (forum_id),

PHPzPHPz2742 days ago664

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 15:00:50

    Key is to add an index to the table. The function of the index is to increase the query speed of the database. The database engine of MySQL is InnoDB, which uses two data structures, B+ tree and hash, to add indexes to the table.
    EXPLAIN SELECT * FROM idc_work_order_main WHERE id = '100' Then use explain to check whether the current SQL statement uses an index and how to use the index.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:00:50

    Key is an index constraint, which constrains the index on the fields in the table. The purpose of
    key: mainly to speed up the query.
    The statement here:
    KEY user_id (user_id),
    KEY topic_id (topic_id),
    KEY forum_id (forum_id),
    refers to creating an index for the three fields user_id, topic_id, forum_id
    The difference between key, primary key, unique key and index in mysql

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:00:50

    Index, increase query speed

    reply
    0
  • Cancelreply