views:

53

answers:

3

Hi,

I'm using query, where the piece is:

...where code in ('va1','var2'...')

I have about 50k of this codes.

It was working when I has 30k codes, but know I get:

The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partition

I think that problem is related with IN...

So now I'm planning use foreach(string code in codes) ...where code =code

Is it good Idea ??

+3  A: 

I suggest you create a temporary table containing all the codes you want to match, and join against that instead.

However, we haven't really seen enough of your code to comment for sure.

Jon Skeet
A: 

I'm not sure where you got those 50k values, but if it is from another query, just JOIN to this table and get all the data at one time from one query.

KM
I have something like this:public Car[] GetCars(string[] codes){}
+1  A: 

First, create a temp table, call it #tmp0, with just one column. Then:

SqlConnection conn = new SqlConnexion(connection_string);
SqlCommand cmd = new SqlCommand("INSERT INTO #tmp0 (code) VALUE (@code)", conn);
conn.Open();

foreach (string s in bigListOfCodes)
{
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@code", s);
    cmd.ExecuteNonQuery();
}
cmd = new SqlCommand("SELECT * FROM tbl " + 
          "WHERE code IN (SELECT CODE FROM #tmp0)", conn);

SqlDataReader rs = cmd.ExecuteReader();

while (rs.Read())
{
    /* do stuff */
}

cmd = new SqlCommand("DROP TABLE #tmp0", conn);
cmd.ExecuteNonQuery();
conn.Close();

I know this seems like a lot of work for the server, but it's really pretty fast.

egrunin
could you look at http://stackoverflow.com/questions/2725353/there-is-insufficient-system-memory-to-run-this-query-when-creating-temporary-tab
That's **much** worse -- please try this approach first or at least tell us what you don't like about it. I realize that 30k `INSERT` statements might take 15 seconds to run, but I'd have to know more about where your data is coming from to give you a better solution.
egrunin
it comes from `public Car[] GetCars(string[] codes) { }`
KM