我在Log表中有几百万条记录想要删除。我在下面使用SQL删除500个记录块中的一个表,以防止数据库锁定,不幸的是,Log表仍然被锁定,所有新连接都无法访问该表。
DECLARE @Deleted_Rows INT;
SET @Deleted_Rows = 1;
WHILE (@Deleted_Rows > 0)
BEGIN
BEGIN TRANSACTION
-- Delete some small number of rows at a time
DELETE TOP (500) Log
WHERE UserId = 3905
SET @Deleted_Rows = @@ROWCOUNT;
COMMIT TRANSACTION
END
SQL是从Microsoft SQL Server Management Studio运行的,当我单击菜单中的“停止”按钮以停止SQL执行时,该锁仍保留在“日志”表上,并且仍然不允许任何新连接。
当我尝试关闭Microsoft SQL Server Management Studio本身时,出现确认对话框
TITLE: Microsoft SQL Server Management Studio
------------------------------
There are uncommitted transactions. Do you wish to commit these transactions before closing the window?
------------------------------
BUTTONS:
&Yes
&No
Cancel
------------------------------
只有当我单击“否”按钮时,桌子才会被解锁。
那么在不关闭数据库的情况下从表中删除大量记录的正确方法是什么?
答案 0 :(得分:4)
首先,将$margin_max_constant_notch:unquote('max(-12px, constant(safe-area-inset-left))');
$margin_max_env_notch:unquote('max(-12px, env(safe-area-inset-left))');
/*** iphone X 1.11, iphone XS (quote is OR) ***/
@media only screen
and (device-width : 375px)
and (max-device-width : 812px)
and (-webkit-device-pixel-ratio : 3),
/*** iphone XR ***/
screen and (device-width : 414px)
and (device-height : 896px)
and (-webkit-device-pixel-ratio : 2),
/*** iphone XS Max ***/
screen and (device-width : 414px)
and (device-height : 896px)
and (-webkit-device-pixel-ratio : 3),
/*** iphone XS Max Retina ***/
only screen and (-webkit-min-device-pixel-ratio: 3),
only screen and ( min--moz-device-pixel-ratio: 3),
only screen and ( -o-min-device-pixel-ratio: 3/1),
only screen and ( min-device-pixel-ratio: 3),
only screen and ( min-resolution: 458dpi),
only screen and ( min-resolution: 3dppx),
/** Google Pixel 3 XL **/
screen and (device-width: 360px)
and (device-height: 740px)
and (-webkit-min-device-pixel-ratio: 4),
only screen and ( min--moz-device-pixel-ratio: 4),
only screen and ( -o-min-device-pixel-ratio: 4/1),
only screen and ( min-device-pixel-ratio: 4),
only screen and ( min-resolution: 523dpi),
only screen and ( min-resolution: 4dppx) {
@media(orientation: portrait) {
/* mobile - vertical */
@media (max-width: 768px) {
/* up to 768px */
}
@media (max-width: 480px) {
/* up to 480px */
}
@media only screen and (max-width: 400px) {
/* up to 400px */
}
}
@media(orientation: landscape) {
html,body {
padding: $margin_max_constant_notch;
padding: $margin_max_env_notch;
}
/* mobile - horizontal */
@media screen and (max-width: 900px) {
/* up to 900px */
}
}
}
/** iphone X 1.12 **/
@supports(padding: max(0px)) {
@media screen and (device-width : 375px)
and (device-height : 812px)
and (-webkit-device-pixel-ratio : 3) {
@media(orientation: portrait) {
/* mobile - vertical */
@media (max-width: 768px) {
//até 768px
}
@media (max-width: 480px) {
/* up to 480px */
}
@media only screen and (max-width: 400px) {
/* up to 400px */
}
}
@media(orientation: landscape) {
html, body {
padding: $margin_max_constant_notch;
padding: $margin_max_env_notch;
}
@media screen and (max-width: 900px) {
/* up to 900px */
}
}
}
}
/** iphone 8 **/
@media only screen
and (device-width : 375px)
and (device-height : 667px)
and (-webkit-device-pixel-ratio : 2),
/** iphone 8 PLUS **/
screen and (device-width : 414px)
and (device-height : 736px)
and (-webkit-device-pixel-ratio : 3) {
@media(orientation: portrait) {
/* mobile - vertical */
}
@media(orientation: landscape) {
/* mobile - horizontal */
}
}
@media only screen
/** IPADS **/
and (min-device-width: 1024px)
and (max-device-width: 1366px)
and (-webkit-min-device-pixel-ratio: 2) {
/* for ipads */
@media(orientation: portrait) {
/* ipad - vertical */
}
@media(orientation: landscape) {
/* ipad - horizontal */
}
}
添加到脚本的开头。如果由于超时或手动取消而导致客户端应用程序取消了查询,这将确保回滚事务。
如果该表在SET XACT_ABORT ON;
上具有索引,我不希望您的脚本锁定整个表,从而有助于更细化的锁定并提高并发性。另外,请确保文字(此处为整数)与列类型匹配。如果没有索引或不使用索引,则必须为每个UserId
迭代扫描表,因为由于data type precedence rules,列的值必须转换为int以便进行比较(例如,如果列数据类型是DELETE
,则应将文字括在单引号中以允许索引查找而不是扫描。)