MySQL optimieren - CPU-Last zu hoch

Poyraz50

New Member
Hallo,

bevor ich selber Hand anlege wollte ich gerne mal bei Experten nachfragen wie ich meine mysql-Einstellungen optimieren kann.
Leider springt meine CPU-Auslastung durch Mysql ständig weit über 50% und nähert sich der 100% gefährlich an.

Wäre wirklich sehr nett wenn mir jemand Hilfestellung geben könnte.

Code:
 >>  MySQLTuner 1.0.1 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering

-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.70-0ubuntu0.10.04.1
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 3G (Tables: 1456)
[--] Data in InnoDB tables: 150M (Tables: 274)
[--] Data in MEMORY tables: 124K (Tables: 11)
[!!] Total fragmented tables: 373

-------- Performance Metrics -------------------------------------------------
[--] Up for: 32m 40s (28K q [14.295 qps], 2K conn, TX: 440M, RX: 19M)
[--] Reads / Writes: 76% / 24%
[--] Total buffers: 51.0M global + 2.7M per thread (151 max threads)
[OK] Maximum possible memory usage: 456.8M (22% of installed RAM)
[OK] Slow queries: 0% (0/28K)
[OK] Highest usage of available connections: 4% (7/151)
[OK] Key buffer size / total MyISAM indexes: 16.0M/625.0M
[OK] Key buffer hit rate: 99.7% (1M cached / 3K reads)
[OK] Query cache efficiency: 45.7% (9K cached / 19K selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 1K sorts)
[!!] Joins performed without indexes: 171
[OK] Temporary tables created on disk: 17% (264 on disk / 1K total)
[OK] Thread cache hit rate: 99% (7 created / 2K connections)
[!!] Table cache hit rate: 0% (64 open / 11K opened)
[OK] Open file limit used: 10% (104/1K)
[OK] Table locks acquired immediately: 99% (22K immediate / 22K locks)
[!!] InnoDB data size / buffer pool: 150.2M/2.0M

-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Enable the slow query log to troubleshoot bad queries
    Adjust your join queries to always utilize indexes
    Increase table_cache gradually to avoid file descriptor limits
Variables to adjust:
    join_buffer_size (> 128.0K, or always use indexes with joins)
    table_cache (> 64)
    innodb_buffer_pool_size (>= 150M)
Wie hoch soll ich am besten den Wert für join_buffer_size und vor allem innodb_buffer_pool_size setzen?
Muss dazu sagen, dass ich den Server heute neu gestartet habe weil mir MySQL den Server lahmgelegt hat.

Vielen Dank
 
Poste bitte Deine MySQL-Konfiguration und noch ein paar zusätzliche Systeminfos (CPU, Ram, "Aufgaben", ...)

ggf. auch mal tuning-primer drüber laufen lassen, das ist ein wenig gesprächiger...
 
Poste bitte Deine MySQL-Konfiguration und noch ein paar zusätzliche Systeminfos (CPU, Ram, "Aufgaben", ...)

ggf. auch mal tuning-primer drüber laufen lassen, das ist ein wenig gesprächiger...

War gerade dabei tuning-primer einzufügen :)

Tuning-Primer:
Code:
-- MYSQL PERFORMANCE TUNING PRIMER --
             - By: Matthew Montgomery -

MySQL Version 5.1.70-0ubuntu0.10.04.1 x86_64

Uptime = 0 days 2 hrs 26 min 33 sec
Avg. qps = 10
Total Questions = 91010
Threads Connected = 1

Warning: Server has not been running for at least 48hrs.
It may not be safe to use these recommendations

To find out more information on how each of these
runtime variables effects performance visit:
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html
Visit http://www.mysql.com/products/enterprise/advisors.html
for info about MySQL's Enterprise Monitoring and Advisory Service

SLOW QUERIES
The slow query log is NOT enabled.
Current long_query_time = 10.000000 sec.
You have 13 out of 91031 that take longer than 10.000000 sec. to complete
Your long_query_time seems to be fine

