这个SQLite查询有效吗?

时间:2014-12-10 14:22:01

标签: php sqlite

我正在编写一个访客计数器。它是 PHP5 / SQLite3 组合。

制作了两个数据库表,一个用于访问者,另一个用于点击。结构和样本数据:

CREATE TABLE 'visitors' (
'id' INTEGER DEFAULT NULL PRIMARY KEY AUTOINCREMENT,
'ip' TEXT DEFAULT NULL,
'hash' TEXT DEFAULT NULL,
UNIQUE(ip)
);

INSERT INTO "visitors" ("id","ip","hash") VALUES ('1','1.2.3.4','f9702c362aa9f1b05002804e3a65280b');
INSERT INTO "visitors" ("id","ip","hash") VALUES ('2','1.2.3.5','43dc8b0a4773e45deab131957684867b');
INSERT INTO "visitors" ("id","ip","hash") VALUES ('3','1.2.3.6','9ae1c21fc74b2a3c1007edf679c3f144');

CREATE TABLE 'hits' (
'id' INTEGER DEFAULT NULL PRIMARY KEY AUTOINCREMENT,
'time' INTEGER DEFAULT NULL,
'visitor_id' INTEGER DEFAULT NULL,
'host' TEXT DEFAULT NULL,
'location' TEXT DEFAULT NULL
);

INSERT INTO "hits" ("id","time","visitor_id","host","location") VALUES ('1','1418219548','1','localhost','/some/path/example.php');
INSERT INTO "hits" ("id","time","visitor_id","host","location") VALUES ('2','1418219550','1','localhost','/some/path/example.php');
INSERT INTO "hits" ("id","time","visitor_id","host","location") VALUES ('3','1418219553','1','localhost','/some/path/example.php');
INSERT INTO "hits" ("id","time","visitor_id","host","location") VALUES ('4','1418219555','2','localhost','/some/path/example.php');
INSERT INTO "hits" ("id","time","visitor_id","host","location") VALUES ('5','1418219557','1','localhost','/some/path/example.php');
INSERT INTO "hits" ("id","time","visitor_id","host","location") VALUES ('6','1418219558','3','localhost','/some/path/example.php');

我现在想要获取访问者数据,但仅限于那些在过去30秒内处于活动状态的人。我需要以下数据作为输出,这里以用户ID 1为例:

$visitor = Array(
    [id] => 1
    [ip] => 1.2.3.4
    [hash] => f9702c362aa9f1b05002804e3a65280b
    [first_hit] => 1418219548
    [last_hit] => 1418219557
    [last_host] => localhost
    [last_location] => /some/path/example.php
    [total_hits] => 4
    [idle_since] => 11
)

我会用我当前的查询得到这个,一切都很好,但是你可以看到我需要很多次选择:

SELECT 
   visitors.id,
   visitors.ip,
   visitors.hash,
   (SELECT hits.time FROM hits WHERE hits.visitor_id = visitors.id ORDER BY hits.id ASC LIMIT 1) AS first_hit,
   (SELECT hits.time FROM hits WHERE hits.visitor_id = visitors.id ORDER BY hits.id DESC LIMIT 1) AS last_hit,
   (SELECT hits.host FROM hits WHERE hits.visitor_id = visitors.id ORDER BY hits.id DESC LIMIT 1) AS last_host,
   (SELECT hits.location FROM hits WHERE hits.visitor_id = visitors.id ORDER BY hits.id DESC LIMIT 1) AS last_location,
   (SELECT COUNT(hits.id) FROM hits WHERE hits.visitor_id = visitors.id) AS total_hits,
   (SELECT strftime('%s','now') - hits.time FROM hits WHERE hits.visitor_id = visitors.id ORDER BY hits.id DESC LIMIT 1) AS idle_since
FROM visitors
WHERE idle_since < 30
ORDER BY last_hit DESC

