iT邦幫忙

0

關於資料表優化與拆分

  • 分享至 

  • xImage

近期有要緊的問題,我要儲存一個素數區間表,並返回其中所有質數。我自己覺得我的資料表有點問題,在商用環境下能優化的地方很多,但我看不出來。請教各位具體方法如何拆分下圖中重複的資料數據?
[]!(https://ithelp.ithome.com.tw/upload/images/20230814/20162136Oeb2rszKkf.png)

obarisk iT邦研究生 2 級 ‧ 2023-08-15 07:43:50 檢舉
只需要存質數就好。用範圍查詢就好
froce iT邦大師 1 級 ‧ 2023-08-15 08:32:44 檢舉
用張表存質數,然後用查詢的就好,不需要前面的範圍。
有需要的話從最大的數找下一個,然後做到超出範圍就停,存到資料庫裡。
brown125 iT邦新手 5 級 ‧ 2023-08-15 17:48:25 檢舉
感謝各位,這的確解答了我的一些謬誤,看來我太執著要變成表的模式XDD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
海綿寶寶
iT邦大神 1 級 ‧ 2023-08-14 19:22:53

我不會
替你弄個圖片
期待真正高手回答

1
純真的人
iT邦大師 1 級 ‧ 2023-08-15 16:56:54

恩~純玩玩@_@a...

Create table Test(
  PP varchar(max)
);
insert into Test
values('{"start":"1","end":"13"}')
,('{"start":"1","end":"7"}')
,('{"start":"1","end":"200"}')
create function Prime(@Start int ,@End int )
returns varchar(max)
begin
	declare @i int,@j int,@r int
	declare @Str varchar(max)
	set @Str = '['
	set @i=@Start
	while @i<@End
	begin
		set @j=1
		set @r=1
		while @j<@i
		begin
			if @i%@j=0 and @i<>@j and @j<>1
			begin
				set @r=0
				break
			end
			set @j=@j+1
		end

		if @r=1 
		set @Str = @Str + ' ' + Convert(varchar,@i)

		set @i=@i+1
	end
	set @Str = @Str + ' ]'
	return @Str
end
select PP
,dbo.Prime(json_value(PP,'$.start'),json_value(PP,'$.end')) 
from Test

測試結果..
https://dbfiddle.uk/c4Ggangn

https://ithelp.ithome.com.tw/upload/images/20230815/20061369TuDo6Y8T2Z.png

我要發表回答

立即登入回答