optimize mysql select in wordpress DB - php

Some data first:
wordpress instalation (~ 110k posts, ~20k tags, ~250 categories, latest version)
W3 Total cache included (disk caching: database, object, page, browser)
server is VPS (4 GB RAM / up to 8 GB Dynamic, 2 vCores, Ubuntu 64bit)
Running Apache2, php 5.3.10, mysql 5.5.37
What is running on the server:
a few very small domains (traffic and resources close to 0)
5 cron jobs that import data to WP
The problem: viewing a post takes around 10 s. Refreshing is in no time as the caching plugin does a good job, however if the post hasn't been visited before the time for viewing it is around 10-12 s.
After some tracing in order to find out what is going on wrong I found out that mysql query for each page is taking some 90 - 95 % of the total time (9-10 seconds for each page).
I have tried to figure out if there is something wrong with mysql configuration and modified the fallowing from default in my.cnf:
key_buffer_size = 64M
innodb_buffer_pool_size = 2G
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_thread_concurrency = 16
innodb_flush_method = O_DIRECT
innodb_additional_mem_pool_size = 20M
query_cache_limit = 8M
query_cache_size = 64M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 32
After restarting there has not been any improvement at all since I guess I am missing something. I have started tracing with newrelic and their system points out that there are wueries that take upt to 34 s to execute.
Here is an SQL query from newrelic's log:
SELECT SQL_CALC_FOUND_ROWS qmV?_posts.ID FROM qmV?_posts INNER JOIN qmV?_postmeta ON ( qmV?_posts.ID = qmV?_postmeta.post_id ) WHERE ?=? AND (
MONTH( qmV?_posts.post_date ) = ?
) AND qmV?_posts.post_type = ? AND (qmV?_posts.post_status = ?) AND (
qmV?_postmeta.meta_key = ?
) GROUP BY qmV?_posts.ID ORDER BY qmV?_postmeta.meta_value+? DESC LIMIT ?, ?
I have tried also to verify wether the tables are indexed correctly and it appears to be so as (obviously) WP is doing a great job in optimizing search across the DB.
In subsidiary a monitoring tools reports "MySQL CPU usage" to something around 95% to 105% while this query is running.
Question: is there anything else I can tweak on this server or I should have a look at some other options for hosting this application? If you propose the 2'nd option please argument your option.
Update 1:
ps aux | grep 'mysqld' returns:
mysql 522 13.5 1.0 1227664 85128 ? Ssl 2014 3513:12 /usr/sbin/mysqld

Related

PHP server change between Apache and Nginx: 3x performance loss

Our current infra works fine with Apache + PHP and MySQL managed elsewhere. I have not done any particular optimization on this server. These are the initial configurations. Except for memory_limit and execution_time.
I am in the process of changing our infra to something more modern with bigger server and Nginx + PHP-FPM.
While working on this problem, I realize that we have big performance losses using this new infra. It's a shame to have more powerful servers and it's slower.
I watched the server load and nothing. Memory and CPU are barely 2%
I pulled out my Inspector badge and began to eliminate guesswork. Obviously, my tests use exactly the same case, same framework, same version of our app, ...
Here are my tests:
I installed on newer Apache + PHP servers and in fact Apache is even slower than with Nginx (no surprise :)).
I created a simple "hello wok" PHP file to test if it could come from Nginx. So I am not using our framework which could be involved.
Everything is normal TTFB times are almost equivalent (~ 24ms). Nginx on the new infra is 2ms faster.
While going with our framework, I added a piece of basic code which amounts to doing a "Hello wok" but using our framework.
Ah there, we have a problem. Finally ! Old infra 61ms, New infra: 184ms.
The new infra is therefore 3x slower to load the framework.
Obviously, I did a lot of optimizing the existing code but seeing that, I'll never be able to optimize it and make it 3x faster.
In all cases: Why the same code takes 3x longer to run on different environments.
The only thing that occurs to me right now is a PHP and PHP-FPM configuration problem.
In these configurations, do you know of any reasons that could have such a slowdown?
Obviously I looked for and tested things to optimize but it's always the same.
Here is my php.ini file:
date.timezone = Europe/Paris
opcache.enable = 0
apc.enabled = 1
apc.enable_cli = 1
upload_max_filesize = 128M
post_max_size = 128M
realpath_cache_size=4096k
realpath_cache_ttl=7200
display_errors = Off
display_startup_errors = Off
access.log = /proc/self/fd/2
error_log = /proc/self/fd/2
error_reporting = E_ALL ^ E_STRICT
; output_buffering=4096
pm = dynamic
pm.max_children = 5
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 1000
max_execution_time=600
memory_limit = 2048M
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 2048
; opcache.interned_strings_buffer = 32
; opcache.max_accelerated_files = 100000
; opcache.max_wasted_percentage = 15
opcache.revalidate_freq = 0
; opcache.validate_timestamps = 1
Do you have any advice ? recommendations ? ideas ?
thanks in advance
Are you doing well
EDIT #1
Server Information:
Old server:
VM with 1 processor / 2GB RAM
No database, just Apache + PHP
Framework specific cache system: file
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 6
model name : Common KVM processor
cpu MHz : 3504.002
cache size : 4096 KB
[...]
New server:
Docker container on a 3 processor / 4GB RAM server
I do not have the speeds of each processor. This is vCPU.
No database
Container PHP-FPM + Container Nginx
Framework specific cache system: APC
processor : 0
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
processor : 1
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
processor : 2
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
Others :
The database is managed in a provider
The sessions are stored in the database
Despite what you will read all over the internet, under low loads, Apache+mod_php is significantly faster than nginx+php-fpm. The latter architecture offers better capacity and scaling. That does not explain the difference in performance reported here.
The only thing you have told us about the two infrastructures is that one is old and one is new. If all you know about them is that you clicked a button in a cloud control panel then it can be difficult to find out more (if this is a Linux VM, check /proc/cpuinfo and mpstat paying particular attention to the steal time). The metrics you measured by testing will be highly dependent on single thread performance which is not a good indicator of throughput on a multi-cpu machine or any machine under heavy load.
But the first place I would be looking is at the storage latency / increasing opcache.revalidate_freq.

