我目前正在使用Microsoft SQL Server 2017实例。
我有一个Java脚本程序,它基于某种逻辑在语句中插入插入内容,然后逐个执行它们。
由于某些原因,insert into语句要花一些时间才能运行,在某些情况下,每次插入需要30s。
但平均来说,每个插入片段的时间更类似于10-11毫秒。
我不确定,但这对我来说似乎很慢?我一直在使用postgresql DB来处理相同的工作负载,每条记录大约需要2.5ms。
该表的记录数量和字段数量都很大。 (即使表为空,这也很慢)该表现在有2000万条记录。
以下是插入语句之一的示例:
INSERT INTO mipbi_ods.test_table (
"field1"
,"field2"
,"field3"
,"field4"
,"field5"
,"field6"
,"field7"
,"field8"
,"field9"
,"field10"
,"field11"
,"field12"
,"field13"
,"field14"
,"field15"
,"field16"
,"field17"
,"field18"
,"field19"
,"field20"
,"field21"
,"field22"
,"field23"
,"field24"
,"field25"
,"field26"
,"field27"
,"field28"
,"field29"
,"field30"
,"field31"
,"field32"
,"field33"
,"field34"
,"field35"
,"field36"
,"field37"
,"field38"
,"field39"
,"field40"
,"field41"
,"field42"
,"field43"
,"field44"
,"field45"
,"field46"
,"field47"
,"field48"
,"field49"
,"field50"
,"field51"
,"field52"
,"field53"
,"field54"
,"field55"
,"field56"
,"field57"
,"field58"
,"field59"
,"field60"
,"field61"
,"field62"
,"field63"
,"field64"
,"field65"
,"field66"
,"field67"
,"field68"
,"field69"
,"field70"
,"field71"
,"field72"
,"field73"
,"field74"
,"field75"
,"field76"
,"field77"
,"field78"
,"field79"
,"field80"
,"field81"
,"field82"
,"field83"
,"field84"
,"field85"
,"field86"
,"field87"
,"field88"
,"field89"
,"field90"
,"field91"
,"field92"
,"field93"
,"field94"
,"field95"
,"field96"
,"field97"
,"field98"
,"field99"
,"field100"
,"field101"
,"field102"
,"field103"
,"field104"
,"field105"
,"field106"
,"field107"
,"field108"
,"field109"
,"field110"
,"field111"
,"field112"
,"field113"
,"field114"
,"field115"
,"field116"
,"field117"
,"field118"
,"field119"
,"field120"
,"field121"
,"field122"
,"field123"
)
VALUES (
NULL
,CONVERT(DATETIMEOFFSET, '2019/12/14 06:27:48.868+02:00', 121)
,NULL
,NULL
,NULL
,NULL
,3
,'190SZ89M'
,32
,'3023456'
,390496
,CONVERT(DATE, '2019/08/22', 23)
,'000594'
,'69840896'
,1
,'Yeah'
,73.21
,99
,'C'
,73.21
,0
,0
,100
,'D'
,NULL
,'M11'
,CONVERT(DATE, '2019/08/22', 23)
,'RT'
,CONVERT(DATE, '2019/08/25', 23)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,40
,NULL
,'HHHH'
,NULL
,0
,45535671
,171
,390496
,390496
,0
,0
,0
,0
,CONVERT(DATE, '2019/08/22', 23)
,0
,6034284
,NULL
,NULL
,0
,60
,73.21
,0
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,0
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,0
,'CodecodesradfdgdfgdfgZ76.9dfgdfgdfg08/22/2019dfgdfhd60noteM11dfgsedf99dfgdfgdfgCdfgdfgd?dfgdfgdfg?dfgdfgdfgdg?sdfsdfsdfsdf?sdfsdfsdfs?sdfsdfsdf?sfsdfs?sdfsdfsd?sdfsdfsf?sdfsgfghhf?dfdfsde171'
,NULL
,NULL
,CONVERT(DATETIME, '2019/08/22 15:41:03.357', 121)
,NULL
,0
,604
,NULL
,NULL
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,NULL
,'*'
,978443465
,CURRENT_TIMESTAMP
,'CURRENT'
,NULL
,'hdfsd'
,dbo.global_sequence()
,NULL
,'dfgdfgdfg'
);
功能如下:
CREATE FUNCTION [dbo].[global_sequence] () RETURNS INT BEGIN RETURN (SELECT number_i_need.sequence FROM schema1.number_i_need) END GO
这是我的查询执行计划,对于基本插入来说,这看起来很多吗?
答案 0 :(得分:0)
感谢所有建议。 @Larnu特别是让我研究我的功能。我已经设法解决了这个问题。
我发现插入缓慢的原因是我在插入语句中拥有的用户定义函数。然后,我转向使用序列,该序列虽然好很多,但仍然有些慢。
然后我碰到了“ rowversion”,它是一个二进制值,不清晰,但可以订购以便很好地工作。它也可以应用于数据库中所有现有记录,而无需重新插入或更新它们。
我的插入内容现在位于1.9ms范围内,以插入记录。考虑到列数,这不是超级惊人,但足以满足我的目的。