Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Linq-to-SQL will generate the equivalent SQL query for your Linq code. What that means is that it will filter in the same order you specify. It doesn't really have a way to know which will be faster without running it to test.</p> <p>Either way round, your first filtering will be operating on the whole dataset, and will therefore be slow. However...</p> <ul> <li>If you filter on the rare condition first, then it can cut the whole table down to a small set of results. Then your second filtering has only a small set to work on, which doesn't take long.</li> <li>If you filter on the common condition first, then the set of data left afterwards is still quite large. The second filtering therefore operates on a large set of data, and therefore takes a little longer.</li> </ul> <p>So, rare first means slow + fast, while common first means slow + slow. The only way for Linq-to-SQL to optimise this distinction away for you is to first make a query to check which of the two conditions is rarer, but this means that the generated SQL would either be different each time you ran it (and therefore couldn't be cached to speed it up) or would be significantly more complex than what you wrote in Linq (which the Linq-to-SQL designers didn't want, probably because it could make debugging a nightmare for the user).</p> <p>There's nothing to stop you from making this optimisation yourself though; add a query beforehand to count and see which of the two filters will produce a smaller result set for the second filter to work on. For small databases, this will be slower in almost every case because you're making a whole extra query, but if your database is big enough and your check query is clever it might end up being faster on average. Also, it might be possible to work out how many there would have to be of condition A for it to be faster regardless of how many condition B objects you have, and then just count condition A, which would help make the check query faster.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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