2017-01-01 95 views
0

我運行一個非常簡單的MySQL數據庫構造。我只有像 id,TimeStamp和OP_fs155e列,我認爲這是極其 基本設置。MySQL在Centos 7中性能不佳7

MariaDB [gadbdfm]> desc optical_power; 
+-----------+------------------+------+-----+----------------------+-----------------------------+ 
| Field  | Type    | Null | Key | Default    | Extra      | 
+-----------+------------------+------+-----+----------------------+-----------------------------+ 
| id_record | int(10) unsigned | NO | PRI | NULL     | auto_increment    | 
| TimeStamp | timestamp(6)  | NO |  | CURRENT_TIMESTAMP(6) | on update CURRENT_TIMESTAMP | 
| OP_fs155e | varchar(30)  | YES | MUL | NULL     |        | 
| data1  | varchar(30)  | YES |  | NULL     |        | 
| data2  | varchar(30)  | YES |  | NULL     |        | 
| data3  | varchar(30)  | YES |  | NULL     |        | 
| data4  | varchar(30)  | YES |  | NULL     |        | 
| data5  | varchar(30)  | YES |  | NULL     |        | 
+-----------+------------------+------+-----+----------------------+-----------------------------+ 
8 rows in set (0.00 sec) 

然而,我開始注意到,我的選擇是在 樹莓裨3(作爲主)和Centos7(如從屬 - 8GB肌肉服務器)第一非常緩慢。 這是我選擇在奴隸服務器花了幾分鐘的一切。我認爲問題在於Raspberry Pi 3速度太慢,但是當我發現它在真正的服務器從服務器上是一樣的時候,確切地說它有什麼問題。

MariaDB [gadbdfm]> select TimeStamp,OP_fs155e from optical_power; 

| 2017-01-01 17:41:03.697000 | -24  | 
| 2017-01-01 17:42:03.666000 | -24  | 
| 2017-01-01 17:43:03.701000 | -24  | 
| 2017-01-01 17:44:03.675000 | -24  | 
| 2017-01-01 17:45:03.676000 | -24  | 
| 2017-01-01 17:46:03.692000 | -24  | 
| 2017-01-01 17:47:03.686000 | -24  | 
| 2017-01-01 17:48:03.539000 | -24  | 
| 2017-01-01 17:49:03.581000 | -24  | 
+----------------------------+-----------+ 
23044062 rows in set (37.24 sec) 

大師的my.cnf

[email protected] - /opt/FlightStrata155E cat /etc/mysql/mysql.conf.d/mysqld.cnf | grep -v "#" | grep -v "^$" 
[mysqld_safe] 
socket  = /var/run/mysqld/mysqld.sock 
nice  = 0 
[mysqld] 
user  = mysql 
pid-file = /var/run/mysqld/mysqld.pid 
socket  = /var/run/mysqld/mysqld.sock 
port  = 3306 
basedir  = /usr 
datadir  = /var/lib/mysql 
tmpdir  = /tmp 
lc-messages-dir = /usr/share/mysql 
skip-external-locking 
bind-address  = 0.0.0.0 
key_buffer_size  = 16M 
max_allowed_packet = 16M 
thread_stack  = 192K 
thread_cache_size  = 8 
myisam-recover   = BACKUP 
query_cache_limit = 1M 
query_cache_size  = 16M 
log_error = /var/log/mysql/error.log 
server-id  = 1 
log_bin   = /var/log/mysql/mysql-bin.log 
expire_logs_days = 10 
max_binlog_size = 100M 
relay-log = /var/lib/mysql/mysql-relay-bin 
relay-log-index = /var/lib/mysql/mysql-relay-bin.index 
log-error = /var/lib/mysql/mysql.err 

從my.cnf文件

