database - Optimizing MySQL ORDER BY field of JOINed table -


i have following structure:

create table `tbl1` (  `id` int(11) not null auto_increment,  `tid` int(11) not null,  `user_id` int(11) not null,  `time` datetime not null,  `count` int(11) not null,  primary key (`id`),  unique key `tid_uid` (`tid`,`user_id`),  key `tid_time` (`tid`,`time`) )       create table `tbl2` (  `id` int(11) unsigned not null auto_increment,  `user_id` int(11) unsigned not null,  `field_to_order_by` int(11) unsigned not null,  primary key (`id`),  unique key `user_id` (`user_id`),  key `field_to_order_by` (`field_to_order_by`) )  

i'm trying perform following query:

select * tbl1  left join tbl2 on tbl1.user_id=tbl2.user_id  tbl1.tid=13 , time > date_sub(now(), interval 1 week)  order tbl2.field_to_order_by desc limit 200   

the performance problem i'm facing due order by of joined table field. if remove or replace where condition on same field i'm getting massive improvement.

how/can achieve reasonable performance combination of join , order or can solved de-normalization?

this explain:

id  select_type  table  type    possible_keys     key       key_len  ref           rows       1   simple       tbl1   range   tid_uid,tid_time  tid_time  12       null          221664   using where; using temporary; using filesort   1   simple       tbl2   eq_ref  user_id           user_id   4        tbl1.user_id  1     


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -