Jump to content

[SOLVED] Optimization


The Little Guy

Recommended Posts

I am trying to do a query on a table that is ~140MB in size, containing nearly 400,000 rows.

 

What I would like to do is try to optimize this:

 

		$sql1 = mysql_query(sprintf("SELECT COUNT(DISTINCT ip) as uniqueCount FROM stats WHERE 
		owner_id = '%s' AND
		project_id = '%s' AND
		`date` >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)
		GROUP BY project_id",
		mysql_real_escape_string($row['owner_id']),
		mysql_real_escape_string($row['id'])))or die(mysql_error());
	$sql2 = mysql_query(sprintf("SELECT COUNT(*) as totalCount FROM stats WHERE 
		owner_id = '%s' AND
		project_id = '%s' AND
		`date` >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)
		GROUP BY project_id",
		mysql_real_escape_string($row['owner_id']),
		mysql_real_escape_string($row['id'])));
	$row1 = array_merge(mysql_fetch_array($sql1), mysql_fetch_array($sql2));

 

$sql1 is getting a count of all the individual ip addresses within the last month

$sql2 is getting a count of all the ip addresses (duplicates included) within the last month

 

If I were to put this in a loop and run it 4 times each with a different ower_id and different project_id, it takes about 1 minute, which to me seems too slow, so how can I optimize this?

Link to comment
Share on other sites

$sql1 = mysql_query(sprintf("SELECT COUNT(DISTINCT ip) as uniqueCount, COUNT(ip) As allIps FROM stats WHERE

owner_id = '%s' AND

project_id = '%s' AND

`date` >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)

GROUP BY project_id",

mysql_real_escape_string($row['owner_id']),

mysql_real_escape_string($row['id'])))or die(mysql_error());

 

u could try this but I dont know if it'd work ;P

Link to comment
Share on other sites

One way is to not do count(*) instead do count(ip) it should speed up the SQL.

 

Other than that, no clue.

 

EDIT:

Reason being is counting 1 column is more efficient then counting all columns. Depending how many columns you have that could really speed it up. Anyhow that is just better practice.

Link to comment
Share on other sites

$sql1 = mysql_query(sprintf("SELECT COUNT(DISTINCT ip) as uniqueCount, COUNT(ip) As allIps FROM stats WHERE

owner_id = '%s' AND

project_id = '%s' AND

`date` >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)

GROUP BY project_id",

mysql_real_escape_string($row['owner_id']),

mysql_real_escape_string($row['id'])))or die(mysql_error());

 

u could try this but I dont know if it'd work ;P

 

This is what it said for just one query, and for not multiple:

Showing rows 0 - 0 (1 total, Query took 10.2801 sec)
Link to comment
Share on other sites

$sql1 = mysql_query(sprintf("SELECT COUNT(DISTINCT ip) as uniqueCount, COUNT(ip) As allIps FROM stats WHERE

owner_id = '%s' AND

project_id = '%s' AND

`date` >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)

GROUP BY project_id",

mysql_real_escape_string($row['owner_id']),

mysql_real_escape_string($row['id'])))or die(mysql_error());

 

u could try this but I dont know if it'd work ;P

 

This is what it said for just one query, and for not multiple:

Showing rows 0 - 0 (1 total, Query took 10.2801 sec)

 

For that many rows with doing it multiple times, I would say you are sort of stuck up a tree. The query times will vary depending on how many ips the owner/project has.

 

Limit it to a certain number, or cache the results and update it every x hours in the backend with CRON. Doubt it helps, but yea.

 

EDIT:

Thinking about it, why do multiple loops? Why not use the IN() keyword in mysql and pull it up where projects are IN that id? Or owners are IN that id then just use the loop and display all the projects for an owner? A single query would speed it up a ton.

Link to comment
Share on other sites

$sql1 = mysql_query(sprintf("SELECT COUNT(DISTINCT ip) as uniqueCount, COUNT(ip) As allIps FROM stats WHERE

owner_id = '%s' AND

project_id = '%s' AND

`date` >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)

GROUP BY project_id",

mysql_real_escape_string($row['owner_id']),

mysql_real_escape_string($row['id'])))or die(mysql_error());

 

u could try this but I dont know if it'd work ;P

 

This is what it said for just one query, and for not multiple:

Showing rows 0 - 0 (1 total, Query took 10.2801 sec)

 

For that many rows with doing it multiple times, I would say you are sort of stuck up a tree. The query times will vary depending on how many ips the owner/project has.

 

Limit it to a certain number, or cache the results and update it every x hours in the backend with CRON. Doubt it helps, but yea.

 

EDIT:

Thinking about it, why do multiple loops? Why not use the IN() keyword in mysql and pull it up where projects are IN that id? Or owners are IN that id then just use the loop and display all the projects for an owner? A single query would speed it up a ton.

 

This is the cron, I am trying to speed up my cron

Link to comment
Share on other sites

$sql1 = mysql_query(sprintf("SELECT COUNT(DISTINCT ip) as uniqueCount, COUNT(ip) As allIps FROM stats WHERE

owner_id = '%s' AND

project_id = '%s' AND

`date` >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)

GROUP BY project_id",

mysql_real_escape_string($row['owner_id']),

mysql_real_escape_string($row['id'])))or die(mysql_error());

 

u could try this but I dont know if it'd work ;P

 

This is what it said for just one query, and for not multiple:

Showing rows 0 - 0 (1 total, Query took 10.2801 sec)

 

For that many rows with doing it multiple times, I would say you are sort of stuck up a tree. The query times will vary depending on how many ips the owner/project has.

 

Limit it to a certain number, or cache the results and update it every x hours in the backend with CRON. Doubt it helps, but yea.

 

EDIT:

Thinking about it, why do multiple loops? Why not use the IN() keyword in mysql and pull it up where projects are IN that id? Or owners are IN that id then just use the loop and display all the projects for an owner? A single query would speed it up a ton.

 

This is the cron, I am trying to speed up my cron

 

Gotcha. You found the indexing feature, that was my next suggestion. Other than that I am not sure. You may try doing the date part with PHP, meaning instead of using DATE_SUB put the actual date in there you get from PHP. That way MySQL does not have to keep processing the DATE_SUB function...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.