查看mysql语句运行时间的2种方法
http://www.jb51.net/article/45185.htm
网站运行很慢的时候,我就特别起知道为什么这么慢,所以我查啊查,数据库绝对是很重要的一部分,里面运行的sql是绝对不能放过的。平时做项目的时候,我也会注意sql语句的书写,写出一些高效的sql来,所以我会经常测试自己写的sql语句。我把我知道的二个方法,总结一下发出来。
一,show profiles 之类的语句来查看
1,查一下profile是不是打开了,默认是不打开的。
mysql> show profiles; Empty set (0.02 sec) mysql> show variables like "%pro%"; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | profiling | OFF | | profiling_history_size | 15 | | protocol_version | 10 | | slave_compressed_protocol | OFF | +---------------------------+-------+ 4 rows in set (0.00 sec)
我查看一下profiles里面没有东西,所以公司的电脑里面profile是没有打开的,我查看了一下mysql变量,果然是OFF的。
2,开启profile,然后测试
开启profile
mysql> set profiling=1; Query OK, 0 rows affected (0.00 sec)
测试如下:
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | aa | | bb | | comment | | string_test | | user | +----------------+ 5 rows in set (0.00 sec) mysql> select * from aa; +----+------+------------+------+ | id | name | nname | sex | +----+------+------------+------+ | 2 | tank | bbbb,4bbbb | NULL | | 3 | zhang| 3,c,u | NULL | +----+------+------------+------+ 2 rows in set (0.00 sec) mysql> update aa set name='d'; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> delete from bb; Query OK, 2 rows affected (0.00 sec) mysql> show profiles; +----------+------------+------------------------+ | Query_ID | Duration | Query | +----------+------------+------------------------+ | 1 | 0.00054775 | show tables | | 2 | 0.00022400 | select * from aa | | 3 | 0.00026275 | update aa set name='d' | | 4 | 0.00043000 | delete from bb | +----------+------------+------------------------+ 4 rows in set (0.00 sec) mysql> show profile; +----------------------+-----------+ | Status | Duration | +----------------------+-----------+ | (initialization) | 0.0000247 | | checking permissions | 0.0000077 | | Opening tables | 0.0000099 | | System lock | 0.000004 | | Table lock | 0.000005 | | init | 0.0003057 | | query end | 0.0000062 | | freeing items | 0.000057 | | closing tables | 0.000008 | | logging slow query | 0.0000015 | +----------------------+-----------+ 10 rows in set (0.00 sec) mysql> show profile for query 1; +----------------------+-----------+ | Status | Duration | +----------------------+-----------+ | (initialization) | 0.000028 | | checking permissions | 0.000007 | | Opening tables | 0.0000939 | | System lock | 0.0000017 | | Table lock | 0.0000055 | | init | 0.000009 | | optimizing | 0.0000027 | | statistics | 0.0000085 | | preparing | 0.0000065 | | executing | 0.000004 | | checking permissions | 0.000258 | | Sending data | 0.000049 | | end | 0.0000037 | | query end | 0.0000027 | | freeing items | 0.0000307 | | closing tables | 0.0000032 | | removing tmp table | 0.0000275 | | closing tables | 0.0000037 | | logging slow query | 0.000002 | +----------------------+-----------+ 19 rows in set (0.00 sec) mysql> show profile for query 3; +----------------------+-----------+ | Status | Duration | +----------------------+-----------+ | (initialization) | 0.0000475 | | checking permissions | 0.0000077 | | Opening tables | 0.000026 | | System lock | 0.0000042 | | Table lock | 0.0000045 | | init | 0.0000205 | | Updating | 0.0000787 | | end | 0.0000567 | | query end | 0.000004 | | freeing items | 0.0000067 | | closing tables | 0.000004 | | logging slow query | 0.000002 | +----------------------+-----------+ 12 rows in set (0.00 sec)
二,timestampdiff来查看测试时间
mysql> set @d=now(); Query OK, 0 rows affected (0.00 sec) mysql> select * from comment; +------+-----+------+------------+---------------------+ | c_id | mid | name | content | datetime | +------+-----+------+------------+---------------------+ | 1 | 1 | ?? | 2222222211 | 2010-05-12 00:00:00 | | 2 | 1 | ?? | ????(??) | 2010-05-13 00:00:00 | | 3 | 2 | tank | ?????? | 0000-00-00 00:00:00 | +------+-----+------+------------+---------------------+ 3 rows in set (0.00 sec) mysql> select timestampdiff(second,@d,now()); +--------------------------------+ | timestampdiff(second,@d,now()) | +--------------------------------+ | 0 | +--------------------------------+ 1 row in set (0.00 sec)
这种方法有一点要注意,就是三条sql语句要尽量连一起执行,不然误差太大,根本不准
set @d=now(); select * from comment; select timestampdiff(second,@d,now());
如果是用命令行来执行的话,有一点要注意,就是在select timestampdiff(second,@d,now());后面,一定要多copy一个空行,不然最后一个sql要你自己按回车执行,这样就不准了。
其实我觉得吧,真正要我们关心的是那些查询慢的sql,因为真正影响速度的是他们,关于慢查询的东西,有空写一下。
http://jingyan.baidu.com/article/d169e1864d254d436711d852.html
shell之获取数据库SQL执行时间(精确到毫秒)
shell之获取时间(精确到毫秒)
需求:通过shell获取数据库当前时间,并计算从打开数据库连接--查询--关闭数据库连接所耗费的时间(精确到毫秒)
步骤:
1、获取时间(获取时间)
注意:
1:连接数据库的最后关闭时必须顶格写否则报错
2:shell无法直接获取精确到毫秒的时间,需要通过计算获取
方法/步骤
获取时间(获取时间)
[oracle@rhel6 zxx_shell]$ cat 3-time.sh
#!/bin/bash
var= #声明全局变量
function getTiming()
{
exec_start=$1
exec_end=$2
exec_start_s=`echo $exec_start | cut -d '.' -f 1` #获取开始时间的秒
exec_start_ns=`echo $exec_start | cut -d '.' -f 2` #获取开始时间的纳秒
exec_end_s=`echo $exec_end | cut -d '.' -f 1` #获取结束时间的秒
exec_end_ns=`echo $exec_end | cut -d '.' -f 2` #获取结束时间的纳秒
exec_time_ms=$[$[$[ 10#$exec_end_s - 10#$exec_start_s ] * 1000] + $[$[10#$exec_end_ns / 1000000] - $[10#$exec_start_ns / 1000000] ] ]
nowdate=`date +%Y%m%d-%T`
echo "--------$nowdate-------->":$exec_time_ms
}
date= #声明全局变量
function importTargetData()
{
exec_start=`date +%s.%N` #获取时间格式:秒.纳秒
sql="select sysdate from dual;"
date=`sqlplus -s zxx/zxx@orclone <<EOF #接收数据库查询返回值
set heading off
set termout off
set feedback off
$sql
quit;
EOF` #一定要顶格写
exec_end=`date +%s.%N`
var=$(getTiming $exec_start $exec_end)
}
importTargetData
echo $var
echo $date
[oracle@rhel6 zxx_shell]$ ./3-time.sh
--------20150805-15:26:19-------->:75
05-AUG-15
来源:https://blog.csdn.net/wangyueting415/article/details/65033543
查看mysql语句运行时间的2种方法相关推荐
- 查看mysql语句运行时间的方法
set @d=now(); select * from comment; select timestampdiff(second,@d,now());为了验证select 1 与 select 1 f ...
- Linux系统中查看Mysql数据库版本号的四种方法(图文完整版)
今天处理一个问题,发现要确定Mysql数据库的版本号,于是我就整理了一下我查看的几种方法. 第一种方法(在终端操作): 操作方式:在终端输入命令即可查询 命令:mysql -V 第二种方法(在终端操作 ...
- 查看mysql语句运行时间
通过 show profiles 语句来查看 查一下profile是不是打开了,默认是不打开的. mysql> show profiles; Empty set (0.02 sec) mysql ...
- 查看MySQL数据库大小的几种方法
1.进入information_schema 数据库(存放了其他的数据库的信息) use information_schema; mysql> use information_schema Re ...
- 查看mysql的版本的四种方法
2019独角兽企业重金招聘Python工程师标准>>> 1:在终端下: $ mysql -V 2:在mysql中: mysql> status; 3:在help里面查找 $ m ...
- mysql解析运行时间_分析 MySQL 语句运行时间
为了验证select 1 与 select 1 from tableName 与 select * from tableName的执行效率,需要测试一下各自执行的时间.于是总结一下,查看mysql语句 ...
- MySQL提供了以下三种方法用于获取数据库对象的元数据
MySQL提供了以下三种方法用于获取数据库对象的元数据: 1)show语句 2)从INFORMATION_SCHEMA数据库里查询相关表 3)命令行程序,如mysqlshow, mysqldump 用 ...
- dapper mysql 批量_MySQL数据库之c#mysql批量更新的两种方法
本文主要向大家介绍了MySQL数据库之c#mysql批量更新的两种方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 总体而言update 更新上传速度还是慢. 1: 简单的 ...
- 修改mysql数据库默认字符集_MySQL数据库之修改mysql默认字符集的两种方法详细解析...
本文主要向大家介绍了MySQL数据库之修改mysql默认字符集的两种方法详细解析 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. (1) 最简单的修改方法,就是修改mysql的m ...
最新文章
- 【点云StatisticalOutlierFilter】python-pcl:去除离群点
- python安装步骤win10-教你如何在Win10系统安装Python?
- 机器学习第11天:朴素贝叶斯模型 - 垃圾短信识别
- Python编程基础:第二十九节 异常Exception
- php run js,PHP switch 语句
- 阜阳市乡镇企业中专学校计算机教师高翱简介,2017年中南林业科技大学博士研究生奖助学金...
- 下载不了java应用程序_Java 7u45 - java webstart不会下载我的jar并执行应用程序,除非我显示java控制台...
- 【HDU - 3746 】Cyclic Nacklace (KMP,最小循环节问题)
- MATLAB视角下的七夕节
- 基于ansj_seg和nlp-lang的简单nlp工具类
- SAP Business One助力洛德集团实现巨大商业价值
- 红外倒车雷达原理图_自动驾驶汽车传感器技术解析—毫米波雷达
- mybatis date类型映射_Mybatis中类型映射处理器详解
- 在位置 0 处没有任何行。_我家孩子最爱这个小零食,外酥里糯,没有任何添加剂,0失败教程...
- Python基础知识 D9
- Django教程(自强学堂)
- 软件测试教程第2版(宫云战主编)
- lae界面开发工具入门之介绍十--如何打包资源文件?
- poj 4005 Moles
- 关于笔记本连接显示器检测不到的问题(NoVideoInput)
热门文章
- python表头写进csv文件_Python读取CSV文件列并在CSV-fi中写入文件名和列名
- redis的基本使用笔记二
- User-Agent-Switcher和fiddler
- Strut2和FreeMarker整合时的一些问题
- 做谷歌地图是获得Map key的方法
- Python数据分析pandas之series初识
- mysql 备份工具xtrabackup全备与还原图解
- commvault oracle备份,CommVault for Oracle备份和恢复.pdf
- 金融python培训班_2019年做金融,一定要学Python!:附Python视频教程
- java se 7web_JAX-WS ::从独立的Java 7 SE客户端调用Web服务的方法