开发者

Meaning of SQL-code in reviewed code

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开发者_如何学JAVA `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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