sql server 2008r2 向带有索引的表里大批量插入数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql server 2008r2 向带有索引的表里大批量插入数据相关的知识,希望对你有一定的参考价值。
sql server 2008 r2
如果有一个表,千万级,带索引,怎么插入数据可以快点?最好有代码
表有4-5个列,其中一个varchar(350),一个varchar(16),是用来存储短信内容的
sqlbulkcopy的话,只有100条每秒(个人电脑,目标是2000,这样服务器才不会太差)
肯定不能去掉索引的,因为这是实时短信系统,要求对短信内容进行审核,还要去重的
追答嗯,那你用表值参数方法试试吧,能比bulk快一些,但可能达不到你要的速度。不过如果到服务器上性能应该还能提升一些。
参考技术A 去索引插会更快追问肯定不能去掉索引的,因为这是实时短信系统,要求对短信内容进行审核,还要去重的
带有 TIMESTAMP 的 SQL Server 中的表连接问题
【中文标题】带有 TIMESTAMP 的 SQL Server 中的表连接问题【英文标题】:Table joining issue in SQL Server with TIMESTAMP 【发布时间】:2019-05-27 06:58:40 【问题描述】:我想从 SQL Server 中的三个表中获取当天的详细信息。我无法使用TIMESTAMP
加入表格,每个表格中的TIMESTAMP
都不同。连接这些表没有共同的因素。有什么办法可以加入这些表。而且我无法修改表格,这些表格来自第三方服务。
查询:
SELECT *
FROM table1
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT *
FROM table2
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT *
FROM table3
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT
CAST(one.TIMESTAMP AS DATETIME2(0)) AS DATE,
CONVERT(DECIMAL(10, 2), one.value) AS v1,
CONVERT(DECIMAL(10, 2), two.VALUE) AS v2,
CONVERT(DECIMAL(10, 2), three.VALUE) AS v3
FROM
table1 one
LEFT JOIN
table2 two ON CAST(one.TIMESTAMP AS DATETIME2(0)) = CAST(two.TIMESTAMP AS DATETIME2(0))
LEFT JOIN
table3 three ON CAST(one.TIMESTAMP AS DATETIME2(0)) = CAST(three.TIMESTAMP AS DATETIME2(0))
WHERE
CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY
CAST(one.TIMESTAMP AS DATETIME2(0))
结果:
我没有得到第 2 表和第 3 表的数据。例如:2018-12-30 00:00:15.657
这次是从第三张表的第一行开始。结果中没有显示。我想要所有数据。
希望你能理解。你能帮我解决这个问题吗?
【问题讨论】:
具体是什么问题?首先,它sez“无法加入”,然后“加入”完美运行。请说明预期结果,以及这与加入有何不同。然后,我们可以重现这一点,删除 jpeg 或 png 输入,并将它们作为副本替换为 headers/paste,然后用大括号框住每一个以保持间距。 @donPablo 我刚刚编辑了我的问题。 加入row_number? @P.Salmon which row_number?你是说身份证? 我假设您希望加入每个表上的第一个条目,每个表上的第二个条目等。您不能加入 id,因为它们不一样,但您可以根据每个表生成 row_number id 并加入。 docs.microsoft.com/en-us/sql/t-sql/functions/… 和 google sqlserver row_number 了解更多信息。 【参考方案1】:或者,只需修改 WHERE
WHERE
CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
OR CAST(two.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
OR CAST(three.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
【讨论】:
这个步骤我试过了,但是不能正常工作。【参考方案2】:你可能想要full join
:
SELECT COALESCE(CAST(one.TIMESTAMP AS DATETIME2(0)),
CAST(two.TIMESTAMP AS DATETIME2(0)),
CAST(three.TIMESTAMP AS DATETIME2(0))
) as dt,
CONVERT(DECIMAL(10, 2), one.value) AS v1,
CONVERT(DECIMAL(10, 2), two.VALUE) AS v2,
CONVERT(DECIMAL(10, 2), three.VALUE) AS v3
FROM (SELECT *
FROM table1 one
WHERE CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) one FULL JOIN
(SELECT *
FROM table2 two
WHERE CAST(two.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) two
ON CAST(two.TIMESTAMP AS DATETIME2(0)) = CAST(on.TIMESTAMP AS DATETIME2(0)) FULL JOIN
(SELECT *
FROM table3 three
WHERE CAST(three.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) three
ON CAST(three.TIMESTAMP AS DATETIME2(0)) = COALESCE(CAST(one.TIMESTAMP AS DATETIME2(0)), CAST(one.TIMESTAMP AS DATETIME2(0)))
ORDER BY dt;
FULL JOIN
s 过滤起来很棘手,这就是它使用子查询的原因。
【讨论】:
以上是关于sql server 2008r2 向带有索引的表里大批量插入数据的主要内容,如果未能解决你的问题,请参考以下文章