join - increase query performance in mysql -
i have used query
select count(case when c <= 1 1 end) nooffamilieshavingcount1, count(case when c between 2 , 4 1 end) nooffamilieshavingcountbetween2and4, count(case when c > 4 1 end) nooffamilieshavingcountgreaterthan3 ( select count(*) c user user_id = (select user_id location location_id in(select location_id country state_name='state')) group house_no ) t
here sub query returning approximately 10000 records . user table has 10,00,000 records. taking time.then error saying server gone away. using mysql.
i searched google.but no luck me.
what changes need tables.how can execute query increasing query performance.please suggest me.thanks in advance....
try query
select count(case when c <= 1 1 end) nooffamilieshavingcount1, count(case when c between 2 , 4 1 end) nooffamilieshavingcountbetween2and4, count(case when c > 4 1 end) nooffamilieshavingcountgreaterthan3 (select count(*) c user u, location l, country c l.state_name='state' , l.some_other_column_id= 4 , <------- add condition c.location_id = l.location_id , u.user_id = l.user_id group u.house_no) t
use proper joins easy understand..
select count(case when c <= 1 1 end) nooffamilieshavingcount1, count(case when c between 2 , 4 1 end) nooffamilieshavingcountbetween2and4, count(case when c > 4 1 end) nooffamilieshavingcountgreaterthan3 (select count(*) c user u inner join location l on l.state_name='state' , l.some_other_column_id= 4 <------- add condition u.user_id = l.user_id inner join country c on c.location_id = l.location_id group u.house_no) t
edited
in cases joins faster sub-queries , rare sub-query faster. accept using subquery more logical , easy understand when comes performance not joins. if using joins db optimize query on own not in case of subquery. try using explain both of query , clear idea how query executes.
hope helps...
Comments
Post a Comment