mysql - Improving a query -


i trying simple sql query:

select distinct id marketing type = 'email'   , id not in (                 select id                 marketing                 type = 'letter'                 ) order id; 

it takes long time run, , assume has select in statement (there large number of ids), can't come way improve it.

first can reason query slow, , second suggestion on how improve it?

edit:

database system: mysql

id indexed but not primary key in table; foreign key.

here's alternative query, although according quassnoi here (mysql) should perform similarly.

   select email.id      marketing email left join marketing letter on letter.type='letter' , letter.id=email.id     email.type='email' , letter.id null  group email.id  order email.id; 

the 3 main ways of writing type of query not in, not exists (correlated) or left join/is null. quassnoi compares them mysql (link above), sql server, oracle, , postgresql.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -