]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/log_daemon/DB/doc/views.sql
Merged from parent (trunk r11240, circa 3.2.0.5+)
[thirdparty/squid.git] / helpers / log_daemon / DB / doc / views.sql
1 --
2 -- Sample views
3 --
4
5 -- ip address of hosts accessing the cache
6 CREATE OR REPLACE VIEW cache_clients AS
7 SELECT DISTINCT ip_client FROM access_log ORDER BY 1;
8
9 -- this index helps view 'cache_clients'
10 CREATE INDEX client_ip_idx ON access_log(ip_client);
11
12
13 -- traffic by client
14 CREATE OR REPLACE VIEW traffic_by_client AS
15 SELECT
16 ip_client,
17 SUM(http_reply_size) AS total_bytes,
18 SUM(http_reply_size)/1024 AS total_kilobytes,
19 SUM(http_reply_size)/1048576 AS total_megabytes
20 FROM access_log
21 GROUP BY 1
22 ORDER BY 1;
23
24 -- most active clients
25 -- same as before, but sorted by traffic;
26 -- show only the 10 most active clients
27 CREATE OR REPLACE VIEW most_active_clients AS
28 SELECT
29 ip_client,
30 SUM(http_reply_size) AS total_bytes,
31 SUM(http_reply_size)/1024 AS total_kilobytes,
32 SUM(http_reply_size)/1048576 AS total_megabytes
33 FROM access_log
34 GROUP BY 1
35 ORDER BY 2 DESC
36 LIMIT 10;
37
38
39 -- traffic per day
40 CREATE OR REPLACE VIEW traffic_per_day AS
41 SELECT
42 date_day,
43 SUM(http_reply_size) AS total_bytes,
44 SUM(http_reply_size)/1024 AS total_kilobytes,
45 SUM(http_reply_size)/1048576 AS total_megabytes
46 FROM access_log
47 GROUP BY 1
48 ORDER BY 1;
49
50 -- traffic by client per day
51 CREATE OR REPLACE VIEW traffic_per_day_per_client AS
52 SELECT
53 date_day,
54 ip_client,
55 SUM(http_reply_size) AS total_bytes,
56 SUM(http_reply_size)/1024 AS total_kilobytes,
57 SUM(http_reply_size)/1048576 AS total_megabytes
58 FROM access_log
59 GROUP BY 1,2
60 ORDER BY 1,2 DESC;
61
62 CREATE OR REPLACE VIEW traffic_per_month_per_client AS
63 SELECT
64 YEAR(date_day) AS date_year,
65 MONTH(date_day) AS date_month,
66 ip_client,
67 SUM(http_reply_size) AS total_bytes,
68 SUM(http_reply_size)/1024 AS total_kilobytes,
69 SUM(http_reply_size)/1048576 AS total_megabytes
70 FROM access_log
71 GROUP BY 2,3
72 ORDER BY 1,2,3;
73
74 -- list of clients with some stats
75 CREATE OR REPLACE VIEW cache_clients_with_infos AS
76 SELECT
77 a.ip_client,
78 COUNT(*) AS total_requests,
79 (COUNT(*)/(SELECT COUNT(*) FROM access_log))*100 AS requests_perc,
80 SUM(a.http_reply_size) AS total_traffic,
81 (SUM(a.http_reply_size)/(SELECT SUM(http_reply_size) FROM access_log))*100 AS traffic_perc,
82 (SELECT COUNT(*) FROM access_log a1 WHERE a1.ip_client=a.ip_client AND squid_request_status LIKE '%HIT%')
83 /
84 (SELECT COUNT(*) FROM access_log)
85 * 100 AS hit_perc,
86 (SELECT COUNT(*) FROM access_log a1 WHERE a1.ip_client=a.ip_client AND squid_request_status LIKE '%MISS%')
87 /
88 (SELECT COUNT(*) FROM access_log)
89 * 100 AS miss_perc,
90 MIN(date_day) AS first_access_date,
91 MIN(date_time) AS first_access_time,
92 MAX(date_day) AS last_access_date,
93 MAX(date_time) AS last_access_time
94 FROM access_log a
95 GROUP BY 1
96 ORDER BY 1;
97
98 -- this index helps view 'cache_clients_with_infos'
99 CREATE INDEX client_req_status_idx ON access_log(ip_client, squid_request_status);
100
101
102 -- number of requests per day
103 CREATE OR REPLACE VIEW requests_per_day AS
104 SELECT
105 DATE(FROM_UNIXTIME(time_since_epoch)) AS date_day,
106 COUNT(*) AS num_of_requests
107 FROM access_log
108 GROUP BY 1
109 ORDER BY 1;
110
111 -- number of requests per minute
112 CREATE OR REPLACE VIEW requests_per_minute AS
113 SELECT
114 DATE(FROM_UNIXTIME(time_since_epoch)) AS date_day,
115 HOUR(FROM_UNIXTIME(time_since_epoch)) AS date_hour,
116 MINUTE(FROM_UNIXTIME(time_since_epoch)) AS date_minute,
117 COUNT(*) AS num_of_requests
118 FROM access_log
119 GROUP BY 1,2,3
120 ORDER BY 1,2,3;
121
122 -- number of requests per day of each cache client
123 CREATE OR REPLACE VIEW requests_per_day_per_client AS
124 SELECT
125 DATE(FROM_UNIXTIME(time_since_epoch)) AS date_day,
126 ip_client,
127 COUNT(*) AS num_of_requests
128 FROM access_log
129 GROUP BY 1,2
130 ORDER BY 1,2;
131
132 -- percentage of each request status
133 CREATE OR REPLACE VIEW requests_status_perc AS
134 SELECT
135 squid_request_status,
136 (COUNT(*)/(SELECT COUNT(*) FROM access_log)*100) AS percentage
137 FROM access_log
138 GROUP BY squid_request_status
139 ORDER BY 2 DESC;
140
141 -- this index helps view 'requests_status_perc'
142 CREATE INDEX req_status_idx ON access_log(squid_request_status);
143
144 -- request hits and misses, in percentage
145 CREATE OR REPLACE VIEW hits_misses_perc AS
146 SELECT
147 'hits',
148 (SELECT COUNT(*) FROM access_log WHERE squid_request_status LIKE '%HIT%')
149 /
150 (SELECT COUNT(*) FROM access_log)*100
151 AS percentage
152 UNION
153 SELECT
154 'misses',
155 (SELECT COUNT(*) FROM access_log WHERE squid_request_status LIKE '%MISS%')
156 /
157 (SELECT COUNT(*) FROM access_log)*100
158 AS pecentage;
159
160 -- response times
161 CREATE OR REPLACE VIEW time_response_ranges AS
162 SELECT
163 '0..500',
164 COUNT(*) / (SELECT COUNT(*) FROM access_log)*100 AS percentage
165 FROM access_log
166 WHERE time_response >= 0 AND time_response < 500
167 UNION
168 SELECT
169 '500..1000',
170 COUNT(*) / (SELECT COUNT(*) FROM access_log)*100 AS percentage
171 FROM access_log
172 WHERE time_response >= 500 AND time_response < 1000
173 UNION
174 SELECT
175 '1000..2000',
176 COUNT(*) / (SELECT COUNT(*) FROM access_log)*100 AS percentage
177 FROM access_log
178 WHERE time_response >= 1000 AND time_response < 2000
179 UNION
180 SELECT
181 '>= 2000',
182 COUNT(*) / (SELECT COUNT(*) FROM access_log)*100 AS percentage
183 FROM access_log
184 WHERE time_response >= 2000;
185
186 -- this index helps view 'time_response_ranges'
187 CREATE INDEX time_response_idx ON access_log(time_response);
188
189 -- response time graph
190 CREATE OR REPLACE VIEW time_response_graph AS
191 SELECT
192 time_response,
193 COUNT(*) AS num_req
194 FROM access_log
195 GROUP BY 1
196 ORDER BY 1;
197
198 -- traffic by mime type
199 CREATE OR REPLACE VIEW traffic_by_http_mime_type AS
200 SELECT
201 http_mime_type,
202 SUM(http_reply_size) as total_bytes
203 FROM access_log
204 GROUP BY http_mime_type
205 ORDER BY 2 DESC;
206
207 -- last 10 queries
208 CREATE OR REPLACE VIEW last_10_queries AS
209 SELECT *
210 FROM access_log
211 WHERE
212 id > (SELECT MAX(id) FROM access_log) - 10
213 ORDER BY id DESC;
214
215 -- id of the last query of each client
216 -- this view is required by the "last n queries by ip" view
217 CREATE OR REPLACE VIEW last_query_by_client AS
218 SELECT
219 ip_client,
220 MAX(id) AS last_query_id
221 FROM access_log
222 GROUP BY ip_client;
223
224
225 -- last 10 queries of each client
226 -- NOTE: this query is conceptually wrong because it assumes that no holes exist
227 -- in the values of column 'id'.
228 -- This can be false if e.g. some access_log entries get deleted...
229 CREATE OR REPLACE VIEW last_10_queries_by_client AS
230 SELECT *
231 FROM access_log a
232 WHERE
233 id > (
234 SELECT l.last_query_id
235 FROM last_query_by_client l
236 WHERE l.ip_client = a.ip_client
237 ) - 10
238 ORDER BY a.ip_client, a.id DESC;
239
240 -- this index helps the "last_10_queries_by_client" view
241 CREATE INDEX client_ip_record_id_idx ON access_log(ip_client, id);
242
243
244 -- number of HIT requests per day
245 CREATE OR REPLACE VIEW hits_per_day AS
246 SELECT
247 date_day,
248 COUNT(*) AS num_hits
249 FROM access_log
250 WHERE squid_request_status LIKE '%HIT%'
251 GROUP BY 1;
252
253 -- HIT requests per day, percent (100% = total number of requests that day)
254 CREATE OR REPLACE VIEW hits_per_day_perc AS
255 SELECT
256 r.date_day,
257 h.num_hits/r.num_of_requests*100 AS hits_per_day_perc
258 FROM requests_per_day r
259 JOIN
260 hits_per_day h
261 ON r.date_day = h.date_day;
262
263
264 -- request methods (count)
265 CREATE OR REPLACE VIEW http_methods AS
266 SELECT
267 http_method,
268 COUNT(*)
269 FROM access_log
270 GROUP BY 1
271 ORDER BY 1;
272
273 -- request methods by percent
274 CREATE OR REPLACE VIEW http_methods_perc AS
275 SELECT
276 http_method,
277 COUNT(*) / (SELECT COUNT(*) FROM access_log) * 100 AS perc
278 FROM access_log
279 GROUP BY 1
280 ORDER BY 2 DESC;
281
282
283 -- slowest queries
284 CREATE OR REPLACE VIEW slowest_requests AS
285 SELECT *
286 FROM access_log
287 ORDER BY time_response DESC
288 LIMIT 10;
289
290
291 CREATE OR REPLACE VIEW slowest_request_by_method AS
292 SELECT *
293 FROM access_log
294 GROUP BY http_method
295 ORDER BY http_method, time_response DESC;
296
297
298 -- requests with the biggest reply size
299 CREATE OR REPLACE VIEW biggest_requests AS
300 SELECT *
301 FROM access_log
302 ORDER BY http_reply_size DESC
303 LIMIT 10;
304
305
306
307 -- list each day which has at least one request, with some statistics
308 CREATE OR REPLACE VIEW days_with_infos AS
309 SELECT
310 date_day,
311 MIN(date_time) AS first_req_time,
312 MAX(date_time) AS last_req_time,
313 COUNT(*) AS number_of_requests,
314 SUM(http_reply_size) AS total_traffic_bytes,
315 SUM(http_reply_size) / 1048576 AS total_traffic_megabytes,
316 COUNT(DISTINCT ip_client) AS number_of_clients,
317 AVG(time_response) AS avg_time_response,
318 MAX(time_response) AS max_time_response,
319
320 -- client that has made the highest number of requests that day
321 (
322 SELECT ip_client
323 FROM requests_per_day_per_client r
324 WHERE r.date_day = a.date_day
325 ORDER BY r.num_of_requests DESC LIMIT 1
326 ) AS most_active_client_r,
327
328 -- the number of requests that client actually made
329 (
330 SELECT r.num_of_requests
331 FROM requests_per_day_per_client r
332 WHERE r.date_day = a.date_day
333 ORDER BY r.num_of_requests DESC LIMIT 1
334 ) AS most_active_client_r_nr,
335
336 -- same info but as percentage on the total number of requests that day
337 -- we have to repeat the whole query because we cannot reference aliases
338 -- defined in previous columns
339 -- a date_day column with an index on it would help here; a view would probably help too...
340 (
341 (
342 SELECT r.num_of_requests
343 FROM requests_per_day_per_client r
344 WHERE r.date_day = a.date_day
345 ORDER BY 1 DESC LIMIT 1
346 ) / (
347 SELECT COUNT(*)
348 FROM access_log a1
349 WHERE a.date_day = a1.date_day
350 ) * 100
351 ) AS most_active_client_r_pc,
352
353 -- client that has generated the highest traffic that day
354 (
355 SELECT t.ip_client
356 FROM traffic_per_day_per_client t
357 WHERE t.date_day = a.date_day
358 ORDER BY t.total_bytes DESC LIMIT 1
359 ) AS most_active_client_t,
360
361 -- the actual traffic generated by that client
362 (
363 SELECT t.total_bytes
364 FROM traffic_per_day_per_client t
365 WHERE t.date_day = a.date_day
366 ORDER BY t.total_bytes DESC LIMIT 1
367 ) AS most_active_client_t_b,
368
369 -- same info expressed in megabytes
370 (
371 SELECT t.total_bytes
372 FROM traffic_per_day_per_client t
373 WHERE t.date_day = a.date_day
374 ORDER BY t.total_bytes DESC LIMIT 1
375 ) / 1048576 AS most_active_client_t_mb,
376
377 -- same info in percentage on the total traffic that day
378 -- see previous comments
379 (
380 (
381 SELECT t.total_bytes
382 FROM traffic_per_day_per_client t
383 WHERE t.date_day = a.date_day
384 ORDER BY t.total_bytes DESC LIMIT 1
385 ) / (
386 SELECT SUM(http_reply_size)
387 FROM access_log a1
388 WHERE a.date_day = a1.date_day
389 ) * 100
390 ) AS most_active_client_t_pc
391
392 FROM access_log a
393 GROUP BY 1
394 ORDER BY 1;
395
396 -- this index helps the "days_with_info" view
397 CREATE INDEX date_day_idx ON access_log(date_day);
398
399
400 CREATE OR REPLACE VIEW requests_in_last_minute AS
401 select * from access_log where time_since_epoch >= ( (select max(time_since_epoch) from access_log) - 60);
402
403
404 CREATE OR REPLACE VIEW avg_req_per_minute AS
405 SELECT COUNT(*) FROM requests_in_last_minute;