简体   繁体   中英

Site performance decreased after changing varchar to nvarchar

I recently had to convert one of the table's column definition to nvarchar from varchar . Since that, I can feel that the searching data thru the table has became slower.

I have like 4200000+ rows in the table and growing.

My web app doesn't currently use stored procedures to retrieve data from the database. If I use stored proc, will it slightly improve the searching performance?

Or is there any other advice you'd give for improvement?

Here is the query currently used now:

SELECT TOP 100 id, callerID, dateTime, activity, senderNum, msgSent, smsgRespond, msgIn 
FROM tbl_activitylog 
WHERE callerID = @callerID 
ORDER BY id DESC

The column msgSent is the one which was converted to nvarchar.

Below is the table structure:

id (int, Primary Key, Auto Increment)  
callerID (bigint)  
dateTime (datetime)  
activity (varchar(50)  
senderNum (int)  
msgSent (nvarchar(160))  
smsgRespond (varchar(50))  
msgIn (varchar(160))  

I do not understand the index part.

I did not know about the indexing part so I guess I didn't do any index in the database.

The number one most important thing when dealing with database performance is INDEXES .

Add an index on (callerID, id DESC) .

Your query will be much, MUCH faster.

You can also run your query in SSMS, and press the "Estimated query plan" and it will most likely come with a missing index warning. You can practically copy and paste this warning and just run it. The only thing you need to change is the name of the index.

EDIT: Putting your query into a stored procedure don't bring you automatically better performance. So if you can retrieve all the data you need with "simple SELECT statements", do it like this.

But you should check and eventually repair your database.

DBCC CHECKDB('<db_name>') -- check the db for error
DBCC UPDATEUSAGE('<db_name>') -- fix errors

Also important create relevant indexes!

EDIT: AS you post your query after: Add index on CalledId.

Check your SQL SELECTS, check what columsn you have in your WHERE statements and add indexes for them. This should improve a lot!

If you have an index against varchar and query contains Nvarhchar then such index will be ignored. You need to sync all types used (the same everywhere) and rebuild the index , eg:

ALTER INDEX IX_msgSent ON tbl_activitylog REBUILD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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