title: Optimizing performance of flexible Linq to Entity criteria when retrieving rows from huge database
body: <p>I have the DB that contains billions of rows.
  I created function that recieve from user number of parameters and cut the DB by those parameters. 
  This works well for me with small DB(30000 rows), but when I try to use this function on big DB I got <code>TIMEOUTEXCEPTION from SQLSERVER</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public static IQueryable&lt;LogViewer.EF.InternetEF.Log&gt; ExecuteInternetGetLogsQuery(FilterCriteria p_Criteria, ref GridView p_Datagrid)
  {
      IQueryable&lt;LogViewer.EF.InternetEF.Log&gt; internetQuery = null;
  
      using (InternetDBConnectionString context = new InternetDBConnectionString())
      {
          internetQuery = context.Logs;
          if ((p_Criteria.DateTo != null &amp;&amp; p_Criteria.DateFrom != null))
          {
              internetQuery = internetQuery.Where(c =&gt; c.Timestamp &gt;= p_Criteria.DateFrom &amp;&amp; c.Timestamp &lt; p_Criteria.DateTo);
          }
          else if (p_Criteria.DateFrom != null &amp;&amp; p_Criteria.DateFrom &gt; DateTime.MinValue)
          {
              internetQuery = internetQuery.Where(c =&gt; c.Timestamp &gt;= p_Criteria.DateFrom);
          }
          else if (p_Criteria.DateTo != null &amp;&amp; p_Criteria.DateTo &gt; DateTime.MinValue)
          {
              internetQuery = internetQuery.Where(c =&gt; c.Timestamp &lt; p_Criteria.DateTo);
          }
          if (!string.IsNullOrEmpty(p_Criteria.FreeText))
          {
              internetQuery = internetQuery.Where(c =&gt; c.FormattedMessage.Contains(p_Criteria.FreeText));
          }
  
          if (p_Criteria.Titles.Count &gt; 0)
          {
              internetQuery = internetQuery.AsEnumerable().Where(c =&gt; p_Criteria.Titles.Contains(c.Title)).AsQueryable();
          }
          if (p_Criteria.MachineNames.Count &gt; 0)
          {
              internetQuery = internetQuery.AsEnumerable().Where(c =&gt; p_Criteria.MachineNames.Contains(c.MachineName)).AsQueryable();
          }
          if (p_Criteria.Severities.Count &gt; 0)
          {
              internetQuery = internetQuery.AsEnumerable().Where(c =&gt; p_Criteria.Severities.Contains(c.Severity)).AsQueryable();
          }
          internetQuery= internetQuery.OrderByDescending(c=&gt;c.LogID);
          if (internetQuery.Count() &gt; p_Criteria.TopValue)
          {
              internetQuery = internetQuery.Take(p_Criteria.TopValue);
          }
          p_Datagrid.DataSource = internetQuery;
          p_Datagrid.DataBind();
          return internetQuery;
  
      }  
  }
  </code></pre>
  
  <p>My version of SQL is 2005. 
  I got an exception on <code>p_Datagrid.DataBind();</code> row.</p>
  
  <p>Any suggetions?
  Thanks</p>