通過使用Oracle的外部表對Oracle的警告日誌文件、跟蹤文件進行獲取和分析以後,我發現外部表實在是很是易用,甚至到了爲所欲爲的境地(固然外部表尚不能修改外部文件)。html
使用外部表能夠很容易的實現網站的訪問日誌分析。
雖然使用Awstats等工具也能夠實現,但是使用Oracle來分析咱們更應該駕輕就熟。
並且這一切仍是有那麼一點點Cool的。sql
好了,閒言少敘,讓咱們來看一下我分析的過程。
首先建立路徑指向日誌存放目錄:數據庫
[oracle@jumper elog]$ pwd /opt/oracle/elog [oracle@jumper elog]$ ls eygle_access_log.20061016 [oracle@jumper elog]$ sqlplus "/ as sysdba" SQL*Plus: Release 9.2.0.4.0 - Production on Wed Oct 18 08:59:35 2006 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved. Connected to: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production With the Partitioning option JServer Release 9.2.0.4.0 - Production SQL> create or replace directory elog 2 as '/opt/oracle/elog'; Directory created.
而後我將這個路徑的訪問權限授予eygle用戶來進行具體操做:oracle
SQL> grant read,write on directory elog to u_test; Grant succeeded.
選擇合適的分隔符建立外部表:工具
SQL> connect u_test/pwd_test Connected. SQL> create table u_test_access_log_20061016 2 ( ip_address_date varchar2(100), 3 acc_file varchar2(400), 4 acc_cdsz varchar2(20), 5 acc_url varchar2(400), 6 left_blank varchar2(10), 7 acc_agent varchar2(400)) 8 organization external ( 9 type oracle_loader 10 default directory ELOG 11 access parameters ( 12 records delimited by newline 13 nobadfile 14 nodiscardfile 15 nologfile 16 fields terminated by '"' 17 missing field values are null 18 ) 19 location('u_test_access_log.20061016') 20 ) reject limit unlimited 21 / Table created.
此時咱們就能夠對網站的2006年10月16日的訪問日誌進行分析了。性能
咱們能夠先看一下各個字段的分界結果,示例以下:網站
SQL> select ip_address_date from u_test_access_log_20061016 2 where rownum <11; IP_ADDRESS_DATE ------------------------------------------------------------- 38.102.128.140 - - [16/Oct/2006:00:00:17 +0800] 66.249.65.113 - - [16/Oct/2006:00:00:19 +0800] 202.160.178.221 - - [16/Oct/2006:00:00:35 +0800] 59.36.78.100 - - [16/Oct/2006:00:00:37 +0800] 59.36.78.100 - - [16/Oct/2006:00:00:38 +0800] 72.30.61.8 - - [16/Oct/2006:00:00:38 +0800] 221.217.84.230 - - [16/Oct/2006:00:00:42 +0800] 221.217.84.230 - - [16/Oct/2006:00:00:42 +0800] 74.6.65.236 - - [16/Oct/2006:00:01:07 +0800] 74.6.73.36 - - [16/Oct/2006:00:01:09 +0800] 10 rows selected.
經過SQL析取出訪問的ip地址:ui
SQL> select substr(ip_address_date,1,instr(ip_address_date,' ')) ip_address 2 from u_test_access_log_20061016 where rownum <11; IP_ADDRESS --------------------------------------------------------------------------- 38.102.128.140 66.249.65.113 202.160.178.221 59.36.78.100 59.36.78.100 72.30.61.8 221.217.84.230 221.217.84.230 74.6.65.236 74.6.73.36 10 rows selected.
接下來咱們就能夠很容易的得到當日訪問我站點的獨立IP數量了:url
SQL> set timing on SQL> select count(distinct(substr(ip_address_date,1,instr(ip_address_date,' ')))) uip 2 from u_test_access_log_20061016; UIP ---------- 7534 Elapsed: 00:00:06.86
由於外部表的處理性能上要差一些,咱們記錄了一下時間,以上查詢大約用了7秒的時間。.net
咱們能夠對比一下數據庫表的性能。
首先將日誌加載到數據庫表中:
SQL> create table ealog as 2 select * from u_test_access_log_20061016; Table created. SQL> desc ealog; Name Null? Type ----------------------------------------------------------------- -------- -------------------------------------------- IP_ADDRESS_DATE VARCHAR2(100) ACC_FILE VARCHAR2(400) ACC_CDSZ VARCHAR2(20) ACC_URL VARCHAR2(400) LEFT_BLANK VARCHAR2(10) ACC_AGENT VARCHAR2(400) SQL> select count(*) from ealog; COUNT(*) ---------- 165443
而後咱們強制刷新Buffer Cache,消除Cache的影響,再次執行查詢: