小编Cri*_*scu的帖子

TSQL 性能 - 加入最小值和最大值之间的值

我有两张表用于存储:

  • IP 范围 - 国家/地区查找表
  • 来自不同 IP 的请求列表

IP 存储为bigints 以提高查找性能。

这是表结构:

create table [dbo].[ip2country](
    [begin_ip] [varchar](15) NOT NULL,
    [end_ip] [varchar](15) NOT NULL,
    [begin_num] [bigint] NOT NULL,
    [end_num] [bigint] NOT NULL,
    [IDCountry] [int] NULL,
    constraint [PK_ip2country] PRIMARY KEY CLUSTERED 
    (
        [begin_num] ASC,
        [end_num] ASC
    )
)

create table Request(
    Id int identity primary key, 
    [Date] datetime, 
    IP bigint, 
    CategoryId int
)
Run Code Online (Sandbox Code Playgroud)

我想获取每个国家/地区的请求细分,因此我执行以下查询:

select 
    ic.IDCountry,
    count(r.Id) as CountryCount
from Request r
left join ip2country ic 
  on r.IP between ic.begin_num …
Run Code Online (Sandbox Code Playgroud)

performance sql-server-2008 t-sql query-performance

10
推荐指数
1
解决办法
1万
查看次数