Mongo資料庫慢查詢功能

2021-10-22 06:27:11 字數 1219 閱讀 2884

使用mysql資料庫的朋友,肯定知道,可以開啟慢查詢日誌,就能捕獲mysql資料庫中的慢sql語句了,那麼在mongo資料庫怎麼捕獲慢查詢呢,下面就一步步帶大家了解mongo資料庫的慢查詢功能的開啟和使用的。

開啟mongo資料庫的慢查詢功能

mongo資料庫的慢查詢功能(profiling)有三個級別 0:代表關閉,不收集任何慢查詢 1:收集慢查詢資料,預設收集超過100毫秒的慢查詢 2:收集任何操作記錄資料

預設情況下,mongo資料庫的慢查詢功能是關閉的,如果想要知道mongo資料庫是否已經開啟慢查詢功能,可以通過下面命令

> db.getprofilinglevel()

0

可以看到返回結果是0,mongo資料庫沒有開啟慢查詢功能。

開啟mongo資料庫的慢查詢功能 在這裡需要注意,mongo資料庫的慢查詢資料是存放在乙個資料庫集合中(system.profile),這個和mysql資料庫是有區別的,如果你不主動建立system.profile這個集合,那這個集合就固定1m大小,當慢查詢記錄超過1m,就會將歷史資料覆蓋,迴圈使用,所以在這裡需要根據業務實際情況設定集合大小。

在這裡需要大家注意的是,system.profile在哪個資料庫下建立,就只會收集這個資料庫下的慢查詢,在這裡,我在test資料庫下建立system.profile集合,操作過程如下所示

> use test

switched to db admin

> show tables;

customers

system.profile

user

開啟慢查詢功能

> db.setprofilinglevel(1, )

> db.getprofilingstatus()

這裡設定的profiling級別為1,慢查詢閾值為500毫秒。

慢查詢功能測試

這裡為了方便測試,將慢查詢的級別設定為2

> db.setprofilinglevel(2)

> db.getprofilingstatus()

模式集合的查詢

> db.user.find()

> db.system.profile.find()

從上面結果可以看到,已經捕獲到慢查詢了。

資料庫慢查詢

使用mysql慢查日誌對有效率問題的sql語句進行優 mysql show variables like slow query log 檢視伺服器慢查詢日啟 mysql set global slow query log on 開啟慢查詢日誌 mysql set global long query ...

資料庫查詢慢

今天寫了個兩個關聯的sql語句,select from a join b on a.relationid b.id where b.otherid 123 a表中的relation跟b表中的id相關聯,當執行時資料庫的執行速度突然便面了,發現這個語句的執行時間特別的長。最開始的時候認為查詢慢是因為b...

MySql資料庫慢查詢

一 什麼是資料庫慢查詢?資料庫慢查詢,就是查詢時間超過了我們設定的時間的語句。預設的設定時間是10秒。也可以自己根據實際專案設定。set long query time 0.0001 slow query log 是否開啟慢查詢日誌,1表示開啟,0表示關閉。log slow queries 舊版 5...