I'm faced with this issue only on high volume traffic sites.
Here is the problem and this is wordpress related as well.
I'm counting page hits using an ajax function that gathers a couple details about the page and adds the info to a separate database on the same server.
Everything works fine for a while then the site starts to load slow and at times will say, Can't Connect to Database and the site stops loading for a few seconds or so.
This problem happens mainly I'm guessing because of the massive amount of traffic hitting all the pages on site (over 30-50k a day) and after a while the site is having trouble keeping up with the ajax function requests and or writing to the other database.
Question is, how can I get this to count on large scale traffic sites and not keep getting a slow or crashing database. BTW. Site is on a dedicated server and the wordpress site does have a caching plugin installed.
if ($user_role != 'author' && $user_role != 'administrator' && $user_role != 'uploader' && $user_role != 'editor' && $user_role != 'contributor') {
$insert = "INSERT IGNORE INTO " . $pps_new_table_name . "( post_id, post_author, create_date, hit_count, {$browser_column}, {$device_column} ) VALUES (" . $_POST['post_id'] . ",'" . $_POST['post_author'] . "','" . $create_date . "','1','1','1')
ON DUPLICATE KEY UPDATE hit_count=hit_count + 1, {$browser_column}= {$browser_column} + 1, {$device_column}= {$device_column} + 1
";
$results = $wpdb->query($wpdb->prepare($insert));
$wpdb->show_errors();
}
die();