【 以下文字转载自 JobHunting 讨论区 】 发信人: light009 (light009), 信区: JobHunting 标 题: SQL fast search in a 10 million records table 发信站: BBS 未名空间站 (Thu Jul 24 23:26:56 2014, 美东) I need to do a fast search in a column with floating point numbers in a table of SQL server 2008 R2 on Win 7. the table has 10 million records. e.g. Id value 532 937598.32421 873 501223.3452 741 9797327.231 ID is primary key, I need o do a search on "value" column for a given value such that I can find the 5 closest points to the given point in the table. The closeness is defined as the absolute value of the difference between the given value and column value. The smaller value, the closer. I would like to use binary search. I want to set an unique index on the value column. But, I am not sure whether the table will be sorted every time when I search the given value in the column ? Or, it only sorts the table one time because I have set the value column as unique index ? Are there better ways to do this search ? Any help would be appreciated. thanks
【在 l******9 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: light009 (light009), 信区: JobHunting : 标 题: SQL fast search in a 10 million records table : 发信站: BBS 未名空间站 (Thu Jul 24 23:26:56 2014, 美东) : I need to do a fast search in a column with floating point numbers in a : table of SQL server 2008 R2 on Win 7. : the table has 10 million records. : e.g. : Id value : 532 937598.32421
进来学习。 Binary search是说用B tree index? ID是primary key的话,在这个search key上的index通常是clustered and ordered. 可以在value上建secondary nonclustered index,怎么order应该可以自己指定吧? 另外,value是不是一个nonunique search key。 能不能考虑用composite search key?
【在 l******9 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: light009 (light009), 信区: JobHunting : 标 题: SQL fast search in a 10 million records table : 发信站: BBS 未名空间站 (Thu Jul 24 23:26:56 2014, 美东) : I need to do a fast search in a column with floating point numbers in a : table of SQL server 2008 R2 on Win 7. : the table has 10 million records. : e.g. : Id value : 532 937598.32421
这不是数据库的思路。 你可以认为所有的数据都是有序的,可是数据库并不能快速的找到第五百万个数据,以 及第二百五十万,等等。 利用数据库功能的做法似乎是 Select top 5 From (select id, abs(value-your number ) from table Order by abs(value-your number ))
★ 发自iPhone App: ChineseWeb 8.1
【在 l******9 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: light009 (light009), 信区: JobHunting : 标 题: SQL fast search in a 10 million records table : 发信站: BBS 未名空间站 (Thu Jul 24 23:26:56 2014, 美东) : I need to do a fast search in a column with floating point numbers in a : table of SQL server 2008 R2 on Win 7. : the table has 10 million records. : e.g. : Id value : 532 937598.32421