select count(tab1.xxId),tab.xxName from tab1 left join tab2 on tab1.xxId=tab2.xxId where tab1.xxName like '%someString1%' and yyName like '%someString2%' group by tab1.xxId oder by tab1.xxId limit 0,20;
调试欢乐多
select count(tab1.xxId),tab1.xxName from tab1 left join tab2 on tab1.xxId=tab2.xxId where tab1.xxName like '%someString1%' and tab2.yyName like '%someString2%' group by tab1.xxId oder by tab1.xxId limit 0,20;
while(mysql_fetch_row()){
query(select count(*) from tab2 where xxId='theXxId' and yyName like '%someString2%');
mysql_fetch_row();
}
还没有用这个快。
mysql> create TEMPORARY TABLE temp_tab_1 select count(*) as num,xxId from tab2 where 1 group by xxId;
Query OK, 1308 rows affected (2.58 sec)
Records: 1308 Duplicates: 0 Warnings: 0mysql> select a.xxId,a.xxName,b.num from tab1 as a left join temp_tab_1 as b on a.xxId=b.xxId where 1 order by a.xxIdlimit 0,20;20 rows in set (1.05 sec)
这样速度就快多了。但是这样create talbe , drop table ,权限太大了,而且多人同时操作可能有问题。有没有什么办法让它不生成临时表而同样能达到这种效果???
大大们帮帮忙!
能给解释一下,为什么临时表很快,而关联却那么慢呢?
另外,请问,有没有一种写法能不建立临时表,却完成我上面那个建立临时表再关联的方法???
但是我这个测试数据库里的数据有点混乱:)-->tab2里有的xxId ,tab1里不一定有。。
另外,我测试了join(代替left join),结果是74s出了结果
原来可能是索引出问题了。
不过这样做where 1 全查仍然需要10s(带条件的要比这个快)。。