Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to Sql and T-SQL Performance Discrepancy
    primarykey
    data
    text
    <p>I have an MVC web site the presents a paged list of data records from a SQL Server database. The UI allows the user to filter the returned data on a number of different criteria, e.g. email address. Here is a snippet of code:</p> <pre><code>Stopwatch stopwatch = new Stopwatch(); var temp = SubscriberDB .GetSubscribers(model.Filter, model.PagingInfo); // Inspect SQL expression here stopwatch.Start(); model.Subscribers = temp.ToList(); stopwatch.Stop(); // 9 seconds plus compared to &lt; 1 second in Query Analyzer </code></pre> <p>When this code is run, the StopWatch shows an execution time of around 9 seconds. If I capture the generated SQL expression (just before it is evaluated with the .ToList() method) and cut'n'paste that as a query into SQL Server Management Studio, the execution times drops to less than 1 second. For reference here is the generated SQL expression:</p> <pre><code>SELECT [t2].[SubscriberId], [t2].[Email], [t3].[Reference] AS [DataSet], [t4].[Reference] AS [DataSource], [t2].[Created] FROM ( SELECT [t1].[SubscriberId], [t1].[SubscriberDataSetId], [t1].[SubscriberDataSourceId], [t1].[Email], [t1].[Created], [t1].[ROW_NUMBER] FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [t0].[Email], [t0].[SubscriberDataSetId]) AS [ROW_NUMBER], [t0].[SubscriberId], [t0].[SubscriberDataSetId], [t0].[SubscriberDataSourceId], [t0].[Email], [t0].[Created] FROM [dbo].[inbox_Subscriber] AS [t0] WHERE [t0].[Email] LIKE '%_EMAIL_ADDRESS_%' ) AS [t1] WHERE [t1].[ROW_NUMBER] BETWEEN 0 + 1 AND 0 + 20 ) AS [t2] INNER JOIN [dbo].[inbox_SubscriberDataSet] AS [t3] ON [t3].[SubscriberDataSetId] = [t2].[SubscriberDataSetId] INNER JOIN [dbo].[inbox_SubscriberDataSource] AS [t4] ON [t4].[SubscriberDataSourceId] = [t2].[SubscriberDataSourceId] ORDER BY [t2].[ROW_NUMBER] </code></pre> <p>If I remove the email filter clause, then the controller's StopWatch returns a similar response time to the SQL Management Studio query, less than 1 second - so I am assuming that the basic interface to SQL plumbing is working correctly and that the problem lies with the evaluation of the Linq expression. I should also mention that this is quite a large database with upwards of 1M rows in the subscriber table.</p> <p>Can anyone throw any light on why there should be such a high (x10) performance differential and what, if anything can be done to address this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload