ipel
ipel

Reputation: 1346

Optimize a PHP / MySQL script to reduce server usage

I have a website with more than 1'000'000 daily pageview and a php script to display 2-3 banner ads on each page and count impressions

To count the impressions, after each banner there is an image:

<img src="https://example.com/impression.php?client=ABC&banner=XYZ" width=1 height=1>

This is an example of the impression script:

require('global.php'); // connect to mysql, read default settings, etc

if( isset( $_GET['client'] ) ) && !empty( $_GET['client'] ) ) { $client = secure( $_GET['client'] ); } else { die('error param'); }
if( isset( $_GET['banner'] ) ) && !empty( $_GET['banner'] ) ) { $banner = secure( $_GET['banner'] ); } else { die('error param'); }

$check_client = mysqli_query( $con, "SELECT id FROM clients WHERE id = '$client' AND status = 1 LIMIT 1" ) or die( 'error mysql' );
if( mysqli_num_rows( $check_client ) == 0 ) { die('error client'); }

$check_banner = mysqli_query( $con, "SELECT campaign_id FROM banners WHERE id = '$banner' AND status = 1 LIMIT 1" ) or die( 'error mysql' );
if( mysqli_num_rows( $check_banner ) == 0 ) { die('error banner'); }    
$read_campaign_id = mysqli_fetch_array( $check_banner ); 

$check_campaign = mysqli_query( $con, "SELECT id FROM campaigns WHERE id = '$read_campaign_id[0]' AND status = 1 LIMIT 1" ) or die( 'error mysql' );
if( mysqli_num_rows( $check_campaign ) == 0 ) { die('error campaign' );  }

$check_unique = mysqli_query( $con, "SELECT id FROM impressions WHERE datetime LIKE '$today%' AND banner = '$banner' AND ip = '$ip' ORDER BY datetime DESC LIMIT 1" ) or die( 'error mysql');
if( mysqli_num_rows( $check_unique ) == 1 ) { $unique = 0; } else { $unique = 1; }

mysqli_query( $con, "INSERT INTO impressions ( client, campaign, banner, datetime, unique, ip, page ) VALUES ( '$client', '$read_campaign_id[0]', '$banner', '$now', '$unique', '$ip', '$url' )" ) or die( 'error mysql' );

header( 'Content-type: image/png' );
header( $path . '/img/pixel.png' );
mysqli_close( $con );
exit;

In this case what is the best way to optimize the script and reduce resources usage and concurrent connections on my server?

EDIT: This is the mysql table structure (only with the useful fields for this example) http://sqlfiddle.com/#!2/1ffe3/1

Upvotes: 0

Views: 295

Answers (1)

symcbean
symcbean

Reputation: 48387

Yes, you can run all the selects in a single query.

Rather confusingly (as you only require one row from each, and there is no obvious relation between them) you can do this with a cartesian product.

Your schema sucks (WHERE datetime LIKE '$today%' using a string to store a date!!!!)

Why are you ordering the result of $check_unique when you only want to see if any row exists?

SELECT clients.id AS client_id
, banner.campaign_id AS banner_campaign_id
, campaign_id
,(SELECT COUNT(*)
   FROM impressions 
   WHERE impressions.datetime LIKE '$today%' 
   AND impressions.banner = '$banner' 
   AND ip = '$ip' 
   LIMIT 1) AS check_unique
FROM clients
INNER JOIN banners
INNER JOIN campaigns
WHERE clients.id = '$client' AND clients.status = 1
AND banners.id = '$banner' AND banners.status = 1
AND campaigns.id = '$read_campaign_id[0]' AND campaigns.status = 1
LIMIT 1

Alternatively you could unroll it into a UNION but that's much less fun.

Upvotes: 1

Related Questions