Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:54+00:00 2026-05-26T10:21:54+00:00

I have a query in my MVC application which takes about 20 seconds to

  • 0

I have a query in my MVC application which takes about 20 seconds to complete (using NHibernate 3.1). When I execute the query manually on Management studio it takes 0 seconds.

I’ve seen similiar questions on SO about problems similar to this one, so I took my test one step further.

I intercepted the query using Sql Server Profiler, and executed the query using ADO.NET in my application.

The query that i got from the Profiler is something like: “exec sp_executesql N’select….”

My ADO.NET code:

SqlConnection conn = (SqlConnection) NHibernateManager.Current.Connection;

var query = @"<query from profiler...>";
var cmd = new SqlCommand(query, conn);

SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return RedirectToAction("Index");

This query is also very fast, taking no time to execute.

Also, I’ve seen something very strange on the Profiler. The query, when executed from NH, has the following statistics:

reads: 281702
writes: 0

The one from ADO.NET:

reads: 333
writes: 0

Anyone has any clue? Is there any info I may provide to help diagnose the problem?

I thought it might be related to some connection settings, but the ADO.NET version is using the same connection from NHibernate.

Thanks in advance

UPDATE:

I’m using NHibernate LINQ. The query is enormous, but is a paging query, with just 10 records being fetched.

The parameters that are passed to the “exec sp_executesql” are:

@p0 int,@p1 datetime,@p2 datetime,@p3 bit,@p4 int,@p5 int

@p0=10,@p1=’2009-12-01 00:00:00′,@p2=’2009-12-31 23:59:59′,@p3=0,@p4=1,@p5=0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:21:55+00:00Added an answer on May 26, 2026 at 10:21 am

    I had the ADO.NET and NHibernate using different query-plans, and I was sufering the effects of parameter sniffing on the NH version. Why? Because I had previously made a query with a small date interval, and the stored query-plan was optimized for it.

    Afterwards, when querying with a large date interval, the stored plan was used and it took ages to get a result.

    I confirmed that this was in fact the problem because a simple:

    DBCC FREEPROCCACHE -- clears the query-plan cache
    

    made my query fast again.

    I found 2 ways to solve this:

    • Injecting an “option(recompile)” to the query, using a NH Interceptor
    • Adding a dummy predicate to my NH Linq expression, like: query = query.Where(true) when the expected result-set was small (date interval wise). This way two different query plans would be created, one for large-sets of data and one for small-sets.

    I tried both options, and both worked, but opted for the second approach. It’s a little bit hacky but works really well I my case, because the data is uniformly distributed date-wise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using sping3 mvc in my application. I have some entities which need
I have an ASP.NET MVC 2 application which in part allows a user to
I have a web application built in asp.net mvc using forms authentication. I am
I have a ASP.Net MVC application which serves user pages with URL like -
This is not an MVC topic. I have an ASP.NET Application which performs URL
In my ASP.NET MVC application I have the following GET input field: <% using
I have a MVC application which returns 2 types of Json responses from 2
I have a ASP.NET MVC application for which I want to write some stress
i have this in my BlogRepository public IQueryable<Subnus.MVC.Data.Model.Post> GetPosts() { var query = from
I am using in C# MYsql .I have query that works if I run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.