Compute Engine MYSQL Server CPU Strange

I couldn't think what else to title this strange problem.
We have a "Worker" Compute Engine which is a MySQL SLAVE. Its primary role is to process a large set of data and then place it back on the Master. All handled via a PHP Script.
Now the processing of data takes roughly 4 hours to complete. During this time we noticed the following CPU pattern.
What you can see above is the 50% solid CPU starts after a server reboot. Then after about 2 hours its starts to produce a ECG style pattern on the CPu. Around every 5/6 minutes CPU spikes to ~48% then drops over the 5 minutes.
My question is, why. Can anyoen please explain why. We ideally want this server to be Maxing out ots cpu at 100% (50% as there are 2 cores)
The spec of the server: 2 VCPU's with 7.5GB Memory.
As mentioned, if we can have this running full throttle it would be great. Below is the my.cnf
symbolic-links=0
max_connections=256
innodb_thread_concurrency = 0
innodb_additional_mem_pool_size = 1G
innodb_buffer_pool_size = 6G
innodb_flush_log_at_trx_commit = 1
innodb_io_capacity = 800
innodb_flush_method = O_DIRECT
innodb_log_file_size = 24M
query_cache_size = 1G
query_cache_limit = 512M
thread_cache_size = 32
key_buffer_size = 128M
max_allowed_packet = 64M
table_open_cache = 8000
table_definition_cache = 8000
sort_buffer_size = 128M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 128M
tmp_table_size = 256M
query_cache_type = 1
join_buffer_size = 256M
wait_timeout = 300
server-id = 2
relay-log = /var/log/mysql/mysql-relay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
log-error=/var/log/mysqld.log
read-only = 1
innodb_flush_log_at_trx_commit=2
I have cleaned up the above to remove any configs with private information which are not relevant to performance.
UPDATE
I have noticed when the VPU starts dropping during the heartbeat section of the graph the PHP script is no longer running. This is impossible, as the script I know takes 4 hours. No errors, and after another 4 hours the data is where I expected it.
Changing innodb_io_capacity = 800 to 1500 will likely reduce your 4 hour elapsed time to process by raising the limit to what you know you can achieve with your slave processing.
For your 7.5G indicated environment, configuration has
innodb_additional_mem_pool_size=1G
innodb_buffer_pool_size=6G
query_cache_size=1G
so before you start, you are overcommitted.
Another angle to consider, with
max_connections=256
max_allowed_packet=64M
could on a fully busy 256 connections need 16GB + just for this function to survive.
It is unlikely max_allowed_packet at 64M is reasonable.
Changing read_rnd_buffer_size = 4M to SET GLOBAL read_rnd_buffer_size=16384; could be significant on your slave then 24 hours later on master. They can be different but if it is significant in reducing your 4 hours on the slave, implement on both instances. Let us know what this single change does for you, please.
The 50% cpu utilization you are seeing is the script maxing out the --- single core that it is capable of utilizing --- . As indicated by PressingOnAlways recently. You can not tune around limit in your running script.
For a more thorough analysis, provide from SLAVE AND MASTER
RAM size (nnG)
SHOW GLOBAL STATUS
SHOW GLOBAL VARIABLES
SHOW INNODB STATUS
CPU % is measured by all the cores - so 100% cpu usage == both cores maxing out. PHP by default runs in a single thread and does not utilize multi-cores. The 50% cpu utilization you are seeing is the script maxing out the single core that it is capable of utilizing.
In order to utilize 100% cpu, consider spawning 2 PHP scripts that work on 2 separate datasets - e.g. script 1 processes records 1-1000000, while script 2 processes 1000001-2000000.
Other option is to rewrite the script to utilize threads. You may want to consider changing the language altogether for something that is more conducive to threads, like Golang? Though this might not be necessary if the main work is done within mysql.
The other issue you're seeing when the graph is below 50% may be due to IO wait. It's hard to tell from a graph though, you may be having a data flow transfer bottleneck where your CPU isn't working and waiting while large bits of data is transferred.
Optimizing CPU utilization is an exercise in finding the bottlenecks and removing them - good luck.
'Monitoring Service' could enabled to periodically capture a 'health check' of your system since it appears to be on a 6 minute cycle when you see spikes.
SHOW GLOBAL STATUS LIKE 'Com_show_%status' may confirm activity of this nature.
Divide your com_show_%status counters by (uptime/3600) to get rate-per-hour.
10 times an hour would be every 6 minutes.

