Longitud de consulta de MySQL

I have more than 400 000 id's in NOT IN statement. Whether it will execute or not ?

$query = "
   SELECT
      *
   FROM
       table_name
   WHERE
       my_field_id NOT IN(
           34535345,3453451234,234242345,3465465,12234234,23435465,122343,345435,3453454,
           34535345,3453451234,234242345,3465465,12234234,23435465,122343,345435,3453454,
           34535345,3453451234,234242345,3465465,12234234,23435465,122343,345435,3453454,
           34535345,3453451234,234242345,3465465,12234234,23435465,122343,345435,3453454,
           34535345,3453451234,234242345,3465465,12234234,23435465,122343,345435,3453454,
           34535345,3453451234,234242345,3465465,12234234,23435465,122343,345435,3453454
       )
";

preguntado el 02 de julio de 12 a las 06:07

@miqdad ali can u tell me the execution time of this query it seems intrseting to me :) -

@Rinzler: this query most likely produces table full scan. So the execution time depends on the table size -

@xdazz These id's I am getting from another database ... -

@Rinzler i didn't tried this query.. first I have to confirm this willn't make my server down.. -

@zerkms ya i know that but has he is saying lets assume there are 4 lakh ids in not in statements then what be the time taken ? any other better way to optimise it came to my mind :) -

3 Respuestas

Yes it will (you could try that yourself without asking here actually). There is no reasonable limit for sql query string.

But you should keep in mind the more ids you add - the slower the query will be

PS: the only mysql setting you may be interested in is max_allowed_packet. From what I remember it is the only parameter that could bring some issues on extra-large queries

Respondido 02 Jul 12, 06:07

Thanks .... I din't tried this because, I afraid of this query will make my server down ... That's why first I am confirming :) - Miqdad Alí

@Miqdad Ali: don't you have a development server?! - zerkms

As I know the IN clause in MySQL does not have limitations, you may write as many id as you want. Agree with zerkms about the performance and max_allowed_packet variable.

As a workaround - try to populate another table (maybe temporary) with indexed id with your values, and then loin these two tables using JOIN clause.

Respondido 02 Jul 12, 07:07

AFAIK the limit is 1024 characters for NOT IN. If you use a sub-select there is no limit. You could populate a memory table with the id's, get them with a sub-select in the query and drop the memory table afterwards. This may even be faster.

Respondido 01 Jul 14, 07:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.