简体   繁体   English

插入时,Sql select在表格上很慢

[英]Sql select is slow on table while it has been inserted

I have a background job (C# console application) which inserts continuously data in product_titleParts table. 我有一个后台作业(C#控制台应用程序),它在product_titleParts表中连续插入数据。 Simply this job selects top 100 products from products table, splits title into part and inserts into product_titleParts table. 只需这个工作从产品表中选择前100个产品,将标题分成部分并插入到product_titleParts表中。

This table has a indexed column called "TitlePart" 该表有一个名为“TitlePart”的索引列

On the other hand, when I try to select from this table SQL query takes forever. 另一方面,当我尝试从这个表中选择SQL查询需要永远。 If I stop console app after a while query takes 0 seconds. 如果我在一段时间后查询停止控制台应用程序需要0秒。 As soon as I start console app again select query unresponsive again. 一旦我再次启动控制台应用程序,请再次选择查询无响应。

Selecting from a table while another job is inserting would cause any slowness? 插入另一个作业时从表中选择会导致任何缓慢? I am using nolock on the select but did not help. 我在选择中使用nolock但没有帮助。

Any Idea? 任何想法?

My Code on Console App: 我的控制台应用程序代码:

if not exists(select 1 from product_titleParts where productid = @productid
and UserId = @userid and titlePart = @titlePart)
begin
insert into product_titleParts (userid, productid, titlePart)
VALUES (@userid, @productid, @titlePart)
end

My Select Code: 我的选择代码:

select productid from product_titleParts 
inner join products
on products.productid = product_titleParts.productid
where titlePart = @titlePart

I had simmilar problems with inserting "mass data" while selecting on the other hand (DB/2) 另一方面,我选择插入“海量数据”时遇到了类似的问题(DB / 2)

what was my solution: 我的解决方案是什么:

inserts are expensive, so i tied them to a block like: 插入是昂贵的,所以我将它们绑在一个块上,如:

 insert into ...
 values ((a,b,c),(x,y,z), ... )

that locks the table only once for eg. 只锁定表一次,例如。 100 inserts and indices are only rebuild once 100个插入和索引仅重建一次

And there is an other point: when you are inserting (updataing) many rows, try to lock the table (depends on DBMS) in exclusive mode. 还有一点:当您插入(更新)多行时,尝试以独占模式锁定表(取决于DBMS)。 You prevent whats called "lock escalation" 你防止什么称为“锁定升级”

in DB2 you would use 在DB2中你会使用

   lock table xyz in exclusive mode
   update/insert many rows
   unlock table xyz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM