查询一个80万的IP地址数据库,VB程序会在查询的时候停顿,必须在查询完成后才能进行其它操作,请教高手如何处理?Function fwip(ipp)
Call LinkDB
Dim iCount As Long
    Dim strUsers As String
    Dim adoRs As New ADODB.Recordset
    Dim strTemp As String
    'ipp = "25.65.45.78"
If ipp <> "unknown" And ipp <> "" And InStr(ipp, ".") > 1 Then
sip = Split(ipp, ".")
ssip = sip(0) * 256 * 256 * 256 + sip(1) * 256 * 256 + sip(2) * 256 + sip(3)
SQL = ("select top 1 ip2,country,city from address where ip2 > " & ssip & " order by ip2 asc ")
adoRs.open SQL, Conns, 1, 1
If Not adoRs.EOF Then
Add = adoRs("country") & adoRs("city")
Else
Add = "未知地址"
End If
adoRs.Close
End If
Conns.Close
Set adoRs = Nothing
Set Conns = NothingEnd FunctionPrivate Function LinkDB() '连接数据库代码
    On Error GoTo LinkDB_Err
    
    Dim strConn As String
    
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & App.Path & "\msnabaga.mdb" '设置连接字串,连接数据库
    Conns.open strConn
    
   Exit FunctionLinkDB_Err:
    MsgBox Err.Number & vbCrLf & Err.Description, , GstrAppTitle
End Function

解决方案 »

  1.   

    select top 1 ip2,country,city from address where ip2 > " & ssip & " order by ip2 asc "
    这个查询语句一点效率都没有....
      

  2.   

    --> liangCK小梁 于2008-09-20
    --> 生成测试数据: [tableip]
    if object_id('[tableip]') is not null drop table [tableip]
    create table [tableip] (id int,startip varchar(15),endip varchar(15),address nvarchar(6))
    insert into [tableip]
    select 1,'024.089.048.000','024.089.063.255','ARIN' union all
    select 2,'024.089.064.000','024.089.127.255','加拿大' union all
    select 3,'024.089.128.000','024.089.191.255','美国' union all
    select 4,'024.089.192.000','024.089.255.255','加拿大' union all
    select 5,'024.090.000.000','024.100.063.255','美国' union all
    select 6,'024.100.064.000','024.103.255.255','ARIN' union all
    select 7,'024.104.000.000','024.104.159.255','美国' union all
    select 8,'024.104.160.000','024.104.255.255','ARIN'--SQL查询如下:go--这个函数不用改.直接搬着来用.
    CREATE FUNCTION dbo.f_IP2Int(
    @ip varchar(15)
    )RETURNS bigint
    AS
    BEGIN
        DECLARE @re bigint
        SET @re=0
        SELECT @re=@re+LEFT(@ip,CHARINDEX('.',@ip+'.')-1)*ID
            ,@ip=STUFF(@ip,1,CHARINDEX('.',@ip+'.'),'')
        FROM(
            SELECT ID=CAST(16777216 as bigint)
            UNION ALL SELECT 65536
            UNION ALL SELECT 256
            UNION ALL SELECT 1)a
        RETURN(@re)
    END
    GO--要查的IP
    DECLARE @ip VARCHAR(15);SET @ip='24.93.121.22';SELECT * 
    FROM [tableip]
    WHERE dbo.f_IP2Int(@ip)>=dbo.f_IP2Int(startip) 
       and dbo.f_IP2Int(@ip)<=dbo.f_IP2Int(endip) ;
       
    GO--删除测试表,函数.
    DROP TABLE [tableip]
    DROP FUNCTION dbo.f_IP2Intid          startip         endip           address
    ----------- --------------- --------------- -------
    5           024.090.000.000 024.100.063.255 美国(1 行受影响)
      

  3.   

    ADO默认是阻塞的,如果查询的操作比较耗时,你可以考虑用ActiveX Exe做多线程,或者是用ADO的异步模式。
      

  4.   

    这个正巧我有过一点实际经验。
    我的应用范围纯真IP库.条数41W条
    想快注意几点
    1.IP1,IP2在数据库里建立索引
    2.查询时不要用rs.open用set rs=con.execute()
    这样查起来非常快。根据经验普通比较好的个人PC上能实现1秒钟10-20条查询
      

  5.   

    补本句
    你上面的
    SQL = ("select top 1 ip2,country,city from address where ip2 > " & ssip & " order by ip2 asc ")
    这样写不对头
    因为IP库都是这样的原理起始IP(ip1)  结束IP(ip2) 地区名  国家名无需top来取回多条记录
    所以应该这样来
    Set sRs = sCon.Execute("select country from IPLIST Where ip1<=" & rIP & " And ip2>=" & rIP)上面提的都注意到了每秒上10条是很容易的
      

  6.   

    拼查询速度有几个人或几个公司能拼过Google?