所以,这对我的用例是否可以,或者你知道更好的方法从这两个表中获取这些数据吗?我已经玩过JOINS,但不管我怎么调整它,COUNT()给了我错误的输出,例如用户ID 1只有一个总命中。

我可能不得不重新建模数据库,如果我想正确使用JOINS,我想。


更新:基于AeroX'答案我已经构建了新查询。它基本上只有一个小bug。你不能在WHERE子句中有MAX()。在GROUPING之后使用HAVING。 我还使用EXPLAIN和EXPLAIN QUERY PLAN测试了旧的和新的。看起来好多了。谢谢你们!

SELECT
    V.id,
    V.ip,
    V.hash,
    MIN(H.time) AS first_hit,
    MAX(H.time) AS last_hit,
    strftime('%s','now') - MAX(H.time) AS idle_since,
    COUNT(H.id) AS total_hits,
    LH.host AS last_host,
    LH.location AS last_location
FROM visitors AS V

INNER JOIN hits AS H ON (V.id = H.visitor_id)

INNER JOIN (
    SELECT visitor_id, MAX(id) AS id
    FROM hits
    GROUP BY visitor_id
) AS L ON (V.id = L.visitor_id)

INNER JOIN hits AS LH ON (L.id = LH.id)

GROUP BY V.id, V.ip, V.hash, LH.host, LH.location

HAVING idle_since < 30

ORDER BY last_hit DESC

2 个答案:

答案 0 :(得分:1)

衡量查询效果的最佳方法之一是使用explain

来自sqlite

  

EXPLAIN QUERY PLAN SQL命令用于获取高级别   SQLite用于实现的策略或计划的描述   特定的SQL查询。最重要的是,EXPLAIN QUERY PLAN报告   查询使用数据库索引的方式。这份文件是   理解和解释EXPLAIN QUERY PLAN输出的指南。   背景信息可单独获得:

Notes on the query optimizer.
How indexing works.
The next generation query planner. 
     

EXPLAIN QUERY PLAN命令返回零行或多行四行   各列。列名是“selectid”,“order”,“from”,   “详情”。前三列包含整数值。决赛   列,“细节”,包含一个文本值,其中包含大部分内容   有用的信息。

     

EXPLAIN QUERY PLAN在SELECT语句中最有用,但也可能   与其他从数据库表中读取数据的语句一起出现   (例如UPDATE,DELETE,INSERT INTO ... SELECT)。

explain查询的示例是:

EXPLAIN SELECT * FROM COMPANY WHERE Salary >= 20000;

http://www.tutorialspoint.com/sqlite/sqlite_explain.htm

以下是更复杂的用法示例。

How can I analyse a Sqlite query execution?

答案 1 :(得分:1)

您可能想要清理它,但这应该让您了解如何进行连接以及如何使用GROUP BY语句聚合每个访问者的命中表。这应该比使用大量子查询更有效。

我已经加入了关于联接的评论,以便您了解我为什么要制作它们。

SELECT 
   V.id,
   V.ip,
   V.hash,
  MIN(H.time) AS first_hit,
  MAX(H.time) AS last_hit,
  COUNT(H.id) AS total_hits,
  strftime('%s','now') - MAX(H.time) AS idle_since,
  LH.host AS last_host,
  LH.location AS last_location
FROM visitors AS V
-- Join hits table so we can calculate aggregates (MIN/MAX/COUNT)
INNER JOIN hits AS H ON (V.id = H.visitor_id)
-- Join a sub-query as a table which contains the most recent hit.id for each visitor.id
INNER JOIN (
  SELECT visitor_id, MAX(id) AS id
  FROM hits
  GROUP BY visitor_id
) AS L ON (V.id = L.visitor_id)
-- Use the most recent hit.id for each visitor.id to fetch that most recent row (for last_host/last_location)
INNER JOIN hits AS LH ON (L.id = LH.id)
GROUP BY V.id, V.ip, V.hash, LH.host, LH.location
HAVING idle_since < 30
ORDER BY last_hit DESC