开发者

speed up UPDATE operation

Please help me speed up UPDATE operation on the table. Table has on unique id, which used to find record and UPDATE its fields. There are 1M records in the table. No index used. Only unique id. Insertion takes some time.. where I expected milliseconds.

Table Structure:

create table customers
(
    id int IDENTITY(1,1) UNIQUE NOT NULL,
    phonenum varchar(15) UNIQUE NOT NULL,
    date datetime,
    company varchar(150),
    full_name varchar(150),
    address varchar(150),   
    street varchar(100),
    zip varchar(100),
    city varchar(100),
    info varchar(300),
    op_data varchar(150),
    op_date datetime,
    op_user_taken varchar(100),
    op_time_taken  datetime,
    op_status varchar(100),
    user_taken varchar(100),
    time_taken  datetime,
    status varchar(100),        
    );

I'm using SqlConnection System.Data.SqlClient.SqlConnection and ExecuteNonQuery method to UPDATE record.

Does my table need redesigned or ExecuteNonQuery is so slow?

EDIT: Update command:

"UPDATE customers 开发者_如何转开发SET user_taken=@param1, time_taken=@param2,
 date=@param3, company=@param4, full_name=@param55,
 address=@param6, street=@param8, zip=@param9,
 city=@param10, info=@param11 , status=\'Completed\',
op_data=@param12 WHERE id=@param7";


Make your id column a primary key. Primary keys automatically become an index. If you are running a simple update statement such as:

UPDATE customers SET <something> WHERE id = @id 

Then this should run an INDEX SEEK and be much faster. Without that index you are updating with a TABLE SCAN which takes orders of magnitude longer than an INDEX SEEK or even an INDEX SCAN (which would occur if you are updating large amounts of rows).

Another trick is to take your update statement and run it in SQL Server and look at the Execution Plan. This will tell you where the bottleneck is. Also, you can go to Query Options | Advanced | and set statistics to display in the output tab.


I think it's because you don't have clustered index (unique is nonclustered by default), so SQL Server uses Nonclustered Index on a Heap to locate your record for updating(UNIQUE is an index ). Id should be a primary key (which is clustered by default), not UNIQUE.


Try -

using (SqlCommand comm = new SqlCommand("update sql", conn))
{
      SqlDataReader results = (SqlDataReader)comm.ExecuteReader();
}

Just curious...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