BINARY UPDATE LOG
The binary update log is NOT enabled.
You will not be able to do point in time recovery
See http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html

WORKER THREADS
Current thread_cache_size = 8
Current threads_cached = 7
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine

MAX CONNECTIONS
Current max_connections = 151
Current threads_connected = 1
Historic max_used_connections = 8
The number of used connections is 5% of the configured maximum.
You are using less than 10% of your configured max_connections.
Lowering max_connections could help to avoid an over-allocation of memory
See "MEMORY USAGE" section to make sure you are not over-allocating

INNODB STATUS
Current InnoDB index space = 310 M
Current InnoDB data space = 151 M
Current InnoDB buffer pool free = 0 %
Current innodb_buffer_pool_size = 2 M
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory

MEMORY USAGE
Max Memory Ever Allocated : 56 M
Configured Max Per-thread Buffers : 405 M
Configured Max Global Buffers : 34 M
Configured Max Memory Limit : 440 M
Physical Memory : 2.00 G
Max memory limit seem to be within acceptable norms

KEY BUFFER
Current MyISAM index space = 625 M
Current key_buffer_size = 16 M
Key cache miss rate is 1 : 274
Key buffer free ratio = 81 %
Your key_buffer_size seems to be fine

QUERY CACHE
Query cache is enabled
Current query_cache_size = 16 M
Current query_cache_used = 10 M
Current query_cache_limit = 1 M
Current Query cache Memory fill ratio = 67.14 %
Current query_cache_min_res_unit = 4 K
MySQL won't cache query results that are larger than query_cache_limit in size

SORT OPERATIONS
Current sort_buffer_size = 2 M
Current read_rnd_buffer_size = 256 K
Sort buffer seems to be fine

JOINS
Current join_buffer_size = 132.00 K
You have had 705 queries where a join could not use an index properly
You should enable "log-queries-not-using-indexes"
Then look for non indexed joins in the slow query log.
If you are unable to optimize your queries you may want to increase your
join_buffer_size to accommodate larger joins in one pass.

Note! This script will still suggest raising the join_buffer_size when
ANY joins not using indexes are found.

OPEN FILES LIMIT
Current open_files_limit = 1024 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine

TABLE CACHE
Current table_open_cache = 64 tables
Current table_definition_cache = 256 tables
You have a total of 1764 tables
You have 64 open tables.
Current table_cache hit rate is 0%
, while 100% of your table cache is in use
You should probably increase your table_cache
You should probably increase your table_definition_cache value.

TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 16 M
Of 4091 temp tables, 15% were created on disk
Created disk tmp tables ratio seems fine

TABLE SCANS
Current read_buffer_size = 128 K
Current table scan ratio = 99739 : 1
You have a high ratio of sequential access requests to SELECTs
You may benefit from raising read_buffer_size and/or improving your use of indexes.

TABLE LOCKING
Current Lock Wait ratio = 1 : 891
You may benefit from selective use of InnoDB.
If you have long running SELECT's against MyISAM tables and perform
frequent updates consider setting 'low_priority_updates=1'
If you have a high concurrency of inserts on Dynamic row-length tables
consider setting 'concurrent_insert=2'.

my.cnf
Code:
innodb_buffer_pool_size=2M
innodb_additional_mem_pool_size=500K
innodb_log_buffer_size=500K
innodb_thread_concurrency=2
[mysqld]
local-infile=0

# * Fine Tuning
#
key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.

System:
GenuineIntel, Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz
RAM: 2GB garantiert, bis 4GB
 
Evtl. antwortet keiner, weil die Antworten ja schon alle dort stehen.
Fang einfach an die Tipps vom Tuning-Primer umzusetzen.

Zwei Hinweise:
Du hast recht wenig Queries. Daher lass den MySQL-Server erstmal 48 Stunden laufen, damit die statistischen Daten überhaupt eine Aussagekraft haben.
Zweitens: long_query_time=2

