c# - SQL huge selection of IDs - How to make it faster? -
I have a large volume ID that I would like to choose from DB.
The usual way would be to choose blabla from xxx where yyy IN (id) option (RECOMPILE)
. ( Option recompile is necessary, because SQL Server is not smart enough that this query is a big waste to put your query in the cache)
However, SQL Server is thus Query on the query when the amount of the id is high, the parser, which uses it to slow down, give me an example:
SELECT * to the table WHERE id (288525, 288528, 288529, & Lt; approximately 5000 ida, 403043, 403044) option (RECOMPILE)
to execute Time: ~ 1100 msec (this gives the appx 200 rows in my example)
versus:
SELECT * 2888525 and 403044 options Time to execute
with table WHERE id between (RECOMPILE): ~ 80 msec (this gives 50000 lines appx in my example)
Even if I get 250 times as much data, it makes 14 times faster ...
So I created this function to get my ID list and make something that would be appropriate The The agreement between the two (some that do not return 250 times as much data, still gives the benefit of parsing the query)
private const int MAX_NUMBER_OF_EXTRA_OBJECTS_TO_FETCH = 5; Public Static String Mass IDsConstringBuilder (List & lt; int & gt; Keys, Ref'Inte StartInx, String Callman) {const int maxlength = 63000; If (keys. Count - start == 1) {string idstring = String.Format ("{0} = {1}", colname, keys [startindex]); STARTINDEX ++; Back idrace; } Stringbiller sb = New stringbiller (max-length + 1000); & Lt; Integer & gt; Individualkeys = new list & lt; Int & gt; (256); Int min = keys [startindex ++]; Int max = min; (; Startindex & lt; keys) between {1} and {2} \ n "; between const string and =" {0} between Sb.Append ("("); calculation & amp; sb.Length + Individualkeys.Count * 8 & lt; maxlength; startindex ++) {int key = keys [startindex]; if (key> max + MAX_NUMBER_OF_EXTRA_OBJECTS_TO_FETCH) {if (min == max) individualkeys.Add (min); else { If (sb.Length & gt; 2) Sb.Append ("OR"); sb.AppendFormat (center and, colname, min, max);} min = max = key;} and {max = key;}} (Min == max) individualkeys.Add (min); and (if (sb.lenthambiti; 2) sb.Append ("OR"); sb.AppendFormat (between, column, min, max);} if ( Individualkeys.Count> 0) {if (sb.leng> 2) sb.Append ("OR"); string [] individualkeysst R = new string [ind ividualkeys.Count]; for (int i = 0; i & lt; individual code; IT; i ++) personalization [i] = personal key [i] ostring (); sb .AppendFormat ("{0} IN ({1})", colname, String.Join (",", individualkeysstr))} Sb.Append (")"); return sb.ToString ();}
It is then used like this:
list & lt; Int & gt; Copper; // Sort and Create Unique ... (int i = 0; i & lt; keys; calculation;) {string idstring = MassIdSelectionStringBuilder (key, ref I, "id"); String sqlstring = string.format ("SELECT * FROM table WHERE {0} OPTION (RECOMPILE)", idstrings);
However, my question is ... does anyone know this in a better / sharp / clever manner?
The fastest way in my experience is to pack numbers in binary format in an image. I was sending the ID of 100, which works fine:
It was just a while ago The following articles are updated by Erland Sommarskog:
Comments
Post a Comment