views:

50

answers:

1

I need review some code of test-application written in PHP/MySQL. Author of this code wrote three SQL-queries. I can't understand, if he meant here some performace optimization?

 DB::fetch("SELECT COUNT( *  ) AS count, `ip`,`datetime`
FROM `logs`
WHERE `datetime` > ('2006-02-03' - INTERVAL 10 DAY)
GROUP BY `ip`
ORDER BY `datetime` DESC");

$hits = DB::fetchAll("SELECT COUNT( *  ) AS count, `datetime`
FROM `logs`
WHERE `datetime` > ( '2006-02-03' - INTERVAL 10
DAY ) AND `is_doc` = 1
GROUP BY `datetime`
ORDER BY `datetime` DESC");

$hosts = DB::fetchAll("SELECT COUNT( *  ) AS hosts , datetime
FROM (

SELECT `ip` , datetime
FROM `logs`
WHERE `is_doc` = 1
GROUP BY `datetime` , `ip`
ORDER BY `logs`.`datetime` DESC
) AS cnt
WHERE cnt.datetime > ( '2006-02-03' - INTERVAL 10
DAY )
GROUP BY cnt.datetime
ORDER BY datetime DESC ");

Results of first query are not used in application.

+2  A: 

The 1st query is invalid, as it selects 2 columns + 1 aggregate and only groups by 1 of the 2 columns selected.

The 2nd query is getting a count of all rows in logs by date within the last 10 days since 2006-02-03

The 3rd query is getting a count of all distinct ip values from logs within the last 10 days since 2006-02-03 and could be better written as

SELECT COUNT(DISTINCT ip) hosts, datetime
FROM logs
WHERE is_doc = 1
GROUP BY datetime
ORDER BY datetime desc

If this was a submission for a job iterview you may wonder why the cutoff date isn't passed as a variable.

Jon Freedman
Ya looks like just some reporting on whatever is being captured in the logs
Wil