MySQL数据库调优基本命令使用

该博文主要总结了mysql的一些常用的命令,可以帮助我们进行数据库调优。该博文使用tpc-h生成的八个表格中的tb_lineitem,tb_order,tb_partsupp。更多关于tpc-h基准数据库的信息可以查看此博文

0.首先打开数据库

# 打开数据库服务
$ sudo /etc/init.d/mysql start
# 关闭数据库服务
$ sudo /etc/init.d/mysql stop

1.连接数据库

$ mysql -u root -p[password]
# 举例如下:
$ mysql -u root -pyyz

2.查看数据库,创建数据库,删除数据库

mysql> show databases;
mysql> create database [db_name]
mysql> drop database [db_name]
# 举例如下
mysql> create database db_test;
mysql> drop database db_test;

3.更改数据库

# 将[database name]替换成实际的数据库名字
mysql> use [database name];
# 例子如下, 博文中以db_store为例子
mysql> use db_store;

4.查看数据库中的表格

mysql> show tables;

5.查看表格的详细信息

mysql> desc [table name]
mysql> 将[table name]修改成对应的table名字,例子如下
mysql> desc tb_lineitem;

6.分析表格状态

mysql> analyze table tb_lineitem;

7.分析查询语句

mysql> explain select * from tb_lineitem;

PS: 在数据库调优中explain是一个很好的命令,它可以查看查询语句的执行状态。

8.查看语句的执行时间

mysql> set profiling=1;

开启存储语句执行时间的表profiles。

mysql> show profiles;

查看执行语句执行时间表的信息。

关注CPU和磁盘IO信息

mysql> show profile CPU, BLOCK IO for query [query ID]
# 将[query ID]换成对应查询的ID,举例如下
mysql> show profile CPU, BLOCK IO for query 1;

9.查看表格索引

mysql> show index from [table name];
# 将[table name]换成对应的表名,例子如下:
mysql> show index from tb_lineitem;

10.创建索引

mysql> create [fulltext] index [index name] on [table name]([column name,..])
# 将[index name], [table name],[column name]换成对应的索引名,表名,列名, 例子如下:
mysql> create index commit_index on tb_lineitem(l_commitdate);
mysql> create fulltext index commit_index on tb_lineitem(l_commitdate);

11.删除索引

mysql> drop index [index name] on [table name]
# 将[index name]和[table name]或称对应索引名,表名,例子如下:
mysql> drop index commit_index on tb_lineitem;

12.表的连接
(1) 内部连接:返回两个表对应连接的结果
(2) 外部连接
[1] 左连接:返回左表+左表与右表连接的结果
[2] 右连接:返回右表+左表与右表连接的结果

# 内部连接
mysql> select count(*) from tb_lineitem inner join tb_order on tb_lineitem.l_orderkey = tb_order.o_orderkey  where tb_order.o_orderkey > 500000

# 左连接
mysql> select count(*) from tb_lineitem left join tb_order on tb_lineitem.l_orderkey = tb_order.o_orderkey  where tb_order.o_orderkey > 500000

# 右连接
mysql> select count(*) from tb_lineitem right join tb_order on tb_lineitem.l_orderkey = tb_order.o_orderkey  where tb_order.o_orderkey > 500000

13.往下是一些基本的mysql语句

创建表

/* define table tb_lineitem */
CREATE TABLE IF NOT EXISTS `tb_lineitem`(
    `l_orderkey` int NOT NULL,
    `l_partkey` int NOT NULL,
    `l_suppkey` int NOT NULL,
    `l_linenumber` int NOT NULL,
    `l_quantity` decimal(15,2) NOT NULL,
    `l_extendedprice` decimal(15,2) NOT NULL,
    `l_discount` decimal(15,2) NOT NULL,
    `l_tax` decimal(15,2) NOT NULL,
    `l_returnflag` char(1) NOT NULL,
    `l_linestatus` char(1) NOT NULL,
    `l_shipdate` varchar(20) NOT NULL,
    `l_commitdate` varchar(20) NOT NULL,
    `l_receiptdate` varchar(20) NOT NULL,
    `l_shipinstruct` char(25) NOT NULL,
    `l_shipmode` char(10) NOT NULL,
    `l_comment` varchar(44) NOT NULL,
    PRIMARY KEY(`l_orderkey`, `l_linenumber`)
);
/* show tabel schema */
DESC `tb_lineitem`;

删除表:删除表的时候一定要确保表格不同哦,不然还得恢复,麻烦的要死。。。

mysql> drop table [table name]
# 举例如下
mysql> drop table tb_lineitem;

查询

mysql> select * from [table name] where [conditions];

更新

mysql> update [table name] set [set configuration] where [conditions];

插入

mysql> insert into [table name]([column1,column2,...]) values ([column1_value, column2_value,...]);

删除

mysql> delete from [table name] where [conditions];

修改表格

mysql> alter table [table name] add [字段 类型 其他];
# 举例如下:
mysql> alter table tb_lineitem add l_addition string not null;
mysql> alter table tb_lineitem add index commit_index(l_commitdate);

好啦今天大概就写这些啦,如果有需要还会继续更新滴!!!

一些调优博客
1.http://www.cnblogs.com/mliang/p/3637937.html
2.http://blog.csdn.net/zhu19774279/article/details/46473981
3.http://www.cnblogs.com/pcjim/articles/799302.html
4.http://www.cnblogs.com/tianhuilove/archive/2011/09/05/2167795.html
5.http://blog.csdn.net/u010179805/article/details/16926955
6.http://blog.csdn.net/xluren/article/details/32746183

猜你喜欢

转载自blog.csdn.net/nnuyi/article/details/78940636