Big database optimisation

I created a web service that generates huge requests every seconds.
Sometimes the MYSQL service seem down few secondes and work again well.
The main table contain more than 4 420 115 entries since one month.
Storage engine : InnoDB
The server configuation :
CPU : Intel(R) Xeon(R) CPU D-1540 # 2.00GHz
Coeurs : 16
Cache : 12288KB
RAM : 4x 32Go
Disques : 2 x 480 Go
The my.cnf :
skip-external-locking
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
myisam-recover = BACKUP
query_cache_limit = 1M
query_cache_size = 16M
Can I have advices to avoid this problem and increase the mysql performances.
Thank's
Sounds like it may be time to iterate on the database structure and application logic to reduce the amount of DB interactions. Another option is adding a memcached or redis layer between the application and the SQL database to provide near millisecond response time for read actions.

Joomla database memory leak

My client has got a pretty large Joomla-based website hosted on Amazon EC2 with 1.5GB of RAM. The server hosts both Apache and MySQL. Right now the database size is around 250MB and the website gets daily traffic of about 5000. It looks like there is a severe memory leak on the website as sometimes MySQL uses about 99% of CPU memory and then crashes. I have tried optimizing database tables, modifying my.cnf, but still there is no improvement.
There are finder tables used by Joomla smart search which occupy over 100MB of db size. I have disabled smart search, but still the problem occurs.
Friends, please throw some suggestions in fixing this.
Thanks.
Below is the my.cnf file
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
bind-address = 127.0.0.1
default-storage-engine=innodb
transaction-isolation = REPEATABLE-READ
character-set-server = UTF8
collation-server = UTF8_general_ci
max_connections = 5000
wait_timeout = 30
connect_timeout = 60
#interactive_timeout = 600
#max_connect_errors = 1000000
#max_allowed_packet = 10M
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
slow_query_log
long_query_time = 2
[mysqld_safe]
log-error=/var/log/mysqld.log
myisam_sort_buffer_size = 64M
My bet would be that you are being hit by a rogue robot - one of the many SEO spiders out there, or tools like 80legs that let people program a network of bots to carry out tasks - often with errors in their programming that result in a heavy bombardment.
I can never remember which of the MySQL settings take memory once and which are per connection - but as you are set to allow up to 5000 simultaneous connections and some of the buffers are 2 and 8 MB I'd bet that the total memory usage under heavy load could easily be in excess of the total ram available.
Your current settings would allow all of your daily traffic to hit simultaneously. I'd knock that down to a setting of a hundred or less and see if that gives more stability.
There are various MySQL tuner scripts out there that can help you spot where too much memory is allocated.
If you have access logs from around the time of the crashes / high load I'd check for malicious bots though - we've had a constant battle to reign them in on some sites we monitor/control.
You might also check the thread_concurrency value - depending upon how many CPUs you have available.

MySql Proccesslist filled with "Sleep" Entries leading to "Too many Connections"?

