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 7637617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:59:13+00:00 2026-05-31T07:59:13+00:00

I have two queries very similars, using the Linq ExecuteQuery method the first take

  • 0

I have two queries very similars, using the Linq ExecuteQuery method the first take 30 seconds, while the second query is inmediate.

I execute the queries too in Microsoft SQL Server Management Studio and the two queries have a response time of 0 seconds.

Query 1 (slow)

IEnumerable<ViewDataTanksDataDevice> res= 
this.ExecuteQuery<ViewDataTanksDataDevice>(
   "SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB WHERE id_tank = {0} AND date >= {1} order by date", 
   new object[] { tankId, date });

Query 2 (fast)

IEnumerable<ViewDataTanksDataDevice> res= 
this.ExecuteQuery<ViewDataTanksDataDevice>(
   "SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB WHERE id_tank = {0} AND date <= {1}     order by date desc", 
   new object[] { tankId, date });

Note 1: ViewDataTanksDataDevicesSB is a view with SCHEMA BINDING, and it has two index

  1. Index 1(id_tank, date asc)
  2. Index 2(id_tank, date desc)

Note 2: If I execute first the second query the result is identical: Query1 slow and Query 2 fast.

Note 3: The view have millions of registers, and the the results are identical for differents dates and tanks.

  • 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-31T07:59:15+00:00Added an answer on May 31, 2026 at 7:59 am

    I resolved it after one week, viewing the execution plan (thanks Yahia by the suggestion)!!!

    In the two queries I specified the INDEX (thanks Denis) and I had to specify the hint NOEXPAND.

    NOEXPAND explanation can be found at: Table hints

    So, the final queries are:

    Query 1

    IEnumerable<ViewDataTanksDataDevice> res= 
    this.ExecuteQuery<ViewDataTanksDataDevice>(
       "SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB with (index(IX_ViewDataTanksDataDevicesSB_TankIdDate) noexpand) WHERE id_tank = {0} AND date >= {1} order by date", 
       new object[] { tankId, date });
    

    Query 2

    IEnumerable<ViewDataTanksDataDevice> res= 
    this.ExecuteQuery<ViewDataTanksDataDevice>(
       "SELECT TOP 1 * FROM ViewDataTanksDataDevicesSB with (index(IX_ViewDataTanksDataDevicesSB_TankIdDate) noexpand) WHERE id_tank = {0} AND date <= {1} order by date desc", 
       new object[] { tankId, date });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query to select from another sub-query select. While the two queries
I'm seeing OraclePreparedStatement executeQuery() exhibit serialization. That is, I have two queries that I
I have two MySQL queries which are very similiar however they return a COUNT()
I have two very complex queries that are displayed on the same form and
We have two Entity Framework queries, one with Include one with standalone query. Here
I have two queries that are functionally identical. One of them performs very well,
I have two queries, as following: SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name
I have two mysql queries: $sql = SELECT * FROM content WHERE threadName LIKE
I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
I have two applications written in Java that communicate with each other using XML

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.