在花1塊錢,購買了那個《服務器日志分析與流量統計》這個插件, 用來分析日志和流量, 當www.wxtv.net流量巨大, 短短幾天就產生了8百萬條網站日志, 使用的是mysql存儲日志的方式, 該插件的作者幾乎沒有優化過mysql存儲方式(我也不知道sqlite方式有沒有優化), 大幾百萬的日志, 查詢幾乎打不開, 還把整個mysql拖死。 打開mysql的慢日志查詢, 發現全是統計的語句超時了。
慢日志記錄:
SET timestamp=1590377705; select website,ip,CAST(time AS CHAR) AS time,httpstatus,size,httpmothed,pageurl,shebieinfo from weblogs where website='www.wxtv.net' and time>='2020-05-2100:00:01' and time<='2020-05-2123:59:59' and shebieinfo like '%baidus%' order by time desc limit 0,100; # Time: 200525 11:35:10 # User@Host: wlog[wlog] ****** Id: 1230472 # Query_time: 4.893739 Lock_time: 0.000176 Rows_sent: 1 Rows_examined: 2227923 SET timestamp=1590377710; select count(ip) from weblogs where website='www.wxtv.net' and time>='2020-05-2100:00:01' and time<='2020-05-2123:59:59' and shebieinfo like '%baidus%' order by time desc;
解決辦法:
增加weblogs表的time字段的索引。 操作步驟如下:
1. 點擊左邊菜單“數據庫”。
2. 選擇當時日志配置的mysql數據庫, 點擊管理,打開phpMyAdmin。
3. 在phpMyAdmin中, 選擇日志的數據庫。 點擊結構。
4. 點擊time字段操作列中的索引, 給time字段新建索引。
建好索引后, 再打開《服務器日志分析與流量統計》這個插件, 查詢速度變成了秒級。
PS: 該款插件只賣1塊錢, 所謂1分錢一分貨, 作者在mysql方式上, 幾乎都沒有測試。 買過來, 可以打開它的源碼, 修改代碼, 在服務器上進行優化。