I'd like to ask your help on a longstanding issue with php/mysql connections.
Every time I execute a "SHOW PROCESSLIST" command it shows me about 400 idle (Status: Sleep) connections to the database Server emerging from our 5 Webservers.
That never was much of a problem (and I didn't find a quick solution) until recently traffic numbers increased and since then MySQL reports the "to many connections" Problems repeatedly, even so 350+ of those connections are in "sleep" state. Also a server can't get a MySQL connection even if there are sleeping connection to that same server.
All those connections vanish when an apache server is restated.
The PHP Code used to create the Database connections uses the normal "mysql" Module, the "mysqli" Module, PEAR::DB and Zend Framework Db Adapter. (Different projects). NONE of the projects uses persistent connections.
Raising the connection-limit is possible but doesn't seem like a good solution since it's 450 now and there are only 20-100 "real" connections at a time anyways.
My question:
Why are there so many connections in sleep state and how can I prevent that?
-- Update:
The Number of Apache requests running at a time never exceeds 50 concurrent requests, so i guess there is a problem with closing the connection or apache keeps the port open without a phpscript attached or something (?)
my.cnf in case it's helpful:
innodb_buffer_pool_size = 1024M
max_allowed_packet = 5M
net_buffer_length = 8K
read_buffer_size = 2M
read_rnd_buffer_size = 8M
query_cache_size = 512M
myisam_sort_buffer_size = 128M
max_connections = 450
thread_cache = 50
key_buffer_size = 1280M
join_buffer_size = 16M
table_cache = 2048
sort_buffer_size = 64M
tmp_table_size = 512M
max_heap_table_size = 512M
thread_concurrency = 8
log-slow-queries = /daten/mysql-log/slow-log
long_query_time = 1
log_queries_not_using_indexes
innodb_additional_mem_pool_size = 64M
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table
Basically, you get connections in the Sleep state when :
a PHP script connects to MySQL
some queries are executed
then, the PHP script does some stuff that takes time
without disconnecting from the DB
and, finally, the PHP script ends
which means it disconnects from the MySQL server
So, you generally end up with many processes in a Sleep state when you have a lot of PHP processes that stay connected, without actually doing anything on the database-side.
A basic idea, so : make sure you don't have PHP processes that run for too long -- or force them to disconnect as soon as they don't need to access the database anymore.
Another thing, that I often see when there is some load on the server :
There are more and more requests coming to Apache
which means many pages to generate
Each PHP script, in order to generate a page, connects to the DB and does some queries
These queries take more and more time, as the load on the DB server increases
Which means more processes keep stacking up
A solution that can help is to reduce the time your queries take -- optimizing the longest ones.
The above solutions like run a query
SET session wait_timeout=600;
Will only work until mysql is restarted. For a persistant solution, edit mysql.conf and add after [mysqld]:
wait_timeout=300
interactive_timeout = 300
Where 300 is the number of seconds you want.
Increasing number of max-connections will not solve the problem.
We were experiencing the same situation on our servers. This is what happens
User open a page/view, that connect to the database, query the database, still query(queries) were not finished and user leave the page or move to some other page.
So the connection that was open, will remains open, and keep increasing number of connections, if there are more users connecting with the db and doing something similar.
You can set interactive_timeout MySQL, bydefault it is 28800 (8hours) to 1 hour
SET interactive_timeout=3600
Before increasing the max_connections variable, you have to check how many non-interactive connection you have by running show processlist command.
If you have many sleep connection, you have to decrease the value of the "wait_timeout" variable to close non-interactive connection after waiting some times.
To show the wait_timeout value:
SHOW SESSION VARIABLES LIKE 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
the value is in second, it means that non-interactive connection still up to 8 hours.
To change the value of "wait_timeout" variable:
SET session wait_timeout=600;
Query OK, 0 rows affected (0.00 sec)
After 10 minutes if the sleep connection still sleeping the mysql or MariaDB drop that connection.
Alright so after trying every solution out there to solve this exact issues on a wordpress blog, I might have done something either really stupid or genius... With no idea why there's an increase in Mysql connections, I used the php script below in my header to kill all sleeping processes..
So every visitor to my site helps in killing the sleeping processes..
<?php
$result = mysql_query("SHOW processlist");
while ($myrow = mysql_fetch_assoc($result)) {
if ($myrow['Command'] == "Sleep") {
mysql_query("KILL {$myrow['Id']}");}
}
?>
So I was running 300 PHP processes simulatenously and was getting a rate of between 60 - 90 per second (my process involves 3x queries). I upped it to 400 and this fell to about 40-50 per second. I dropped it to 200 and am back to between 60 and 90!
So my advice to anyone with this problem is experiment with running less than more and see if it improves. There will be less memory and CPU being used so the processes that do run will have greater ability and the speed may improve.
Look into persistent MySQL connections: I connected using mysqli('p:$HOSTNAME') and had Laravel database.php settings like:
'options' => [
PDO::ATTR_PERSISTENT => true,
],
For some reason, for some time, I believed it was smart to keep connections persistent as I thought my applications would share them. They didn't. They just opened connections and left them unused until they timed out.
After I removed my mad dream of persistency I went from 120-150+ connections from several hosts to only a handful, most of the time actually just one (being the one that runs SHOW PROCESSLIST).

Categories