huschi.
 
Leider springt meine CPU-Auslastung durch Mysql ständig weit über 50% und nähert sich der 100% gefährlich an.
Inwiefern sind die 100% aus deiner Sicht gefährlich? MySQL selbst kann Multithreading, nur einzelne Queries bleiben single-threaded. Oder ist der Server insgesamt stark ausgelastet?

Wie hoch soll ich am besten den Wert für join_buffer_size und vor allem innodb_buffer_pool_size setzen?
Zum ersten Wert kann ich leider nichts beitragen, dein InnoDB-Bufferpool ist aber viel (!) zu klein. Generell kann man sagen, dass Schreibzugriffe auf eine InnoDB-Tabelle nicht performant sind, wenn diese nicht komplett in den Bufferpool passt. Ich habe habe hier z.B. ein MySQL-Setup mit gut 30 GB Bufferpool, während die zwei größten InnoDB-Tabellen jeweils 23 GB groß sind - und das Setup läuft schreibend nicht mehr wirklich flott, weil diese Tabellen stark frequentiert werden. Der Unterschied ist enorm, wenn die Tabellen komplett in den Bufferpool passen.

Muss dazu sagen, dass ich den Server heute neu gestartet habe weil mir MySQL den Server lahmgelegt hat.
Wenn dein Server wirklich quasi tot ist, du aber noch per SSH draufkommst, solltest du in einem solchen Moment mal ein SHOW PROCESSLIST; auf MySQL loslassen - ggf. findest du da den Übeltäter.


Grüße
Tim
 
Sorry für die späte Antwort, ich habe aber extra abgewartet bis die 48 Stunden rum sind.

Mir ist schon klar, dass ich die von tuning-primer bemängelten Werte entsprechend anpassen muss, aber ich bin mir halt unsicher in Bezug auf die konkreten Zahlen. Da wäre ich über ein wenig Hilfe sehr dankbar.

Code:
        -- MYSQL PERFORMANCE TUNING PRIMER --
             - By: Matthew Montgomery -

MySQL Version 5.1.70-0ubuntu0.10.04.1 x86_64

Uptime = 2 days 0 hrs 2 min 23 sec
Avg. qps = 6
Total Questions = 1203622
Threads Connected = 1

Server has been running for over 48hrs.
It should be safe to follow these recommendations

SLOW QUERIES
The slow query log is NOT enabled.
Current long_query_time = 10.000000 sec.
You have 161 out of 1203643 that take longer than 10.000000 sec. to complete
Your long_query_time seems to be fine

BINARY UPDATE LOG
The binary update log is NOT enabled.
You will not be able to do point in time recovery
See http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html

WORKER THREADS
Current thread_cache_size = 8
Current threads_cached = 7
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine

MAX CONNECTIONS
Current max_connections = 50
Current threads_connected = 1
Historic max_used_connections = 8
The number of used connections is 16% of the configured maximum.
Your max_connections variable seems to be fine.

INNODB STATUS
Current InnoDB index space = 311 M
Current InnoDB data space = 150 M
Current InnoDB buffer pool free = 0 %
Current innodb_buffer_pool_size = 2 M
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory

MEMORY USAGE
Max Memory Ever Allocated : 56 M
Configured Max Per-thread Buffers : 134 M
Configured Max Global Buffers : 34 M
Configured Max Memory Limit : 169 M
Physical Memory : 2.00 G
Max memory limit seem to be within acceptable norms

KEY BUFFER
Current MyISAM index space = 625 M
Current key_buffer_size = 16 M
Key cache miss rate is 1 : 1161
Key buffer free ratio = 81 %
Your key_buffer_size seems to be fine

QUERY CACHE
Query cache is enabled
Current query_cache_size = 16 M
Current query_cache_used = 11 M
Current query_cache_limit = 11 M
Current Query cache Memory fill ratio = 70.00 %
Current query_cache_min_res_unit = 4 K
MySQL won't cache query results that are larger than query_cache_limit in size

SORT OPERATIONS
Current sort_buffer_size = 2 M
Current read_rnd_buffer_size = 256 K
Sort buffer seems to be fine

JOINS
Current join_buffer_size = 132.00 K
You have had 7927 queries where a join could not use an index properly
You should enable "log-queries-not-using-indexes"
Then look for non indexed joins in the slow query log.
If you are unable to optimize your queries you may want to increase your
join_buffer_size to accommodate larger joins in one pass.

Note! This script will still suggest raising the join_buffer_size when
ANY joins not using indexes are found.

OPEN FILES LIMIT
Current open_files_limit = 1024 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine

TABLE CACHE
Current table_open_cache = 256 tables
Current table_definition_cache = 256 tables
You have a total of 1764 tables
You have 256 open tables.
Current table_cache hit rate is 0%
, while 100% of your table cache is in use
You should probably increase your table_cache
You should probably increase your table_definition_cache value.

TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 16 M
Of 39318 temp tables, 6% were created on disk
Created disk tmp tables ratio seems fine

TABLE SCANS
Current read_buffer_size = 128 K
Current table scan ratio = 134478 : 1
You have a high ratio of sequential access requests to SELECTs
You may benefit from raising read_buffer_size and/or improving your use of indexes.

TABLE LOCKING
Current Lock Wait ratio = 1 : 1238
You may benefit from selective use of InnoDB.
If you have long running SELECT's against MyISAM tables and perform
frequent updates consider setting 'low_priority_updates=1'
If you have a high concurrency of inserts on Dynamic row-length tables
consider setting 'concurrent_insert=2'.

Wie wären folgende Einstellungen?

long_query_time = 2
innodb_buffer_pool_size = 512M oder besser mehr?
join_buffer_size = 512k oder höher?
open_files_limit= auf 1024 belassen oder besser auf 1536?
table_open_cache = 512
table_definition_cache = 512
read_buffer_size = 256k
low_priority_updates=1


Vielen Dank
 
Mal ein bisschen "Händchen halten":
tuning-primer sagt:
Code:
low_priority_updates=1
concurrent_insert=2
Macht Beides Sinn und stört auch nicht.

Sowohl bei innodb_buffer_pool_size als auch bei table_open_cache gibt TP einen Hit-Rate an und wie voll die Buffer aktuell sind. Drehe die Werte soweit hoch, dass die Hit-Rate gegen 100% gehen aber die Buffer dennoch gut gefüllt sind. Achte danach auf die "Configured Max Memory Limit". Die sollte den "Physical Memory" nicht übersteigen.
Analog zu table_open_cache (selber Wert in table_definition_cache) auch die open_files_limit einstellen. Achte dabei auf Deine Systembedingten File-Limits. Je nach Virtualisierungsumgebung könntest Du dabei an Grenzen stoßen.

Den read_buffer_size lass lieber so wie er im Original ist. Er hat auch keinen wesentlichen Einfluss auf die darunter liegende "table scan ratio".


Noch eine Anmerkung:
Im ersten Schritt kann man mehrere Werte auf einmal verbessern. In den folgenden Schritten (ja, plural, immer Zeit dazwischen vergehen lassen, und auch später regelmäßig überprüfen ob noch alles stimmt... denn es reicht wenn nur eine Tabelle dazu kommt und alle Buffer laufen über...) nur noch einzelne Werte ändern und später den Erfolg prüfen.
TP liefert zwei wesentliche Zahlen, die den Optimierungs-Erfolg bescheinigen (oder eben nicht). Dabei ist der erste eigentlich nur per Application- und/oder Modell-Optimierung zu verbessern und der Zweite lediglich über die Server-Einstellungen:
- "Current table scan ratio": Hier sollte der erste Wert möglichst kleiner werden.
- "Current Lock Wait ratio": Hier sollte der hintere Wert im Idealfall größer werden.

huschi.
 
Back
Top