[[email protected] ~]# cat /etc/my.cnf | grep -v "#" | grep -v "^$" 
[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
symbolic-links=0 
server-id = 2 
[mysqld_safe] 
log-error=/var/log/mariadb/mariadb.log 
pid-file=/var/run/mariadb/mariadb.pid 
!includedir /etc/my.cnf.d 

我知道,我在我的計劃零優化

再舉一個例子:

這對樹莓派3選擇需要像7分鐘

mysql> select TimeStamp, OP_fs155e from optical_power ORDER BY TimeStamp desc limit 15; 
+0

向我們展示來自**的輸出從optical_power ORDER EXPLAIN選擇時間戳,OP_fs155e按時間戳降序極限15; ** –

+0

好了,所以什麼我的應用程序是這樣的:像每分鐘我收集我所說的價值**自由空間光學器件的OP_fs155e **(光功率),並將其保存在樹莓派(MASTER)的** mysql **中。 – user2156115

回答

0

好了,關於你的第一個建議:

Database changed 
mysql> SHOW TABLE STATUS LIKE 'optical_power'\G 
*************************** 1. row *************************** 
      Name: optical_power 
     Engine: InnoDB 
     Version: 10 
    Row_format: Compact 
      Rows: 22030014 
Avg_row_length: 38 
    Data_length: 843055104 
Max_data_length: 0 
    Index_length: 0 
     Data_free: 7340032 
Auto_increment: 34973978 
    Create_time: 2016-12-30 16:10:49 
    Update_time: NULL 
    Check_time: NULL 
     Collation: latin1_swedish_ci 
     Checksum: NULL 
Create_options: 
     Comment: 
1 row in set (0.00 sec) 

我使用16GB優盤東芝堅持使用作爲SD存儲卡,因爲我的SD卡有這麼多麻煩。比如,我使用了所有的rapberry pi3 USB端口因爲需要從多個傳感器收集數據。 我想

ALTER TABLE optical_power ADD INDEX(TimeStamp,OP_fs155e); 

我非常感謝您的回答,我會嘗試用它玩。

非常感謝

所以它只是簡單時間戳值(OP_fs155e)表。

code mysql> select TimeStamp, OP_fs155e from optical_power ORDER BY TimeStamp desc limit 15; 
+----------------------------+-----------+ 
| TimeStamp     | OP_fs155e | 
+----------------------------+-----------+ 
| 2017-01-01 17:49:03.581000 | -24  | 
| 2017-01-01 17:48:03.539000 | -24  | 
| 2017-01-01 17:47:03.686000 | -24  | 
| 2017-01-01 17:46:03.692000 | -24  | 
| 2017-01-01 17:45:03.676000 | -24  | 
| 2017-01-01 17:44:03.675000 | -24  | 
| 2017-01-01 17:43:03.701000 | -24  | 
| 2017-01-01 17:42:03.666000 | -24  | 
| 2017-01-01 17:41:03.697000 | -24  | 
| 2017-01-01 17:40:03.688000 | -24  | 
| 2017-01-01 17:39:03.574000 | -24  | 
| 2017-01-01 17:38:03.596000 | -24  | 
| 2017-01-01 17:37:03.545000 | -24  | 
| 2017-01-01 17:36:03.667000 | -24  | 
| 2017-01-01 17:35:03.544000 | -24  | 
+----------------------------+-----------+ 
15 rows in set (2 min 41.32 sec) 
+0

我在找的是'Data_length:843055104',這與我對755MB的猜測並不遙遠。表格狀態只能顯示行,數據長度,索引長度的粗略估計值。 –

1

的樹莓派用來存儲SD卡,因此它是可以理解的I/O性能會比真正的服務器磁盤系統差很多。

您的第一個查詢需要37.24秒才能返回2300萬行。這是相當多的I/O。我估計每16KB頁面可以容納大約500行(假設你的數據列平均每個字節大約3個字節),所以你需要讀取超過46,000個頁面來閱讀2300萬行,這些行數將達到755MB。

我敢打賭,或多或少你的表,的數據長度,你可以檢查:

SHOW TABLE STATUS LIKE 'optical_power'\G 

但對於隨機的I/O速率讀取SD卡爲2.28MB /秒和8.10MB之間/秒,所以從SD卡讀取755MB需要93到331秒。這只是數學。

也許有些數據頁已經被緩存在MySQL的緩衝池中,或者InnoDB能夠做一些預讀優化來幫助這裏。

您的第二個查詢沒有得到很好的優化。它必須使用文件夾,因爲您的TimeStamp列中沒有索引。文件夾可能使用臨時存儲空間,這會導致寫入I/O。寫入比在SD卡上讀取要慢得多。所以一點也不奇怪,它需要7分鐘來完成你的ORDER BY TimeStamp LIMIT 15查詢。

顯然SD卡的品牌有很大的不同。參見http://www.jeffgeerling.com/blogs/jeff-geerling/raspberry-pi-microsd-card進行一些比較。

但即使您獲得的SD卡速度更快,您仍然通過低效地使用I/O導致其磨損。避免I/O會更好。

  • TimeStamp列創建索引,所以ORDER BY TimeStamp可以用它代替做文件排序的。索引對於優化查詢非常重要。看我的介紹How to Design Indexes, Really
  • 許多Raspberry Pi用戶將MySQL數據存儲在MyISAM存儲引擎中。 MyISAM僅使用緩衝I/O,並且沒有碰撞安全功能。這應該有助於改善I/O性能並減少SD卡的磨損。索引和複製對MyISAM表格很好。 MyISAM也傾向於將數據存儲在比InnoDB更少的空間中。
  • 確保您設置sync_binlog=0以允許複製日誌使用異步I/O。

如果必須使用InnoDB,調整它的最大緩存和保質:

  • 增加innodb_buffer_pool_size不亞於您可以備用。但不要讓它太大以至於其他進程沒有足夠的內存。使用另一個10%計算緩衝池,所以如果將其設置爲512M,那麼它確實需要563M。
  • 儘可能避免同步I/O並使用緩衝I/O。見https://dev.mysql.com/doc/refman/5.5/en/optimizing-innodb-diskio.html

    innodb_flush_log_at_trx_commit=2 
    innodb_flush_method=O_DSYNC 
    innodb_doublewrite=0 
    

回覆您的評論:

我使用16GB USB東芝棒作爲存儲,而不是OD SD卡,因爲我有SD卡

這麼多麻煩

USB閃存盤(棒)在性能上與SD卡沒有多大差別。這兩款器件均針對順序讀/寫進行了優化,而不是隨機讀取/寫入。它們非常吸引人的文件系統或數據庫工作。

底線是如果您需要真實服務器的性能,數據的大小屬於服務器,則不要使用Raspberry Pi。

我希望Raspberry Pi的數據收集解決方案不要在Pi上存儲任何數據,而是立即將數據發佈到網絡上的服務器上。一個好的解決方案是運行消息隊列服務器,收集各種Pi設備發佈的事件。然後編寫一個腳本以消費來自消息隊列的數據並批量將其發佈到數據庫。

0

你好@Bill Karwin和@Bernd Buffen!我非常感謝你的 意見!我加入這樣指數作爲節目和在演示here解釋:

mysql> ALTER TABLE optical_power ADD INDEX(TimeStamp,OP_fs155e); 

和比較〜2分鐘OT7分鐘有時結果爲3.24秒: AMAZING !!!

mysql> use gadbdfm; 
Database changed 
mysql> select TimeStamp, OP_fs155e from optical_power ORDER BY TimeStamp desc limit 15; 
+----------------------------+-----------+ 
| TimeStamp     | OP_fs155e | 
+----------------------------+-----------+ 
| 2017-01-01 17:49:03.581000 | -24  | 
| 2017-01-01 17:48:03.539000 | -24  | 
| 2017-01-01 17:47:03.686000 | -24  | 
| 2017-01-01 17:46:03.692000 | -24  | 
| 2017-01-01 17:45:03.676000 | -24  | 
| 2017-01-01 17:44:03.675000 | -24  | 
| 2017-01-01 17:43:03.701000 | -24  | 
| 2017-01-01 17:42:03.666000 | -24  | 
| 2017-01-01 17:41:03.697000 | -24  | 
| 2017-01-01 17:40:03.688000 | -24  | 
| 2017-01-01 17:39:03.574000 | -24  | 
| 2017-01-01 17:38:03.596000 | -24  | 
| 2017-01-01 17:37:03.545000 | -24  | 
| 2017-01-01 17:36:03.667000 | -24  | 
| 2017-01-01 17:35:03.544000 | -24  | 
+----------------------------+-----------+ 
15 rows in set (3.24 sec) 
+0

使用該索引的查詢應該運行得更快。 –