VPS 向け MySQL 最適化

VPS 向け MySQL 最適化

VPS (Linux / Cpanel) の MySQL 設定を最適化するのに少し問題があります。Mysql についてはあまり詳しくありませんが、この VPS を約 50 の異なる Web サイトで使用しています。すべての Web サイトの訪問者数は 1 日あたり 100 人未満なので、トラフィックはそれほど問題ではありません。VPS は現在正常に動作していますが、mysqltuner.pl の [!!] 警告が心配です。心配する必要がありますか?

=> 私の VPS 情報:

Processor Information
**Total processors: 6**
Processor #1
Vendor
GenuineIntel
Name
Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
Speed
2394.454 MHz
Cache
15360 KB
Processor #2
Vendor
GenuineIntel
Name
Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
Speed
2394.454 MHz
Cache
15360 KB
Processor #3
Vendor
GenuineIntel
Name
Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
Speed
2394.454 MHz
Cache
15360 KB
Processor #4
Vendor
GenuineIntel
Name
Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
Speed
2394.454 MHz
Cache
15360 KB
Processor #5
Vendor
GenuineIntel
Name
Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
Speed
2394.454 MHz
Cache
15360 KB
Processor #6
Vendor
GenuineIntel
Name
Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
Speed
2394.454 MHz
Cache
15360 KB

Memory Information
Memory: 30789880k/**32505856k** available (5336k kernel code, 1050056k absent, 665920k reserved, 7016k data, 1288k init)

System Information
Linux xl.xlxlxl.com 2.6.32-504.12.2.el6.x86_64 #1 SMP Wed Mar 11 22:03:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Current Memory Usage
             total       used       free     shared    buffers     cached
Mem:      30812400   30064332     748068        244    3883652   17129112
-/+ buffers/cache:    9051568   21760832
Swap:            0          0          0
Total:    30812400   30064332     748068

=> My.cnf:

[mysqld]
max_allowed_packet=268435456
open_files_limit=10000
query_cache_type=1
query_cache_size=768M
query_cache_limit=2048M
sort_buffer_size=4M
join_buffer_size=26M
read_rnd_buffer_size = 1M
max_heap_table_size = 1024M
tmp_table_size = 1024M
table_open_cache = 15000
innodb_log_buffer_size = 64M
max_connections=200
wait_timeout=26000
interactive_timeout=26000
innodb_buffer_pool_size=256M

=> Mysqltuner.pl:

 >>  MySQLTuner 1.4.0 - Major Hayden <[email protected]>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
[OK] Currently running supported MySQL version 5.6.23
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
[--] Data in MyISAM tables: 3G (Tables: 3156)
[--] Data in InnoDB tables: 183M (Tables: 1455)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 52)
[!!] Total fragmented tables: 5

-------- Security Recommendations  -------------------------------------------
[OK] All database users have passwords assigned

-------- Performance Metrics -------------------------------------------------
[--] Up for: 2d 20h 23m 59s (11M q [45.850 qps], 381K conn, TX: 85B, RX: 2B)
[--] Reads / Writes: 82% / 18%
[--] Total buffers: 2.1G global + 31.4M per thread (200 max threads)
[OK] Maximum possible memory usage: 8.2G (27% of installed RAM)
[OK] Slow queries: 0% (4/11M)
[OK] Highest usage of available connections: 7% (14/200)
[OK] Key buffer size / total MyISAM indexes: 8.0M/432.3M
[OK] Key buffer hit rate: 99.6% (493M cached / 1M reads)
[OK] Query cache efficiency: 59.6% (5M cached / 8M selects)
[!!] Query cache prunes per day: 143463
[OK] Sorts requiring temporary tables: 0% (16 temp sorts / 434K sorts)
[!!] Joins performed without indexes: 2610
[!!] Temporary tables created on disk: 83% (458K on disk / 550K total)
[OK] Thread cache hit rate: 99% (36 created / 381K connections)
[!!] Table cache hit rate: 13% (1K open / 12K opened)
[OK] Open file limit used: 1% (324/30K)
[OK] Table locks acquired immediately: 99% (4M immediate / 4M locks)
[!!] Connections aborted: 18%
[OK] InnoDB buffer pool / data size: 256.0M/183.9M
[OK] InnoDB log waits: 0
-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    Increasing the query_cache size over 128M may reduce performance
    Adjust your join queries to always utilize indexes
    Temporary table size is already large - reduce result set size
    Reduce your SELECT DISTINCT queries without LIMIT clauses
    Increase table_open_cache gradually to avoid file descriptor limits
    Read this before increasing table_open_cache over 64: http://bit.ly/1mi7c4C
    Your applications are not closing MySQL connections properly
Variables to adjust:
    query_cache_size (> 768M) [see warning above]
    join_buffer_size (> 26.0M, or always use indexes with joins)
    table_open_cache (> 15000)

答え1

パフォーマンスを向上させるためのアクションを推奨するのが、mysqltuner の考え方です。最適化テーブルは断片化を考慮します。ただし、大きなテーブルの最適化には時間がかかります。その間、最適化するテーブルはロックされます。通常、 で始まる警告メモは、セクション[!!]にコメントされていますGeneral recommendations

mysql のデフォルト パラメータを調整する前に、sql ステートメントと sql 接続を確認する必要があります。インデックスのない結合は、パフォーマンスに大きなペナルティをもたらします。 を使用して、slow_queriesどのクエリを最適化するかを特定してみてください。

また、結果セットのサイズが大きすぎることもよくあります。データベースを備えたショップ システムを考えてみましょう。

たとえば、ショップ システムに 10,000 個のアイテムがある場合、select * from items;呼び出しによって 10,000 個のアイテムすべてが返されるため、結果セットのサイズは 10,000 になります。ただし、実際に 10,000 個のアイテムすべてを一度に必要とすることはほとんどありません。

SQLの主な目的は、実際には、インデックスで最も効果的に機能するフィルターを使用して結果セットを減らすことです。たとえば、黄色のアイテムをすべて検索する場合、結果セットのサイズを次のように減らします。select * from items where color="yellow";

しかし、黄色のアイテムが数百個あると、ウェブページに表示するには多すぎます。そこで、結果を制限します。

select * from items where color="yellow" limit 1,10

最初のページの10項目

select * from items where color="yellow" limit 11,20

次のページも同様です。

したがって、テーブル インデックスを修正し、大きな結果セットを制限するとパフォーマンスが大幅に向上するため、データベースをチューニングする前にステートメントをチューニングすることをお勧めします。これが、mysqltuner がこれらの警告を発する理由です。

結合とインデックスをデバッグするための最も便利なツールの 1 つは、explainSELECT ステートメントの前で使用することです。

explain select i.name, l.address
    from items i
         inner join location l on i.location = l.id
    where l.description like '%universe%';

結果をコンパイルするために使用される、インデックス付きとインデックスなしのテーブル行の数が表示されます。

関連情報