mysql - Setting a LIMIT before JOINs of tables -


i have following tables:

client_purchases:

id_sale | id_client | timestamp 

files_purchases:

id_sale | id_file 

so 1 purchase of client can buy many files , files can bought several times.

i select want this:

select cp.id_sale, fp.id_file  client_purchases cp     join file_purchases fp     on cp.id_sale = fp.id_sale; 

works fine. this:

id_sale | id_file     1         1    1         2    1         3    2         1    3         1 

now make sure doesn't take forever through database if grows wanted limit amount of rows.

select cp.id_sale, fp.id_file  client_purchases cp      join file_purchases fp      on cp.id_sale = fp.id_sale  limit 0,25; 

whick returns me 25 rows. acctually want 25 different "id_sale". there method tell sql count destinctvalues of column , stop if value reaches specified number? , need able set start , end value of limit.

you can use join + subquery

select cp.id_sale, fp.id_file     (select id, id_sale client_purchases order id limit 25) cp  join (select id file_purchases order id limit 25) fp      on cp.id_sale = fp.id_sale  

however may speed query or may make go slower. depends on kinds of indexes have , how many records have in table.

what seems fast 100 records might slow 100m records , vice verce.


Comments