了解 SSMS 和 ASP.NET 之间的 SQL 性能差异
在 SSMS 中执行 SQL 查询时,您会遇到显着的性能差异到 ASP.NET。如下所示的查询在 SSMS 中运行只需几秒钟,但在您的网站上则需要几分钟。
DECLARE @customerID INT SET @customerID = @CustID DECLARE @MyTable table( Iden int NOT NULL IDENTITY(1,1), ProductID int) INSERT INTO @MyTable(ProductID) SELECT P.ProductID FROM Product P WITH (NOLOCK) left join Compunix_ProductMMY cpmmy with (nolock) on p.ProductID = cpmmy.ProductID left join Compunix_CustomerMMY ccmmy with (nolock) on ccmmy.mmyid = cpmmy.mmyid WHERE P.Deleted=0 AND P.Published=1 and (ccmmy.customerid = @customerID OR cpmmy.productid IS NULL) SELECT c.Name, c.SeName, c.CategoryID FROM Category c WITH (NOLOCK) JOIN ProductCategory PC With (NOLOCK) ON C.CategoryID = PC.CategoryID JOIN @MyTable MT ON PC.ProductID=MT.ProductID WHERE C.Published = 1 GROUP BY c.Name, c.SeName, c.CategoryID ORDER BY c.Name
罪魁祸首:参数嗅探
The造成这种差异的最可能原因是参数嗅探。参数嗅探是一种性能优化技术,SQL Server 在第一次执行时分析查询参数,并根据这些参数创建执行计划。如果稍后查询参数发生更改,SQL Server 可能不会重新编译执行计划并继续使用原始计划,这对于新参数可能不是最佳的。
在这种情况下,查询性能可能会下降,因为首次使用特定 @CustID 值运行查询时优化的执行计划对于使用不同 @CustID 值的后续运行来说并不理想。
解决问题
为了减轻参数嗅探,您可以在查询中使用 FORCESEEK 提示。这会强制 SQL Server 为每次执行查询创建一个新的执行计划,确保它使用给定参数的最佳计划。
要解决此问题,请按如下方式修改查询:
DECLARE @customerID INT SET @customerID = @CustID DECLARE @MyTable table( Iden int NOT NULL IDENTITY(1,1), ProductID int) WITH (FORCESEEK) INSERT INTO @MyTable(ProductID) SELECT P.ProductID FROM Product P WITH (NOLOCK) left join Compunix_ProductMMY cpmmy with (nolock) on p.ProductID = cpmmy.ProductID left join Compunix_CustomerMMY ccmmy with (nolock) on ccmmy.mmyid = cpmmy.mmyid WHERE P.Deleted=0 AND P.Published=1 and (ccmmy.customerid = @customerID OR cpmmy.productid IS NULL) SELECT c.Name, c.SeName, c.CategoryID FROM Category c WITH (NOLOCK) JOIN ProductCategory PC With (NOLOCK) ON C.CategoryID = PC.CategoryID JOIN @MyTable MT ON PC.ProductID=MT.ProductID WHERE C.Published = 1 GROUP BY c.Name, c.SeName, c.CategoryID ORDER BY c.Name
以上是为什么我的 SQL 查询在 SSMS 中很快,但在 ASP.NET 中却很慢?的详细内容。更多信息请关注PHP中文网其他相关文章!

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

查看InMysqlareBeneForsImplifyingComplexqueries,增强安全性,确保dataConsistency,andOptimizingPerformance.1)他们simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver CS6
视觉化网页开发工具