Solved

Speeding up LINQ query

Posted on 2013-01-25
4
237 Views
Last Modified: 2013-02-08
Hi guys,

I have the below query, which I want filtered on every keystroke in a textbox. Ive got it working but its painfully slow. SQ_SysproHis_Step3 is a view on the server, when run on the server it takes a good 15 seconds to run, but my understanding is that entity framework will then store alot of the data locally so i would expect the load to possibly be slow but the filtering to be quick? But this isnt the case?

 private void LoadGrid()
        {
            using (SmartQuoteEntities CounterfeitContext = new SmartQuoteEntities())
            {
                var counterfeits = from d in CounterfeitContext.SQ_SysproHis_Step3
                                   where d.StockCode.Contains(txtStockCode.Text)
                                   //orderby d.Supplier
                                   select new
                                   {
                                       d.Supplier,
                                       d.SupplierClass,
                                       d.SupplierName,
                                       d.Customer,
                                       d.Name,
                                       d.Manufacturer,
                                       d.PurchaseOrder,
                                       d.OrderDate,
                                       d.StockCode,
                                       d.CustXRef,
                                       d.SalesPerson,
                                       d.Sales_Area,
                                       d.PurchOrderQty,
                                       d.Purchase_ForeignPrice,
                                       d.Purchase_Currency,
                                       d.SoldPricePerUnit__GBP_,
                                       d.Sales_Currency

                                   };

                flx_SysproArchive.ItemsSource = counterfeits.ToList();

            }

        }



        private void txtStockCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            LoadGrid();
        }

Open in new window


Many Thanks,
Dean.
0
Comment
Question by:deanlee17
  • 2
4 Comments
 
LVL 14

Expert Comment

by:binaryevo
ID: 38819415
See how long it takes to pull in the data into an IQueryable.  Looks like this:

var test = CounterfeitContext.SQ_SysproHis_Step3.AsQueryable();

Open in new window

0
 
LVL 4

Expert Comment

by:Srinivasulu Muppala
ID: 38820681
Do the testing of such a way:

First you the SQ_SysproHis_Step3 view in SSMS find how many seconds it's taking to complet

if it is not taking 15 seconds better to go by datareader concept that will reduce your execution time because Entrity framework is pure ORM in huge relational db schema little pain.
0
 

Author Comment

by:deanlee17
ID: 38824112
binaryevo, How can I test how long that takes to load?
0
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
ID: 38830795
The simplest way is to just set a breakpoint and step through your code.  Here is a pretty cool utility and it still looks like its got all the features it used to and more:

http://www.linqpad.net
http://stackoverflow.com/questions/11345886/linqpad-tutorial

Let me know how long the query takes and we can further dig into it a little more.  Also do what srinipro suggested and run the equivalent sql query in mgmt studio so we can use these metrics to help us figure out where the bottleneck is.  Also how many records are you pulling on average?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now