sql - Comparing 2 tables , find not existing "id" -
i have speed problem when comparing 2 tables. let's have following tables.
table a 14,000 records
name(varchar) , join_id(int)
table b 54,209 records
second_name , join_id(int)
now want find rows exists in table , not in b connected joint_id ordered random ( sort of )
i tried following :
select a.name , b.second_name left outer join b on a.joint_id = b.joint_id b.joint_id null limit 0,10
the query took ages , messing server , question ;
is there faster way accomplish ?
edit : removed rand() , can solved otherwise. still have same problem. try out suggestions below , see if can improve query time.
notice gave second column fixed value of null, since when b not exist, b.second_name => null. crux of problem order rand()
require full scan put rand() against each record. cannot avoid table scan.
select a.name , null second_name not exists (select * b a.joint_id = b.joint_id) order rand() limit 0,10;
you possibly make faster not having drag columns table a, assuming have id on table a. http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
select a.name , null second_name a.id in ( select id not exists (select * b a.joint_id = b.joint_id) order rand() limit 0,10)
Comments
Post a Comment