php - MySQL SELECT JOIN WHERE ORDER BY performance -


i'm having 2 tables growing , queries performing nice, don't perform quick anymore.

i'm having 2 tables:

# accounts table create table `account` (   `id` int(11) unsigned not null auto_increment,   `firstname` varchar(50) default null,   `lastname` varchar(50) default null,   `email` varchar(100) default null,   primary key (`id`),   key `email` (`email`) ) engine=innodb default charset=utf8;  # orders table create table `order` (   `id` int(11) unsigned not null auto_increment,   `account_id` int(11) default null,   `datetime` datetime default null,   `ip` varchar(50) default null,   primary key (`id`),   key `account_id` (`account_id`) ) engine=innodb default charset=utf8; 

see sqlfiddle full example data.

the query in question

select      o.id,     a.email `order` o join account on o.account_id = a.id a.email '%@hotmail.com%' order o.id desc limit 2 

this query running fast couple of records. using explain feature shows searches records in account table, not te limited set of 2. enter image description here

this order o.id desc cause trouble in bigger tables. how can fix best way?

  1. split query in 2 queries. first matching accounts, , second query on order where o.account_id in (1, 3, 5) order o.id desc.
  2. use subqueries (no clue how to).
  3. other options...

i'm open suggestions on how handle kind of queries in bigger datasets.

note: know question asked several times, questions don't have answer can use.

how this:

select  o.id, a.email `order` o join (select id, email  account  email '%@hotmail.com%') on o.account_id = a.id order o.id desc; 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -