Sphinx+Mysql+Php 12億DNS資料秒查

來源:互聯網
上載者:User
最近得到一個接近12億的全球ns節點的資料,本來想用來做一個全國通過dns反查網域名稱然後進行全國範圍的網站收集和掃描的,後來發現網站的數量不是很準確,加上一個人的精力和財力實在難以完成這樣一個龐大的任務,就沒有做下去,只留下了這個搭建的筆記。
文字格式設定,簡單的文本搜尋,速度太慢,一次搜尋接近花掉5-10分鐘時間,決定將其倒入資料庫進行一次最佳化,速度應該能提升不到,電腦上只有AMP的環境,那麼就決定將其倒入到mysql中,
一開始使用Navicat進行倒入,剛好資料的格式是 ip,ns 這樣的格式,倒入了接近5個小時發現還沒有倒入到百分之一,這可是純文字格式化的時候大小為54G的資料檔案啊!
後來發現用mysql內建的load data local infile只話了30分鐘左右,第一次匯入的時候忘記建立鍵了,只好重新匯入一次

mysql> load data local infile 'E:\\dns\\rite\\20141217-rdns.txt' into table dnsfields terminated by ',';Query OK, 1194674130 rows affected, 1700 warnings (29 min 26.65 sec)Records: 1194674130  Deleted: 0  Skipped: 0  Warnings: 1700

因為添加了一個id欄位,所以匯入速度明顯下降,不過大概也只花了1個半小時左右的時間就完成了55G資料的匯入。
接著是建立索引,因為我需要的模糊查詢,所以在這裡建立的是Full Text+Btree,差不多花了3天時間索引才建立完成,期間因為一不小心把mysql的執行視窗關閉了,以為就這麼完蛋了,最後發現其實mysql還在後台默默的建立索引。
建立了索引之後發現查詢速度也就比沒有建立索引快那麼一點,執行了一條

select * from ns where ns like '%weibo.com'

花掉了210秒的時間,還是太慢了。
然後就開始使用SPhinx來做索引提升速度,
從官方下載了64位的SPHINX MYSQL SUPPORT的包下載地址
接著配置設定檔,src裡配置要mysql的帳號密碼

source src1{    sql_host        = localhost    sql_user        = root    sql_pass        = root    sql_db          = ns    sql_port        = 3306      sql_query       = \        SELECT id,ip,ns from ns //這裡寫上查詢語句    sql_attr_uint       = id

然後searchd裡也需要配置一下,連接埠和日誌,pid檔案的路徑配置好即可

searchd{    listen          = 9312    listen          = 9306:mysql41    log         = E:/phpStudy/splinx/file/log.log    query_log       = E:/phpStudy/splinx/file/query.log    pid_file        = E:/phpStudy/splinx/file/searchd.pid

然後切換到sphinx的bin目錄進行建立索引,執行

searchd test1 #test1是你source的名稱

我大概建立了不到2個小時的時間就建立完成了,
然後切換到api目錄下執行

E:\phpStudy\splinx\api>test.py asdDEPRECATED: Do not call this method or, even better, use SphinxQL instead of anAPIQuery 'asd ' retrieved 1000 of 209273 matches in 0.007 secQuery stats:        'asd' found 209291 times in 209273 documentsMatches:1. doc_id=20830, weight=12. doc_id=63547, weight=13. doc_id=96147, weight=14. doc_id=1717000, weight=15. doc_id=2213385, weight=16. doc_id=3916825, weight=17. doc_id=3981791, weight=18. doc_id=5489598, weight=19. doc_id=9348383, weight=110. doc_id=18194414, weight=111. doc_id=18194415, weight=112. doc_id=18195126, weight=113. doc_id=18195517, weight=114. doc_id=18195518, weight=115. doc_id=18195519, weight=116. doc_id=18195520, weight=117. doc_id=18195781, weight=118. doc_id=18195782, weight=119. doc_id=18200301, weight=120. doc_id=18200303, weight=1

進行了測試,發現速度真的很快,寫了一個PHP指令碼進行調用

SetServer ( '127.0.0.1', 9312 );$result = $sphinx->query ('weibo.com', 'test1'); foreach($result['matches'] as $key => $val){    $sql="select * from ns where id='{$key}'";    $res=mysql_query($sql);    $res=mysql_fetch_array($res);    echo "{$res['ip']}:{$res['ns']}";}echo time()-$now;?>

基本實現了秒查!,最後輸出的時間只花掉了0!

123.125.104.176:w-176.service.weibo.com123.125.104.178:w-178.service.weibo.com123.125.104.179:w-179.service.weibo.com123.125.104.207:w-207.service.weibo.com123.125.104.208:w-208.service.weibo.com123.125.104.209:w-209.service.weibo.com123.125.104.210:w-210.service.weibo.com202.106.169.235:staff.weibo.com210.242.10.56:weibo.com.tw218.30.114.174:w114-174.service.weibo.com219.142.118.228:staff.weibo.com60.28.2.221:w-221.hao.weibo.com60.28.2.222:w-222.hao.weibo.com60.28.2.250:w-222.hao.weibo.com61.135.152.194:sina152-194.staff.weibo.com61.135.152.212:sina152-212.staff.weibo.com65.111.180.3:pr1.cn-weibo.com160.34.0.155:srm-weibo.us2.cloud.oracle.com202.126.57.40:w1.weibo.vip.hk3.tvb.com202.126.57.41:w1.weibo.hk3.tvb.com0
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.