简体   繁体   English

将varchar更改为nvarchar后,站点性能下降

[英]Site performance decreased after changing varchar to nvarchar

I recently had to convert one of the table's column definition to nvarchar from varchar . 我最近不得不将表的列定义之一转换为varchar nvarchar 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. 我在表中有4200000多行并且正在增长。

My web app doesn't currently use stored procedures to retrieve data from the database. 我的Web应用程序当前不使用存储过程从数据库中检索数据。 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. msgSent列是转换为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 . 处理数据库性能时最重要的是INDEXES

Add an index on (callerID, id DESC) . (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. 您还可以在SSMS中运行查询,然后按“估计的查询计划”,它很可能会出现缺少索引警告。 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. 因此,如果您可以使用“简单的SELECT语句”检索所需的所有数据,请执行此操作。

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. 编辑:在您发布查询后:在CalledId上添加索引。

Check your SQL SELECTS, check what columsn you have in your WHERE statements and add indexes for them. 检查SQL SELECTS,检查WHERE语句中的columsn,并为它们添加索引。 This should improve a lot! 这应该会改善很多!

If you have an index against varchar and query contains Nvarhchar then such index will be ignored. 如果你有一个针对varchar的索引,而查询包含Nvarhchar那么这个索引将被忽略。 You need to sync all types used (the same everywhere) and rebuild the index , eg: 您需要同步所有使用的类型(在任何地方都相同)并重建索引 ,例如:

ALTER INDEX IX_msgSent ON tbl_activitylog REBUILD

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

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