日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

查看mysql语句运行时间的2种方法

發(fā)布時間:2024/9/20 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 查看mysql语句运行时间的2种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.jb51.net/article/45185.htm

?

網站運行很慢的時候,我就特別起知道為什么這么慢,所以我查啊查,數據庫絕對是很重要的一部分,里面運行的sql是絕對不能放過的。平時做項目的時候,我也會注意sql語句的書寫,寫出一些高效的sql來,所以我會經常測試自己寫的sql語句。我把我知道的二個方法,總結一下發(fā)出來。

一,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語句要盡量連一起執(zhí)行,不然誤差太大,根本不準

set @d=now(); select * from comment; select timestampdiff(second,@d,now());

如果是用命令行來執(zhí)行的話,有一點要注意,就是在select timestampdiff(second,@d,now());后面,一定要多copy一個空行,不然最后一個sql要你自己按回車執(zhí)行,這樣就不準了。

其實我覺得吧,真正要我們關心的是那些查詢慢的sql,因為真正影響速度的是他們,關于慢查詢的東西,有空寫一下。

?

http://jingyan.baidu.com/article/d169e1864d254d436711d852.html

?

shell之獲取數據庫SQL執(zhí)行時間(精確到毫秒)

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种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。